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