1 mike 1.1 //BEGIN_LICENSE
2 //
3 // Copyright (c) 2000 The Open Group, BMC Software, Tivoli Systems, IBM
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18 // DEALINGS IN THE SOFTWARE.
19 //
20 //END_LICENSE
21 //BEGIN_HISTORY
22 mike 1.1 //
23 // Author:
24 //
|
25 mike 1.2 // $Log: FileSystem.cpp,v $
|
26 mike 1.14 // Revision 1.13 2001/04/08 21:57:13 karl
27 // dir hier tested
28 //
|
29 karl 1.13 // Revision 1.12 2001/04/08 20:31:55 mike
30 // Fixed
31 //
|
32 mike 1.12 // Revision 1.11 2001/04/08 20:29:41 mike
33 // Added std:: before cout and endl
34 //
|
35 mike 1.11 // Revision 1.10 2001/04/08 20:28:27 karl
36 // test
37 //
|
38 karl 1.10 // Revision 1.9 2001/04/08 19:56:38 karl
39 // Test version
40 //
|
41 karl 1.9 // Revision 1.8 2001/04/08 19:20:04 mike
42 // more TCP work
43 //
|
44 mike 1.8 // Revision 1.7 2001/04/08 01:13:21 mike
45 // Changed "ConstCIM" to "CIMConst"
46 //
|
47 karl 1.6 // Revision 1.5 2001/03/11 23:35:32 mike
48 // Ports to Linux
49 //
|
50 mike 1.4 // Revision 1.3 2001/02/13 02:06:40 mike
51 // Added renameFile() method.
52 //
|
53 mike 1.3 // Revision 1.2 2001/02/11 05:42:33 mike
54 // new
55 //
|
56 mike 1.2 // Revision 1.1.1.1 2001/01/14 19:51:35 mike
57 // Pegasus import
58 //
|
59 mike 1.1 //
60 //END_HISTORY
61
|
62 mike 1.14 #include <iostream>
|
63 mike 1.15 #include <cstdio>
|
64 mike 1.1 #include <Pegasus/Common/Config.h>
|
65 mike 1.14 #include <Pegasus/Common/System.h>
|
66 mike 1.1 #include "Destroyer.h"
67 #include "FileSystem.h"
|
68 mike 1.14 #include "System.h"
|
69 mike 1.2 #include "Dir.h"
|
70 mike 1.12
71 using namespace std;
|
72 mike 1.1
|
73 mike 1.14 PEGASUS_NAMESPACE_BEGIN
|
74 mike 1.1
|
75 mike 1.14 // Clone the path as a C String but discard trailing slash if any:
|
76 mike 1.1
77 static char* _clonePath(const String& path)
78 {
79 char* p = path.allocateCString();
80
81 if (!*p)
82 return p;
83
84 char* last = p + path.getLength() - 1;
85
86 if (*last == '/')
87 *last = '\0';
88
89 return p;
90 }
91
92 Boolean FileSystem::exists(const String& path)
93 {
|
94 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
95 mike 1.14 return System::exists(p.getPointer());
|
96 mike 1.1 }
97
|
98 karl 1.6 Boolean FileSystem::getCurrentDirectory(String& path)
99 {
|
100 mike 1.14 path.clear();
101 char tmp[4096];
|
102 karl 1.6
|
103 mike 1.14 if (!System::getCurrentDirectory(tmp, sizeof(tmp) - 1))
|
104 karl 1.6 return false;
105
106 path.append(tmp);
107 return true;
108 }
109
|
110 mike 1.2 Boolean FileSystem::existsIgnoreCase(const String& path, String& realPath)
111 {
112 realPath.clear();
|
113 mike 1.5 ArrayDestroyer<char> destroyer(_clonePath(path));
|
114 mike 1.2 char* p = destroyer.getPointer();
115
116 char* dirPath;
117 char* fileName;
118 char* slash = strrchr(p, '/');
119
120 if (slash)
121 {
122 *slash = '\0';
123 fileName = slash + 1;
124 dirPath = p;
|
125 mike 1.14
|
126 mike 1.2 if (*fileName == '\0')
127 return false;
128 }
129 else
130 {
131 fileName = p;
132 dirPath = ".";
133 }
134
135 for (Dir dir(dirPath); dir.more(); dir.next())
136 {
|
137 mike 1.14 if (CompareIgnoreCase(fileName, dir.getName()) == 0)
|
138 mike 1.2 {
139 if (strcmp(dirPath, ".") == 0)
140 realPath = dir.getName();
141 else
142 {
143 realPath = dirPath;
144 realPath += '/';
145 realPath += dir.getName();
146 }
147 return true;
148 }
149 }
150
151 return false;
152 }
153
|
154 mike 1.1 Boolean FileSystem::canRead(const String& path)
155 {
|
156 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
157 mike 1.14 return System::canRead(p.getPointer());
|
158 mike 1.1 }
159
160 Boolean FileSystem::canWrite(const String& path)
161 {
|
162 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
163 mike 1.14 return System::canWrite(p.getPointer());
|
164 mike 1.1 }
165
166 Boolean FileSystem::isDirectory(const String& path)
167 {
|
168 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
169 mike 1.14 return System::isDirectory(p.getPointer());
|
170 mike 1.1 }
171
172 Boolean FileSystem::changeDirectory(const String& path)
173 {
|
174 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
175 mike 1.14 return System::changeDirectory(p.getPointer());
|
176 mike 1.1 }
177
178 Boolean FileSystem::makeDirectory(const String& path)
179 {
|
180 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
181 mike 1.14 return System::makeDirectory(p.getPointer());
|
182 mike 1.1 }
183
184 Boolean FileSystem::getFileSize(const String& path, Uint32& size)
185 {
|
186 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
187 mike 1.14 return System::getFileSize(p.getPointer(), size);
|
188 mike 1.1 }
189
190 Boolean FileSystem::removeDirectory(const String& path)
191 {
|
192 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
193 mike 1.14 return System::removeDirectory(p.getPointer());
|
194 mike 1.1 }
195
|
196 karl 1.6 Boolean FileSystem::removeDirectoryHier(const String& path)
197 {
|
198 mike 1.7 Array<String> fileList;
|
199 karl 1.6
|
200 karl 1.13 // Get contents of current directory
|
201 mike 1.14
|
202 karl 1.6 if (!FileSystem::getDirectoryContents(path,fileList))
203 return false;
204
205 // for files-in-directory, delete or recall removedir
|
206 mike 1.14
|
207 karl 1.6 for (Uint32 i = 0, n = fileList.getSize(); i < n; i++)
|
208 karl 1.10 {
|
209 karl 1.13 String newPath = path; // extend path to subdir
210 newPath.append("/");
211 newPath.append(fileList[i]);
|
212 karl 1.10
|
213 karl 1.13 if (FileSystem::isDirectory(newPath))
214 {
215 // Recall ourselves with extended path
216 if (!FileSystem::removeDirectoryHier(newPath))
217 return false;
|
218 karl 1.6 }
219
|
220 karl 1.13 else
221 {
222 if (!FileSystem::removeFile(newPath))
223 return false;
|
224 karl 1.6 }
225 }
|
226 mike 1.8
|
227 karl 1.6 return removeDirectory(path);
228 }
229
|
230 mike 1.1 Boolean FileSystem::removeFile(const String& path)
231 {
|
232 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
233 mike 1.15 return System::removeFile(p.getPointer());
|
234 mike 1.1 }
235
236 void FileSystem::loadFileToMemory(
237 Array<Sint8>& array,
238 const String& fileName)
239 {
240 Uint32 fileSize;
241
242 if (!getFileSize(fileName, fileSize))
243 throw CannotOpenFile(fileName);
244
245 char* tmp = fileName.allocateCString();
246 FILE* fp = fopen(tmp, "rb");
247 delete [] tmp;
248
249 if (fp == NULL)
250 throw CannotOpenFile(fileName);
251
252 array.reserve(fileSize);
253 char buffer[4096];
254 size_t n;
255 mike 1.1
256 while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0)
257 array.append(buffer, n);
258
259 fclose(fp);
260 }
261
262 Boolean FileSystem::compare(
263 const String& fileName1,
264 const String& fileName2)
265 {
266 Uint32 fileSize1;
267
268 if (!getFileSize(fileName1, fileSize1))
269 throw CannotOpenFile(fileName1);
270
271 Uint32 fileSize2;
272
273 if (!getFileSize(fileName2, fileSize2))
274 throw CannotOpenFile(fileName2);
275
276 mike 1.1 if (fileSize1 != fileSize2)
277 return false;
278
279 char* tmp1 = fileName1.allocateCString();
280 FILE* fp1 = fopen(tmp1, "rb");
281 delete [] tmp1;
282
283 if (fp1 == NULL)
284 throw CannotOpenFile(fileName1);
285
286 char* tmp2 = fileName2.allocateCString();
287 FILE* fp2 = fopen(tmp2, "rb");
288 delete [] tmp2;
289
290 if (fp2 == NULL)
291 {
292 fclose(fp1);
293 throw CannotOpenFile(fileName2);
294 }
295
296 int c1;
297 mike 1.1 int c2;
298
299 while ((c1 = fgetc(fp1)) != EOF && (c2 = fgetc(fp2)) != EOF)
300 {
301 if (c1 != c2)
302 {
303 fclose(fp1);
304 fclose(fp2);
305 return false;
306 }
307 }
308
309 fclose(fp1);
310 fclose(fp2);
311 return true;
312 }
313
|
314 mike 1.14 //
315 // Get the file list in the directory into the
316 // array of strings provided
317 // @return The function should return false under these circumstances:
318 //
319 //
320 // 1. The directory does not exist.
321 // 2. The file exists but is not a directory.
322 // 3. The directory is inaccessible.
323 //
324 //
|
325 mike 1.1 Boolean FileSystem::getDirectoryContents(
326 const String& path,
327 Array<String>& paths)
328 {
|
329 karl 1.6 // This may be just extra fluff but added anyway
330 if (!FileSystem::isDirectory(path))
331 return false;
332
|
333 mike 1.1 paths.clear();
|
334 mike 1.14
|
335 karl 1.6 try
336 {
337 for (Dir dir(path); dir.more(); dir.next())
338 {
339 String name = dir.getName();
|
340 mike 1.14
|
341 karl 1.6 if (String::equal(name, ".") || String::equal(name, ".."))
342 continue;
|
343 mike 1.14
|
344 karl 1.6 paths.append(name);
345 }
346 return true;
347 }
|
348 mike 1.1
|
349 karl 1.6 // Catch the Dir exception
350 catch(CannotOpenDirectory&)
|
351 mike 1.1 {
|
352 karl 1.6 return false;
|
353 mike 1.1 }
354
|
355 mike 1.3 }
356
357 Boolean FileSystem::renameFile(
358 const String& oldFileName,
359 const String& newFileName)
360 {
|
361 mike 1.5 ArrayDestroyer<char> p(oldFileName.allocateCString());
362 ArrayDestroyer<char> q(newFileName.allocateCString());
|
363 mike 1.14 return System::renameFile(p.getPointer(), q.getPointer());
|
364 mike 1.1 }
365
366 PEGASUS_NAMESPACE_END
|