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 kumpf 1.51 /**
190 Renames a file. If the new name refers to an existing file, it is
191 removed and replaced with the renamed file. The rename operation is
192 performed atomically.
193 @param oldPath A String containing the name of the file to rename.
194 @param newPath A String containing the name to which to rename the file.
195 @return A Boolean indicating whether the rename operation was
196 successful.
|
197 mike 1.15 */
198 static Boolean renameFile(
|
199 kumpf 1.42 const String& oldPath,
200 const String& newPath);
|
201 mike 1.15
|
202 mike 1.17 /** Same as rename file except that the case of the file referred to
|
203 kumpf 1.51 by oldPath is ignored. The case resolution of the oldPath is
204 performed prior to the atomic rename operation.
|
205 mike 1.17 */
206 static Boolean renameFileNoCase(
|
207 kumpf 1.42 const String& oldPath,
208 const String& newPath);
|
209 mike 1.17
210 /** Copy a file.
|
211 kumpf 1.42 @param fromPath name of existing file.
212 @param toPath name of new file.
213 @return true on success.
|
214 mike 1.17 */
215 static Boolean copyFile(
|
216 kumpf 1.42 const String& fromPath,
217 const String& toPath);
|
218 mike 1.17
|
219 mike 1.15 /** Opens a file and ignores the case of the file. Note that the file
|
220 kumpf 1.42 will be opend in binary mode (no translation of carriage-return-line-
221 feed sequences on Windows).
222 @param os file stream to be opend.
223 @param path path of file to be opened.
224 @return true on success.
|
225 mike 1.15 */
|
226 mike 1.17 static Boolean openNoCase(PEGASUS_STD(ifstream)& is, const String& path);
227
228 /** Opens a file and ignores the case of the file. Note that the file
|
229 kumpf 1.42 open mode of the file must be passed in.
230 @param os file stream to be opend.
231 @param path path of file to be opened.
232 @param mode mode to open the file in.
233 @return true on success.
|
234 mike 1.17 */
235 static Boolean openNoCase(
|
236 kumpf 1.43 PEGASUS_STD(fstream)& fs,
237 const String& path,
|
238 kumpf 1.42 int mode);
|
239 mike 1.15
240 /** Determines whether the path refers to a directory.
|
241 kumpf 1.42 @param path path of the directory.
242 @return true if path refers to a directory.
|
243 mike 1.15 */
244 static Boolean isDirectory(const String& path);
245
246 /** Changes the current directory.
|
247 kumpf 1.42 @param path path of directory to be changed to.
248 @return true on success.
|
249 mike 1.15 */
250 static Boolean changeDirectory(const String& path);
251
|
252 mike 1.16 /** Creates a directory.
|
253 kumpf 1.42 @param path path of directory to be created.
254 @return true on success.
|
255 mike 1.15 */
256 static Boolean makeDirectory(const String& path);
257
|
258 mike 1.16 /** Get the path of the current working Directory.
|
259 kumpf 1.42 @param path set to current working directory upon return.
260 @return true on success (operation may fail if the current
261 working directory becomes stale; this can happen on
262 Unix if it is removed but is impossible on Windows
263 due to reference counting).
|
264 mike 1.15 */
265 static Boolean getCurrentDirectory(String& path);
266
267 /** Remove the given directory. The directory must be empty
|
268 kumpf 1.42 to be eligible for removal
269 @param String path is the relative or ablsolute path to
270 the directory to remove
271 @return true if directory removed
|
272 mike 1.15 */
273 static Boolean removeDirectory(const String& path);
274
275 /** Remove a directory and all files and directories under it.
|
276 kumpf 1.42 WARNING: This differs significantly from the <tt>removeDirectory</tt>
277 function in that it removes both directories and files and
278 removes a complete hiearchy. Use with caution.
279 @param path path of directory to be removed.
280 @return true on success.
|
281 mike 1.16 */
|
282 mike 1.15 static Boolean removeDirectoryHier(const String& path);
283
284 /** Gets names of all entries (files and directories) of a directory.
|
285 kumpf 1.42 Note that this function excludes the "." and ".." entries.
286 @param path path path of directory.
287 @param paths contains list of entry names upon return. Note that
288 the entry names only are provided (no path part).
289 @return true on success.
|
290 mike 1.15 */
291 static Boolean getDirectoryContents(
|
292 kumpf 1.42 const String& path,
293 Array<String>& paths);
|
294 mike 1.15
295 /** Determines whether the given directory is empty. A directory is
|
296 kumpf 1.42 empty if it contains no files or directories.
297 @param path path of directory.
298 @return true if directory is empty.
|
299 mike 1.15 */
300 static Boolean isDirectoryEmpty(const String& path);
301
|
302 mike 1.16 /** Translate backward slashes to forward slashes.
|
303 kumpf 1.42 @param path to be translated.
|
304 mike 1.15 */
305 static void translateSlashes(String& path);
306
|
307 kumpf 1.20 /** Get an absolute path from an absolute directory and a relative or
308 absolute file name. If the file name is fully specified, it is
309 returned unchanged. Otherwise, the specified directory is prepended
310 to the file name.
311 */
312 static String getAbsolutePath(const char* path, const String& filename);
313
|
314 tony 1.25 /** Return the just the filename to the file name into base.
315 */
316 static String extractFileName(const String& base);
317
318 /** Return the just the path to the file name into path.
319 */
320 static String extractFilePath(const String& path);
321
|
322 kumpf 1.31 /** Changes file permissions on the given file.
|
323 kumpf 1.42 @param path path of the file.
324 @param mode the bit-wise inclusive OR of the values for the
|
325 kumpf 1.31 desired permissions.
|
326 kumpf 1.42 @return true on success, false on error and errno is set appropriately.
|
327 kumpf 1.31 */
328 static Boolean changeFilePermissions(const String& path, mode_t mode);
329
|
330 konrad.r 1.32 /**
331 Return OS path specific delimiter.
|
332 kumpf 1.43
|
333 konrad.r 1.32 @return delimiter specific to the platform
334 */
335 static String getPathDelimiter();
336
337 /**
338 Returns the absolute pathname for the specified filename.
|
339 kumpf 1.43
|
340 konrad.r 1.32 @param paths directories seperated by an OS specific delimiter to search
341 @param filename filename to search for in the paths
342
343 @return the full absolute pathname to the found filename or an empty
344 string on failure.
345 */
|
346 kumpf 1.42 static String getAbsoluteFileName(
347 const String& paths,
348 const String& filename);
|
349 kumpf 1.43
|
350 kumpf 1.33 /**
351 Convert a library name to its corresponding file name by adding the
352 appropriate prefix and suffix.
353
354 @param libraryName The name of the library for which to build the file
355 name.
356 @return The file name corresponding to the specified library name.
357 */
358 static String buildLibraryFileName(const String &libraryName);
359
|
360 b.whiteley 1.50 /**
361 Returns the platform-specific file name extension for dynamic
362 libraries.
363
364 @return the platform-specific file name extension for dynamic
365 libraries.
366 */
367 static String getDynamicLibraryExtension();
368
|
369 kumpf 1.42 static Boolean changeFileOwner(
370 const String& fileName,
371 const String& userName);
372
|
373 mateus.baur 1.47 /**
374 Flushes the data from the iostream buffers to the OS buffers and
375 then flushes the data from the OS buffers to the disk.
376
377 This will avoid the possible data loss in case of an OS crash when
378 OS filesystem commit directory-level changes immediately while
379 file-level changes remain cached (e.g. HP-UX).
380
381 @param fstream. The iostream that we want to flush data.
382 */
383 static void syncWithDirectoryUpdates(PEGASUS_STD(fstream)&);
384
|
385 mike 1.15 private:
386
387 FileSystem() { }
388 };
389
390 inline Boolean FileSystem::existsNoCase(const String& path)
391 {
392 String dummy;
393 return existsNoCase(path, dummy);
394 }
395
396 inline Boolean FileSystem::canReadNoCase(const String& path)
397 {
398 String realPath;
399
400 if (!existsNoCase(path, realPath))
|
401 kumpf 1.42 return false;
|
402 mike 1.15
403 return FileSystem::canRead(realPath);
404 }
405
406 inline Boolean FileSystem::canWriteNoCase(const String& path)
407 {
408 String realPath;
409
410 if (!existsNoCase(path, realPath))
|
411 kumpf 1.42 return false;
|
412 mike 1.15
413 return FileSystem::canWrite(realPath);
414 }
415
416 inline Boolean FileSystem::removeFileNoCase(const String& path)
417 {
418 String realPath;
419
420 if (!existsNoCase(path, realPath))
|
421 kumpf 1.42 return false;
|
422 mike 1.15
423 return FileSystem::removeFile(realPath);
|
424 mike 1.17 }
425
426 inline Boolean FileSystem::renameFileNoCase(
427 const String& oldPath,
428 const String& newPath)
429 {
430 String realPath;
431
432 if (!existsNoCase(oldPath, realPath))
|
433 kumpf 1.42 return false;
|
434 mike 1.17
435 return FileSystem::renameFile(realPath, newPath);
|
436 mike 1.15 }
437
438 inline Boolean FileSystem::getFileSizeNoCase(const String& path, Uint32& size)
439 {
440 String realPath;
441
442 if (!existsNoCase(path, realPath))
|
443 kumpf 1.42 return false;
|
444 mike 1.15
445 return FileSystem::getFileSize(realPath, size);
|
446 kumpf 1.20 }
447
448 inline String FileSystem::getAbsolutePath(
449 const char* path,
450 const String& filename)
451 {
452 String absolutePath;
453
454 if (filename != String::EMPTY)
455 {
|
456 kumpf 1.26 if (!System::is_absolute_path(filename.getCString()) && path && path[0])
|
457 kumpf 1.20 {
458 absolutePath.append(path);
459 absolutePath.append('/');
460 }
461 absolutePath.append(filename);
462 }
463 translateSlashes(absolutePath);
464
465 return absolutePath;
|
466 mike 1.15 }
467
|
468 kumpf 1.18 inline Boolean Open(PEGASUS_STD(ifstream)& is, const String& path)
469 {
|
470 kumpf 1.24 is.open(path.getCString());
|
471 kumpf 1.18 return !!is;
472 }
473
474 inline Boolean Open(PEGASUS_STD(ofstream)& os, const String& path)
475 {
|
476 kumpf 1.24 os.open(path.getCString());
|
477 kumpf 1.18 return !!os;
478 }
479
480 inline Boolean OpenAppend(PEGASUS_STD(ofstream)& os, const String& path)
481 {
|
482 kumpf 1.24 os.open(path.getCString(), PEGASUS_STD(ios::app));
|
483 kumpf 1.18 return !!os;
|
484 konrad.r 1.32 }
485
|
486 kumpf 1.43 inline String FileSystem::getPathDelimiter()
|
487 konrad.r 1.32 {
|
488 david.dillard 1.38 #if defined(PEGASUS_OS_TYPE_WINDOWS)
|
489 kumpf 1.42 return String(";");
|
490 konrad.r 1.32 #else
|
491 kumpf 1.42 return String(":");
|
492 konrad.r 1.32 #endif
|
493 kumpf 1.18 }
494
495 /** Get the next line from the input file.
496 */
497 PEGASUS_COMMON_LINKAGE Boolean GetLine(PEGASUS_STD(istream)& is, String& line);
498
|
499 mike 1.15 PEGASUS_NAMESPACE_END
500
501 #endif /* Pegasus_FileSystem_h */
|