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.1 #include <Pegasus/Common/Config.h>
|
64 mike 1.14 #include <Pegasus/Common/System.h>
|
65 mike 1.1 #include "Destroyer.h"
66 #include "FileSystem.h"
|
67 mike 1.14 #include "System.h"
|
68 mike 1.2 #include "Dir.h"
|
69 mike 1.12
70 using namespace std;
|
71 mike 1.1
|
72 mike 1.14 PEGASUS_NAMESPACE_BEGIN
|
73 mike 1.1
|
74 mike 1.14 // Clone the path as a C String but discard trailing slash if any:
|
75 mike 1.1
76 static char* _clonePath(const String& path)
77 {
78 char* p = path.allocateCString();
79
80 if (!*p)
81 return p;
82
83 char* last = p + path.getLength() - 1;
84
85 if (*last == '/')
86 *last = '\0';
87
88 return p;
89 }
90
91 Boolean FileSystem::exists(const String& path)
92 {
|
93 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
94 mike 1.14 return System::exists(p.getPointer());
|
95 mike 1.1 }
96
|
97 karl 1.6 Boolean FileSystem::getCurrentDirectory(String& path)
98 {
|
99 mike 1.14 path.clear();
100 char tmp[4096];
|
101 karl 1.6
|
102 mike 1.14 if (!System::getCurrentDirectory(tmp, sizeof(tmp) - 1))
|
103 karl 1.6 return false;
104
105 path.append(tmp);
106 return true;
107 }
108
|
109 mike 1.2 Boolean FileSystem::existsIgnoreCase(const String& path, String& realPath)
110 {
111 realPath.clear();
|
112 mike 1.5 ArrayDestroyer<char> destroyer(_clonePath(path));
|
113 mike 1.2 char* p = destroyer.getPointer();
114
115 char* dirPath;
116 char* fileName;
117 char* slash = strrchr(p, '/');
118
119 if (slash)
120 {
121 *slash = '\0';
122 fileName = slash + 1;
123 dirPath = p;
|
124 mike 1.14
|
125 mike 1.2 if (*fileName == '\0')
126 return false;
127 }
128 else
129 {
130 fileName = p;
131 dirPath = ".";
132 }
133
134 for (Dir dir(dirPath); dir.more(); dir.next())
135 {
|
136 mike 1.14 if (CompareIgnoreCase(fileName, dir.getName()) == 0)
|
137 mike 1.2 {
138 if (strcmp(dirPath, ".") == 0)
139 realPath = dir.getName();
140 else
141 {
142 realPath = dirPath;
143 realPath += '/';
144 realPath += dir.getName();
145 }
146 return true;
147 }
148 }
149
150 return false;
151 }
152
|
153 mike 1.1 Boolean FileSystem::canRead(const String& path)
154 {
|
155 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
156 mike 1.14 return System::canRead(p.getPointer());
|
157 mike 1.1 }
158
159 Boolean FileSystem::canWrite(const String& path)
160 {
|
161 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
162 mike 1.14 return System::canWrite(p.getPointer());
|
163 mike 1.1 }
164
165 Boolean FileSystem::isDirectory(const String& path)
166 {
|
167 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
168 mike 1.14 return System::isDirectory(p.getPointer());
|
169 mike 1.1 }
170
171 Boolean FileSystem::changeDirectory(const String& path)
172 {
|
173 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
174 mike 1.14 return System::changeDirectory(p.getPointer());
|
175 mike 1.1 }
176
177 Boolean FileSystem::makeDirectory(const String& path)
178 {
|
179 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
180 mike 1.14 return System::makeDirectory(p.getPointer());
|
181 mike 1.1 }
182
183 Boolean FileSystem::getFileSize(const String& path, Uint32& size)
184 {
|
185 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
186 mike 1.14 return System::getFileSize(p.getPointer(), size);
|
187 mike 1.1 }
188
189 Boolean FileSystem::removeDirectory(const String& path)
190 {
|
191 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
192 mike 1.14 return System::removeDirectory(p.getPointer());
|
193 mike 1.1 }
194
|
195 karl 1.6 Boolean FileSystem::removeDirectoryHier(const String& path)
196 {
|
197 mike 1.7 Array<String> fileList;
|
198 karl 1.6
|
199 karl 1.13 // Get contents of current directory
|
200 mike 1.14
|
201 karl 1.6 if (!FileSystem::getDirectoryContents(path,fileList))
202 return false;
203
204 // for files-in-directory, delete or recall removedir
|
205 mike 1.14
|
206 karl 1.6 for (Uint32 i = 0, n = fileList.getSize(); i < n; i++)
|
207 karl 1.10 {
|
208 karl 1.13 String newPath = path; // extend path to subdir
209 newPath.append("/");
210 newPath.append(fileList[i]);
|
211 karl 1.10
|
212 karl 1.13 if (FileSystem::isDirectory(newPath))
213 {
214 // Recall ourselves with extended path
215 if (!FileSystem::removeDirectoryHier(newPath))
216 return false;
|
217 karl 1.6 }
218
|
219 karl 1.13 else
220 {
221 if (!FileSystem::removeFile(newPath))
222 return false;
|
223 karl 1.6 }
224 }
|
225 mike 1.8
|
226 karl 1.6 return removeDirectory(path);
227 }
228
|
229 mike 1.1 Boolean FileSystem::removeFile(const String& path)
230 {
|
231 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
232 mike 1.1 return unlink(p.getPointer()) == 0;
233 }
234
235 void FileSystem::loadFileToMemory(
236 Array<Sint8>& array,
237 const String& fileName)
238 {
239 Uint32 fileSize;
240
241 if (!getFileSize(fileName, fileSize))
242 throw CannotOpenFile(fileName);
243
244 char* tmp = fileName.allocateCString();
245 FILE* fp = fopen(tmp, "rb");
246 delete [] tmp;
247
248 if (fp == NULL)
249 throw CannotOpenFile(fileName);
250
251 array.reserve(fileSize);
252 char buffer[4096];
253 mike 1.1 size_t n;
254
255 while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0)
256 array.append(buffer, n);
257
258 fclose(fp);
259 }
260
261 Boolean FileSystem::compare(
262 const String& fileName1,
263 const String& fileName2)
264 {
265 Uint32 fileSize1;
266
267 if (!getFileSize(fileName1, fileSize1))
268 throw CannotOpenFile(fileName1);
269
270 Uint32 fileSize2;
271
272 if (!getFileSize(fileName2, fileSize2))
273 throw CannotOpenFile(fileName2);
274 mike 1.1
275 if (fileSize1 != fileSize2)
276 return false;
277
278 char* tmp1 = fileName1.allocateCString();
279 FILE* fp1 = fopen(tmp1, "rb");
280 delete [] tmp1;
281
282 if (fp1 == NULL)
283 throw CannotOpenFile(fileName1);
284
285 char* tmp2 = fileName2.allocateCString();
286 FILE* fp2 = fopen(tmp2, "rb");
287 delete [] tmp2;
288
289 if (fp2 == NULL)
290 {
291 fclose(fp1);
292 throw CannotOpenFile(fileName2);
293 }
294
295 mike 1.1 int c1;
296 int c2;
297
298 while ((c1 = fgetc(fp1)) != EOF && (c2 = fgetc(fp2)) != EOF)
299 {
300 if (c1 != c2)
301 {
302 fclose(fp1);
303 fclose(fp2);
304 return false;
305 }
306 }
307
308 fclose(fp1);
309 fclose(fp2);
310 return true;
311 }
312
|
313 mike 1.14 //
314 // Get the file list in the directory into the
315 // array of strings provided
316 // @return The function should return false under these circumstances:
317 //
318 //
319 // 1. The directory does not exist.
320 // 2. The file exists but is not a directory.
321 // 3. The directory is inaccessible.
322 //
323 //
|
324 mike 1.1 Boolean FileSystem::getDirectoryContents(
325 const String& path,
326 Array<String>& paths)
327 {
|
328 karl 1.6 // This may be just extra fluff but added anyway
329 if (!FileSystem::isDirectory(path))
330 return false;
331
|
332 mike 1.1 paths.clear();
|
333 mike 1.14
|
334 karl 1.6 try
335 {
336 for (Dir dir(path); dir.more(); dir.next())
337 {
338 String name = dir.getName();
|
339 mike 1.14
|
340 karl 1.6 if (String::equal(name, ".") || String::equal(name, ".."))
341 continue;
|
342 mike 1.14
|
343 karl 1.6 paths.append(name);
344 }
345 return true;
346 }
|
347 mike 1.1
|
348 karl 1.6 // Catch the Dir exception
349 catch(CannotOpenDirectory&)
|
350 mike 1.1 {
|
351 karl 1.6 return false;
|
352 mike 1.1 }
353
|
354 mike 1.3 }
355
356 Boolean FileSystem::renameFile(
357 const String& oldFileName,
358 const String& newFileName)
359 {
|
360 mike 1.5 ArrayDestroyer<char> p(oldFileName.allocateCString());
361 ArrayDestroyer<char> q(newFileName.allocateCString());
|
362 mike 1.14 return System::renameFile(p.getPointer(), q.getPointer());
|
363 mike 1.1 }
364
365 PEGASUS_NAMESPACE_END
|