1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4
5 #ifndef ICE_FILE_UTIL_H
6 #define ICE_FILE_UTIL_H
7
8 #include <IceUtil/Config.h>
9 #include <IceUtil/Shared.h>
10 #include <IceUtil/Handle.h>
11
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15
16 namespace IceUtilInternal
17 {
18
19 extern const ICE_API std::string pathsep;
20 extern const ICE_API std::string separator;
21
22 //
23 // Detemine if path is an absolute path.
24 //
25 ICE_API bool isAbsolutePath(const std::string&);
26
27 //
28 // Determine if a file exists.
29 //
30 ICE_API bool fileExists(const std::string&);
31
32 //
33 // Determine if a directory exists.
34 //
35 ICE_API bool directoryExists(const std::string&);
36
37 //
38 // Determine if a directory exists and is empty.
39 //
40 ICE_API bool isEmptyDirectory(const std::string&);
41
42 #ifdef _WIN32
43
44 #if defined(__MINGW32__)
45 typedef struct _stat structstat;
46 #else
47 typedef struct _stat64i32 structstat;
48 #endif
49
50 #ifdef _MSC_VER
51 #ifndef O_RDONLY
52 # define O_RDONLY _O_RDONLY
53 #endif
54
55 #ifndef O_BINARY
56 # define O_BINARY _O_BINARY
57 #endif
58
59 #ifndef S_ISDIR
60 # define S_ISDIR(mode) ((mode) & _S_IFDIR)
61 #endif
62
63 #ifndef S_ISREG
64 # define S_ISREG(mode) ((mode) & _S_IFREG)
65 #endif
66 #endif
67
68 #else
69
70 typedef struct stat structstat;
71 # define O_BINARY 0
72
73 #endif
74
75 //
76 // OS stat
77 //
78 ICE_API int stat(const std::string&, structstat*);
79 ICE_API int remove(const std::string&);
80 ICE_API int rename(const std::string&, const std::string&);
81 ICE_API int rmdir(const std::string&);
82
83 ICE_API int mkdir(const std::string&, int);
84 ICE_API FILE* fopen(const std::string&, const std::string&);
85 ICE_API FILE* freopen(const std::string&, const std::string&, FILE*);
86 ICE_API int open(const std::string&, int);
87
88 #ifndef ICE_OS_UWP
89 ICE_API int getcwd(std::string&);
90 #endif
91
92 ICE_API int unlink(const std::string&);
93 ICE_API int close(int);
94
95 //
96 // This class is used to implement process file locking. This class
97 // is not intended to do file locking within the same process.
98 //
99 class ICE_API FileLock : public IceUtil::Shared, public IceUtil::noncopyable
100 {
101 public:
102 //
103 // The constructor opens the given file (eventually creating it)
104 // and acquires a lock on the file or throws FileLockException if
105 // the file couldn't be locked.
106 //
107 // If the lock can be acquired, the process pid is written to the
108 // file.
109 //
110 FileLock(const std::string&);
111
112 //
113 // The destructor releases the lock and removes the file.
114 //
115 virtual ~FileLock();
116
117 private:
118
119 #ifdef _WIN32
120 HANDLE _fd;
121 #else
122 int _fd;
123 #endif
124 std::string _path;
125 };
126
127 typedef IceUtil::Handle<FileLock> FileLockPtr;
128
129 //
130 // Use streamFilename to construct the filename given to std stream classes
131 // like ifstream and ofstream.
132 //
133 #if defined(_WIN32) && !defined(__MINGW32__)
134 ICE_API std::wstring streamFilename(const std::string&);
135 #else
streamFilename(const std::string & filename)136 inline std::string streamFilename(const std::string& filename)
137 {
138 return filename;
139 }
140 #endif
141
142 }
143 #endif
144