(file) Return to FileSystem.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  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           // Revision 1.1.1.1  2001/01/14 19:51:35  mike
 27           // Pegasus import
 28           //
 29 mike  1.1 //
 30           //END_HISTORY
 31           
 32           #include <Pegasus/Common/Config.h>
 33           
 34           #ifdef PEGASUS_OS_TYPE_WINDOWS
 35           # include <io.h>
 36           # include <direct.h>
 37           #else
 38           # include <unistd.h>
 39           # include <dirent.h>
 40           #endif
 41           
 42           #include <sys/stat.h>
 43           #include <sys/types.h>
 44           #include <cstdio>
 45           #include "Destroyer.h"
 46           #include "FileSystem.h"
 47 mike  1.2 #include "Dir.h"
 48 mike  1.1 
 49           // ATTN-B: porting!
 50           
 51           PEGASUS_NAMESPACE_BEGIN
 52           
 53           #ifdef PEGASUS_OS_TYPE_WINDOWS
 54           static const int ACCESS_EXISTS = 0;
 55           static const int ACCESS_WRITE = 2;
 56           static const int ACCESS_READ = 4;
 57           static const int ACCESS_READ_AND_WRITE = 6;
 58           #endif
 59           
 60           // Clone the path but discard trailing slash if any:
 61           
 62           static char* _clonePath(const String& path)
 63           {
 64               char* p = path.allocateCString();
 65           
 66               if (!*p)
 67           	return p;
 68           
 69 mike  1.1     char* last = p + path.getLength() - 1;
 70           
 71               if (*last == '/')
 72           	*last = '\0';
 73           
 74               return p;
 75           }
 76           
 77           Boolean FileSystem::exists(const String& path)
 78           {
 79               Destroyer<char> p(_clonePath(path));
 80           
 81           #ifdef PEGASUS_OS_TYPE_WINDOWS
 82               return _access(p.getPointer(), ACCESS_EXISTS) == 0;
 83           #else
 84               return access(p.getPointer(), F_OK) == 0;
 85           #endif
 86           }
 87           
 88 mike  1.2 Boolean FileSystem::existsIgnoreCase(const String& path, String& realPath)
 89           {
 90               realPath.clear();
 91               Destroyer<char> destroyer(_clonePath(path));
 92               char* p = destroyer.getPointer();
 93           
 94               char* dirPath;
 95               char* fileName;
 96               char* slash = strrchr(p, '/');
 97           
 98               if (slash)
 99               {
100           	*slash = '\0';
101           	fileName = slash + 1;
102           	dirPath = p;
103           	if (*fileName == '\0')
104           	    return false;
105               }
106               else
107               {
108           	fileName = p;
109 mike  1.2 	dirPath = ".";
110               }
111           
112               for (Dir dir(dirPath); dir.more(); dir.next())
113               {
114           #ifdef PEGASUS_OS_TYPE_WINDOWS
115           	if (stricmp(fileName, dir.getName()) == 0)
116           #else
117           	if (strcasecmp(fileName, dir.getName()) == 0)
118           #endif
119           	{
120           	    if (strcmp(dirPath, ".") == 0)
121           		realPath = dir.getName();
122           	    else
123           	    {
124           		realPath = dirPath;
125           		realPath += '/';
126           		realPath += dir.getName();
127           	    }
128           	    return true;
129           	}
130 mike  1.2     }
131           
132               return false;
133           }
134           
135 mike  1.1 Boolean FileSystem::canRead(const String& path)
136           {
137               Destroyer<char> p(_clonePath(path));
138           
139           #ifdef PEGASUS_OS_TYPE_WINDOWS
140               return _access(p.getPointer(), ACCESS_READ) == 0;
141           #else
142               return access(p.getPointer(), R_OK) == 0;
143           #endif
144           }
145           
146           Boolean FileSystem::canWrite(const String& path)
147           {
148               Destroyer<char> p(_clonePath(path));
149           
150           #ifdef PEGASUS_OS_TYPE_WINDOWS
151               return _access(p.getPointer(), ACCESS_WRITE) == 0;
152           #else
153               return access(p.getPointer(), W_OK) == 0;
154           #endif
155           }
156 mike  1.1 
157           #if 0
158           // ATTN: not implemented for NT. But not used by Pegasus.
159           Boolean FileSystem::canExecute(const String& path)
160           {
161               Destroyer<char> p(_clonePath(path));
162               return access(p.getPointer(), X_OK) == 0;
163           }
164           #endif
165           
166           Boolean FileSystem::isDirectory(const String& path)
167           {
168               Destroyer<char> p(_clonePath(path));
169           
170               struct stat st;
171           
172           #ifdef PEGASUS_OS_TYPE_WINDOWS
173           
174               if (stat(p.getPointer(), &st) != 0)
175           	return false;
176           
177 mike  1.1     Boolean result = (st.st_mode & _S_IFDIR) != 0;
178               return result;
179           
180           #else
181           
182               if (stat(p.getPointer(), &st) != 0)
183           	return false;
184           
185               Boolean result = S_ISDIR(st.st_mode);
186               return result;
187           
188           #endif
189           }
190           
191           Boolean FileSystem::changeDirectory(const String& path)
192           {
193               Destroyer<char> p(_clonePath(path));
194               return chdir(p.getPointer()) == 0;
195           }
196           
197           Boolean FileSystem::makeDirectory(const String& path)
198 mike  1.1 {
199               Destroyer<char> p(_clonePath(path));
200           #ifdef PEGASUS_OS_TYPE_WINDOWS
201               return _mkdir(p.getPointer()) == 0;
202           #else
203               return mkdir(p.getPointer(), 0777) == 0;
204           #endif
205           }
206           
207           Boolean FileSystem::getFileSize(const String& path, Uint32& size)
208           {
209               struct stat st;
210           
211               Destroyer<char> p(_clonePath(path));
212           
213               if (stat(p.getPointer(), &st) != 0)
214           	return false;
215           
216               size = st.st_size;
217               return true;
218           }
219 mike  1.1 
220           Boolean FileSystem::removeDirectory(const String& path)
221           {
222               Destroyer<char> p(_clonePath(path));
223               return rmdir(p.getPointer()) == 0;	
224           }
225           
226           Boolean FileSystem::removeFile(const String& path)
227           {
228               Destroyer<char> p(_clonePath(path));
229               return unlink(p.getPointer()) == 0;	
230           }
231           
232           void FileSystem::loadFileToMemory(
233               Array<Sint8>& array,
234               const String& fileName)
235           {
236               Uint32 fileSize;
237           
238               if (!getFileSize(fileName, fileSize))
239           	throw CannotOpenFile(fileName);
240 mike  1.1 
241               char* tmp = fileName.allocateCString();
242               FILE* fp = fopen(tmp, "rb");
243               delete [] tmp;
244           
245               if (fp == NULL)
246           	throw CannotOpenFile(fileName);
247           
248               array.reserve(fileSize);
249               char buffer[4096];
250               size_t n;
251           
252               while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0)
253                   array.append(buffer, n);
254           
255               fclose(fp);
256           }
257           
258           Boolean FileSystem::compare(
259               const String& fileName1,
260               const String& fileName2)
261 mike  1.1 {
262               Uint32 fileSize1;
263           
264               if (!getFileSize(fileName1, fileSize1))
265           	throw CannotOpenFile(fileName1);
266           
267               Uint32 fileSize2;
268           
269               if (!getFileSize(fileName2, fileSize2))
270           	throw CannotOpenFile(fileName2);
271           
272               if (fileSize1 != fileSize2)
273           	return false;
274           
275               char* tmp1 = fileName1.allocateCString();
276               FILE* fp1 = fopen(tmp1, "rb");
277               delete [] tmp1;
278           
279               if (fp1 == NULL)
280           	throw CannotOpenFile(fileName1);
281           
282 mike  1.1     char* tmp2 = fileName2.allocateCString();
283               FILE* fp2 = fopen(tmp2, "rb");
284               delete [] tmp2;
285           
286               if (fp2 == NULL)
287               {
288           	fclose(fp1);
289           	throw CannotOpenFile(fileName2);
290               }
291           
292               int c1;
293               int c2;
294           
295               while ((c1 = fgetc(fp1)) != EOF && (c2 = fgetc(fp2)) != EOF)
296               {
297           	if (c1 != c2)
298           	{
299           	    fclose(fp1);
300           	    fclose(fp2);
301           	    return false;
302           	}
303 mike  1.1     }
304           
305               fclose(fp1);
306               fclose(fp2);
307               return true;
308           }
309           
310           Boolean FileSystem::getDirectoryContents(
311               const String& path,
312               Array<String>& paths)
313           {
314               paths.clear();
315           
316 mike  1.2     for (Dir dir(path); dir.more(); dir.next())
317 mike  1.1     {
318 mike  1.2 	String name = dir.getName();
319 mike  1.1 
320 mike  1.2 	if (name == "." || name ==  "..")
321 mike  1.1 	    continue;
322           
323           	paths.append(name);
324               }
325           
326               return true;
327           }
328           
329           PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2