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