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 kumpf 1.23 #include <Pegasus/Common/ArrayInternal.h>
|
36 kumpf 1.22 #include <Pegasus/Common/InternalException.h>
|
37 kumpf 1.26 #include <Pegasus/Common/System.h>
|
38 kumpf 1.21 #include <Pegasus/Common/Linkage.h>
|
39 mike 1.16 #include <fstream>
|
40 david 1.27 #include <cstdio>
41 #if defined(PEGASUS_OS_OS400)
42 #include "OS400ConvertChar.h"
43 #endif
|
44 mike 1.15
45 PEGASUS_NAMESPACE_BEGIN
46
47 /** The FileSystem class provides methods for manipulating the file system.
48
49 This class provides an methods for:
50 <ul>
51 <li>Manipulating directories (create, remove, change).</li>
52 <li>Checking files for ability to read and write.</li>
53 <li>Removing files.</li>
54 <li>Comparing files.</li>
55 <li>Loading files into memory.</li>
56 </ul>
57
|
58 mike 1.16 The methods of this class are all static. So there is no need to
|
59 mike 1.15 instantiate this class to use it. In fact, instantiation is precluded
60 by a private default constructor.
61
62 A word about the "NoCase" extensions. Some methods of this class have
63 a "NoCase" version. For example, there is a canRead() and a canReadNoCase().
64 The "NoCase" variation ignores the case of the file while it is being
65 located. For example, suppose there is a file called "File1". Then
66 canReadNoCase("file1") finds the file called "File1" and returns true (of
67 course there is a possibility that there really is a file called "file1"
|
68 mike 1.16 in the same directory in which case the behavior of this method is
69 undefined). Notice that Windows does this anyway. These methods were
|
70 mike 1.15 developed primarily for Unix which is case sensitive with respect to file
71 names. It should be noted that the no-case methods are slower (since they
72 must stat the directory and look at the file names).
73
74 The no-case variations are used by the repository which--according to
|
75 mike 1.16 CIM--must treat two classes names with different case characterisits, but
|
76 mike 1.15 othererwise similar, as identical. For example, "MyClass", "myclass", and
77 "MYCLASS" all refer to the same class. Since the default repository
78 implementation uses disk file names to represent class names (e.g., there
79 may be a file called "MyClass.#) that there must be a way of opening
80 a file without regard to its case.
81 */
82 class PEGASUS_COMMON_LINKAGE FileSystem
83 {
84 public:
85
86 /** Determines whether file exists.
87 @param path path of the file.
88 @return true if the file exists.
89 */
90 static Boolean exists(const String& path);
91
92 /** Determine whether the file exists. Ignores case of the file.
93 @param path path of the file.
94 @param pathOut path of the file with actual case.
95 @return true if the file exists; false otherwise.
96 */
97 mike 1.15 static Boolean existsNoCase(const String& path, String& pathOut);
98
99 /** Determine whether the file exists. Ignores the case of the file.
100 @param path path of the file.
101 @return true if the file exists; false otherwise.
102 */
103 static Boolean existsNoCase(const String& path);
104
105 /** Determines whether the file can be read.
106 @param path path of the file.
107 @return true if the file can be read.
108 */
109 static Boolean canRead(const String& path);
110
111 /** Determines whether the file can be read. Ignores case of file.
112 @param path path of the file.
113 @return true if the file can be read.
114 */
115 static Boolean canReadNoCase(const String& path);
116
117 /** Determines whether the file can be written.
118 mike 1.15 @param path path of the file.
119 @return true if the file can be written.
120 */
121 static Boolean canWrite(const String& path);
122
123 /** Determines whether the file can be written. Ignores case of file.
124 @param path path of the file.
125 @return true if the file can be written.
126 */
127 static Boolean canWriteNoCase(const String& path);
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 getFileSize(const String& path, Uint32& size);
135
136 /** Get the size of the file in bytes.
137 @param path path of file.
138 @param size set to size of file.
139 mike 1.15 @return true on success.
140 */
141 static Boolean getFileSizeNoCase(const String& path, Uint32& size);
142
143 /** Removes a file.
144 @param path of file to be removed.
145 @return true on sucess.
146 */
147 static Boolean removeFile(const String& path);
148
149 /** Removes a file. Ignores case of file.
150 @param path of file to be removed.
151 @return true on sucess.
152 */
153 static Boolean removeFileNoCase(const String& path);
154
155 /** Loads contents of the file into the array. Note that the file is
156 opened using binary mode (newline sequences are not expanded to
157 carriage-return-line-feed sequences on Windows).
158 @param array set to the contents of the file upon return.
159 @param fileName name of file to be loaded.
|
160 mike 1.16 @exception CannotOpenFile
|
161 mike 1.15 */
162 static void loadFileToMemory(
163 Array<Sint8>& array,
164 const String& fileName);
165
166 /** Determines whether two files have exactly the same content.
167 @param path1 path of first file.
168 @param path2 path of second file.
169 @return true if files are identical.
170 @exception CannotOpenFile
171 */
172 static Boolean compareFiles(
173 const String& path1,
174 const String& path2);
175
176 /** Renames a file.
177 @param oldPath old name of file.
178 @param newPath new name of file.
179 @return true on success.
180 */
181 static Boolean renameFile(
182 mike 1.15 const String& oldPath,
183 const String& newPath);
184
|
185 mike 1.17 /** Same as rename file except that the case of the file referred to
186 by oldPath is ignored.
187 */
188 static Boolean renameFileNoCase(
189 const String& oldPath,
190 const String& newPath);
191
192 /** Copy a file.
193 @param fromPath name of existing file.
194 @param toPath name of new file.
195 @return true on success.
196 */
197 static Boolean copyFile(
198 const String& fromPath,
199 const String& toPath);
200
|
201 mike 1.15 /** Opens a file and ignores the case of the file. Note that the file
202 will be opend in binary mode (no translation of carriage-return-line-
203 feed sequences on Windows).
204 @param os file stream to be opend.
205 @param path path of file to be opened.
206 @return true on success.
207 */
|
208 mike 1.17 static Boolean openNoCase(PEGASUS_STD(ifstream)& is, const String& path);
209
210 /** Opens a file and ignores the case of the file. Note that the file
211 open mode of the file must be passed in.
212 @param os file stream to be opend.
213 @param path path of file to be opened.
214 @param mode mode to open the file in.
215 @return true on success.
216 */
217 static Boolean openNoCase(
218 PEGASUS_STD(fstream)& fs,
219 const String& path,
220 int mode);
|
221 mike 1.15
222 /** Determines whether the path refers to a directory.
223 @param path path of the directory.
224 @return true if path refers to a directory.
225 */
226 static Boolean isDirectory(const String& path);
227
228 /** Changes the current directory.
229 @param path path of directory to be changed to.
230 @return true on success.
231 */
232 static Boolean changeDirectory(const String& path);
233
|
234 mike 1.16 /** Creates a directory.
|
235 mike 1.15 @param path path of directory to be created.
236 @return true on success.
237 */
238 static Boolean makeDirectory(const String& path);
239
|
240 mike 1.16 /** Get the path of the current working Directory.
|
241 mike 1.15 @param path set to current working directory upon return.
242 @return true on success (operation may fail if the current
243 working directory becomes stale; this can happen on
244 Unix if it is removed but is impossible on Windows
245 due to reference counting).
246 */
247 static Boolean getCurrentDirectory(String& path);
248
249 /** Remove the given directory. The directory must be empty
250 to be eligible for removal
251 @param String path is the relative or ablsolute path to
252 the directory to remove
253 @return true if directory removed
254 */
255 static Boolean removeDirectory(const String& path);
256
257 /** Remove a directory and all files and directories under it.
258 WARNING: This differs significantly from the <tt>removeDirectory</tt>
259 function in that it removes both directories and files and
|
260 mike 1.16 removes a complete hiearchy. Use with caution.
|
261 mike 1.15 @param path path of directory to be removed.
262 @return true on success.
|
263 mike 1.16 */
|
264 mike 1.15 static Boolean removeDirectoryHier(const String& path);
265
266 /** Gets names of all entries (files and directories) of a directory.
267 Note that this function excludes the "." and ".." entries.
268 @param path path path of directory.
269 @param paths contains list of entry names upon return. Note that
270 the entry names only are provided (no path part).
271 @return true on success.
272 */
273 static Boolean getDirectoryContents(
274 const String& path,
275 Array<String>& paths);
276
277 /** Determines whether the given directory is empty. A directory is
278 empty if it contains no files or directories.
279 @param path path of directory.
280 @return true if directory is empty.
281 */
282 static Boolean isDirectoryEmpty(const String& path);
283
|
284 mike 1.16 /** Translate backward slashes to forward slashes.
|
285 mike 1.15 @param path to be translated.
286 */
287 static void translateSlashes(String& path);
288
|
289 kumpf 1.20 /** Get an absolute path from an absolute directory and a relative or
290 absolute file name. If the file name is fully specified, it is
291 returned unchanged. Otherwise, the specified directory is prepended
292 to the file name.
293 */
294 static String getAbsolutePath(const char* path, const String& filename);
295
|
296 tony 1.25 /** Return the just the filename to the file name into base.
297 */
298 static String extractFileName(const String& base);
299
300 /** Return the just the path to the file name into path.
301 */
302 static String extractFilePath(const String& path);
303
|
304 mike 1.15 private:
305
306 FileSystem() { }
307 };
308
309 inline Boolean FileSystem::existsNoCase(const String& path)
310 {
311 String dummy;
312 return existsNoCase(path, dummy);
313 }
314
315 inline Boolean FileSystem::canReadNoCase(const String& path)
316 {
317 String realPath;
318
319 if (!existsNoCase(path, realPath))
320 return false;
321
322 return FileSystem::canRead(realPath);
323 }
324
325 mike 1.15 inline Boolean FileSystem::canWriteNoCase(const String& path)
326 {
327 String realPath;
328
329 if (!existsNoCase(path, realPath))
330 return false;
331
332 return FileSystem::canWrite(realPath);
333 }
334
335 inline Boolean FileSystem::removeFileNoCase(const String& path)
336 {
337 String realPath;
338
339 if (!existsNoCase(path, realPath))
340 return false;
341
342 return FileSystem::removeFile(realPath);
|
343 mike 1.17 }
344
345 inline Boolean FileSystem::renameFileNoCase(
346 const String& oldPath,
347 const String& newPath)
348 {
349 String realPath;
350
351 if (!existsNoCase(oldPath, realPath))
352 return false;
353
354 return FileSystem::renameFile(realPath, newPath);
|
355 mike 1.15 }
356
357 inline Boolean FileSystem::getFileSizeNoCase(const String& path, Uint32& size)
358 {
359 String realPath;
360
361 if (!existsNoCase(path, realPath))
362 return false;
363
364 return FileSystem::getFileSize(realPath, size);
|
365 kumpf 1.20 }
366
367 inline String FileSystem::getAbsolutePath(
368 const char* path,
369 const String& filename)
370 {
371 String absolutePath;
372
373 if (filename != String::EMPTY)
374 {
|
375 kumpf 1.26 if (!System::is_absolute_path(filename.getCString()) && path && path[0])
|
376 kumpf 1.20 {
377 absolutePath.append(path);
378 absolutePath.append('/');
379 }
380 absolutePath.append(filename);
381 }
382 translateSlashes(absolutePath);
383
384 return absolutePath;
|
385 mike 1.15 }
386
|
387 kumpf 1.18 inline Boolean Open(PEGASUS_STD(ifstream)& is, const String& path)
388 {
|
389 david 1.27 #if defined(PEGASUS_OS_OS400) && defined(DAVES_CHANGES)
390 CString tempPath = path.getCString();
391 const char * tmp = tempPath;
392 AtoE((char *)tmp);
393 is.open(tmp, PEGASUS_STD(_CCSID_T(1208)));
394 #else
|
395 kumpf 1.24 is.open(path.getCString());
|
396 david 1.27 #endif
|
397 kumpf 1.18 return !!is;
398 }
399
400 inline Boolean Open(PEGASUS_STD(ofstream)& os, const String& path)
401 {
|
402 david 1.27 #if defined(PEGASUS_OS_OS400) && defined(DAVES_CHANGES)
403 CString tempPath = path.getCString();
404 const char * tmp = tempPath;
405 AtoE((char *)tmp);
406 os.open(tmp, PEGASUS_STD(_CCSID_T(1208)));
407 #else
|
408 kumpf 1.24 os.open(path.getCString());
|
409 david 1.27 #endif
|
410 kumpf 1.18 return !!os;
411 }
412
413 inline Boolean OpenAppend(PEGASUS_STD(ofstream)& os, const String& path)
414 {
|
415 david 1.27 #if defined(PEGASUS_OS_OS400) && defined(DAVES_CHANGES)
416 CString tempPath = path.getCString();
417 const char * tmp = tempPath;
418 AtoE((char *)tmp);
419 os.open(tmp, PEGASUS_STD(ios::app), PEGASUS_STD(_CCSID_T(1208)));
420 #else
|
421 kumpf 1.24 os.open(path.getCString(), PEGASUS_STD(ios::app));
|
422 david 1.27 #endif
|
423 kumpf 1.18 return !!os;
424 }
425
426 /** Get the next line from the input file.
427 */
428 PEGASUS_COMMON_LINKAGE Boolean GetLine(PEGASUS_STD(istream)& is, String& line);
429
|
430 mike 1.15 PEGASUS_NAMESPACE_END
431
432 #endif /* Pegasus_FileSystem_h */
|