1 mike 1.14 //%/////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001 The Open group, BMC Software, Tivoli Systems, IBM
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to
7 // deal in the Software without restriction, including without limitation the
8 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 // sell copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
13 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
14 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 //
21 //==============================================================================
22 mike 1.14 //
23 // Author: Mike Brasher (mbrasher@bmc.com)
24 //
25 // Modified By:
26 //
27 //%/////////////////////////////////////////////////////////////////////////////
28
29 #ifndef Pegasus_FileSystem_h
30 #define Pegasus_FileSystem_h
31
32 #include <fstream>
33 #include <Pegasus/Common/Config.h>
34 #include <Pegasus/Common/String.h>
35 #include <Pegasus/Common/Array.h>
36 #include <Pegasus/Common/Exception.h>
37
38 PEGASUS_NAMESPACE_BEGIN
39
40 /** The FileSystem class provides methods for manipulating the file system.
41
42 This class provides an methods for:
43 mike 1.14 <ul>
44 <li>Manipulating directories (create, remove, change).</li>
45 <li>Checking files for ability to read and write.</li>
46 <li>Removing files.</li>
47 <li>Comparing files.</li>
48 <li>Loading files into memory.</li>
49 </ul>
50
51 The methods of this class are all static. So there is no need to
52 instantiate this class to use it. In fact, instantiation is precluded
53 by a private default constructor.
54
55 A word about the "NoCase" extensions. Some methods of this class have
56 a "NoCase" version. For example, there is a canRead() and a canReadNoCase().
57 The "NoCase" variation ignores the case of the file while it is being
58 located. For example, suppose there is a file called "File1". Then
59 canReadNoCase("file1") finds the file called "File1" and returns true (of
60 course there is a possibility that there really is a file called "file1"
61 in the same directory in which case the behavior of this method is
62 undefined). Notice that Windows does this anyway. These methods were
63 developed primarily for Unix which is case sensitive with respect to file
64 mike 1.14 names. It should be noted that the no-case methods are slower (since they
65 must stat the directory and look at the file names).
66
67 The no-case variations are used by the repository which--according to
68 CIM--must treat two classes names with different case characterisits, but
69 othererwise similar, as identical. For example, "MyClass", "myclass", and
70 "MYCLASS" all refer to the same class. Since the default repository
71 implementation uses disk file names to represent class names (e.g., there
72 may be a file called "MyClass.#) that there must be a way of opening
73 a file without regard to its case.
74 */
75 class PEGASUS_COMMON_LINKAGE FileSystem
76 {
77 public:
78
79 /** Determines whether file exists.
80 @param path path of the file.
81 @return true if the file exists.
82 */
83 static Boolean exists(const String& path);
84
85 mike 1.14 /** Determine whether the file exists. Ignores case of the file.
86 @param path path of the file.
87 @param pathOut path of the file with actual case.
88 @return true if the file exists; false otherwise.
89 */
90 static Boolean existsNoCase(const String& path, String& pathOut);
91
92 /** Determine whether the file exists. Ignores the case of the file.
93 @param path path of the file.
94 @return true if the file exists; false otherwise.
95 */
96 static Boolean existsNoCase(const String& path);
97
98 /** Determines whether the file can be read.
99 @param path path of the file.
100 @return true if the file can be read.
101 */
102 static Boolean canRead(const String& path);
103
104 /** Determines whether the file can be read. Ignores case of file.
105 @param path path of the file.
106 mike 1.14 @return true if the file can be read.
107 */
108 static Boolean canReadNoCase(const String& path);
109
110 /** Determines whether the file can be written.
111 @param path path of the file.
112 @return true if the file can be written.
113 */
114 static Boolean canWrite(const String& path);
115
116 /** Determines whether the file can be written. Ignores case of file.
117 @param path path of the file.
118 @return true if the file can be written.
119 */
120 static Boolean canWriteNoCase(const String& path);
121
122 /** Get the size of the file in bytes.
123 @param path path of file.
124 @param size set to size of file.
125 @return true on success.
126 */
127 mike 1.14 static Boolean getFileSize(const String& path, Uint32& size);
128
129 /** Get the size of the file in bytes.
130 @param path path of file.
131 @param size set to size of file.
132 @return true on success.
133 */
134 static Boolean getFileSizeNoCase(const String& path, Uint32& size);
135
136 /** Removes a file.
137 @param path of file to be removed.
138 @return true on sucess.
139 */
140 static Boolean removeFile(const String& path);
141
142 /** Removes a file. Ignores case of file.
143 @param path of file to be removed.
144 @return true on sucess.
145 */
146 static Boolean removeFileNoCase(const String& path);
147
148 mike 1.14 /** Loads contents of the file into the array. Note that the file is
149 opened using binary mode (newline sequences are not expanded to
150 carriage-return-line-feed sequences on Windows).
151 @param array set to the contents of the file upon return.
152 @param fileName name of file to be loaded.
153 @exception CannotOpenFile
154 */
155 static void loadFileToMemory(
156 Array<Sint8>& array,
157 const String& fileName);
158
159 /** Determines whether two files have exactly the same content.
160 @param path1 path of first file.
161 @param path2 path of second file.
162 @return true if files are identical.
163 @exception CannotOpenFile
164 */
165 static Boolean compareFiles(
166 const String& path1,
167 const String& path2);
168
169 mike 1.14 /** Renames a file.
170 @param oldPath old name of file.
171 @param newPath new name of file.
172 @return true on success.
173 */
174 static Boolean renameFile(
175 const String& oldPath,
176 const String& newPath);
177
178 /** Opens a file and ignores the case of the file. Note that the file
179 will be opend in binary mode (no translation of carriage-return-line-
180 feed sequences on Windows).
181 @param os file stream to be opend.
182 @param path path of file to be opened.
183 @return true on success.
184 */
185 Boolean openNoCase(PEGASUS_STD(ifstream)& is, const String& path);
186
187 /** Determines whether the path refers to a directory.
188 @param path path of the directory.
189 @return true if path refers to a directory.
190 mike 1.14 */
191 static Boolean isDirectory(const String& path);
192
193 /** Changes the current directory.
194 @param path path of directory to be changed to.
195 @return true on success.
196 */
197 static Boolean changeDirectory(const String& path);
198
199 /** Creates a directory.
200 @param path path of directory to be created.
201 @return true on success.
202 */
203 static Boolean makeDirectory(const String& path);
204
205 /** Get the path of the current working Directory.
206 @param path set to current working directory upon return.
207 @return true on success (operation may fail if the current
208 working directory becomes stale; this can happen on
209 Unix if it is removed but is impossible on Windows
210 due to reference counting).
211 mike 1.14 */
212 static Boolean getCurrentDirectory(String& path);
213
214 /** Remove the given directory. The directory must be empty
215 to be eligible for removal
216 @param String path is the relative or ablsolute path to
217 the directory to remove
218 @return true if directory removed
219 */
220 static Boolean removeDirectory(const String& path);
221
222 /** Remove a directory and all files and directories under it.
223 WARNING: This differs significantly from the <tt>removeDirectory</tt>
224 function in that it removes both directories and files and
225 removes a complete hiearchy. Use with caution.
226 @param path path of directory to be removed.
227 @return true on success.
228 */
229 static Boolean removeDirectoryHier(const String& path);
230
231 /** Gets names of all entries (files and directories) of a directory.
232 mike 1.14 Note that this function excludes the "." and ".." entries.
233 @param path path path of directory.
234 @param paths contains list of entry names upon return. Note that
235 the entry names only are provided (no path part).
236 @return true on success.
237 */
238 static Boolean getDirectoryContents(
239 const String& path,
240 Array<String>& paths);
241
242 /** Determines whether the given directory is empty. A directory is
243 empty if it contains no files or directories.
244 @param path path of directory.
245 @return true if directory is empty.
246 */
247 static Boolean isDirectoryEmpty(const String& path);
248
249 /** Translate backward slashes to forward slashes.
250 @param path to be translated.
251 */
252 static void translateSlashes(String& path);
253 mike 1.14
254 private:
255
256 FileSystem() { }
257 };
258
259 inline Boolean FileSystem::existsNoCase(const String& path)
260 {
261 String dummy;
262 return existsNoCase(path, dummy);
263 }
264
265 inline Boolean FileSystem::canReadNoCase(const String& path)
266 {
267 String realPath;
268
269 if (!existsNoCase(path, realPath))
270 return false;
271
272 return FileSystem::canRead(realPath);
273 }
274 mike 1.14
275 inline Boolean FileSystem::canWriteNoCase(const String& path)
276 {
277 String realPath;
278
279 if (!existsNoCase(path, realPath))
280 return false;
281
282 return FileSystem::canWrite(realPath);
283 }
284
285 inline Boolean FileSystem::removeFileNoCase(const String& path)
286 {
287 String realPath;
288
289 if (!existsNoCase(path, realPath))
290 return false;
291
292 return FileSystem::removeFile(realPath);
293 }
294
295 mike 1.14 inline Boolean FileSystem::getFileSizeNoCase(const String& path, Uint32& size)
296 {
297 String realPath;
298
299 if (!existsNoCase(path, realPath))
300 return false;
301
302 return FileSystem::getFileSize(realPath, size);
303 }
304
305 PEGASUS_NAMESPACE_END
306
307 #endif /* Pegasus_FileSystem_h */
|