1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is mozilla.org code.
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
---|
26 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 |
|
---|
39 |
|
---|
40 |
|
---|
41 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
---|
42 | THESE CLASSES ARE DEPRECATED AND UNSUPPORTED! USE |nsIFile| and |nsILocalFile|.
|
---|
43 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
---|
44 |
|
---|
45 |
|
---|
46 |
|
---|
47 |
|
---|
48 |
|
---|
49 |
|
---|
50 |
|
---|
51 |
|
---|
52 |
|
---|
53 |
|
---|
54 | // First checked in on 98/11/20 by John R. McMullen in the wrong directory.
|
---|
55 | // Checked in again 98/12/04.
|
---|
56 | // Polished version 98/12/08.
|
---|
57 |
|
---|
58 | //========================================================================================
|
---|
59 | //
|
---|
60 | // Classes defined:
|
---|
61 | //
|
---|
62 | // nsFilePath, nsFileURL, nsFileSpec, nsPersistentFileDescriptor
|
---|
63 | // nsDirectoryIterator.
|
---|
64 | //
|
---|
65 | // Q. How should I represent files at run time?
|
---|
66 | // A. Use nsFileSpec. Using char* will lose information on some platforms.
|
---|
67 | //
|
---|
68 | // Q. Then what are nsFilePath and nsFileURL for?
|
---|
69 | // A. Only when you need a char* parameter for legacy code.
|
---|
70 | //
|
---|
71 | // Q. How should I represent files in a persistent way (eg, in a disk file)?
|
---|
72 | // A. Use nsPersistentFileDescriptor. Convert to and from nsFileSpec at run time.
|
---|
73 | //
|
---|
74 | // This suite provides the following services:
|
---|
75 | //
|
---|
76 | // 1. Encapsulates all platform-specific file details, so that files can be
|
---|
77 | // described correctly without any platform #ifdefs
|
---|
78 | //
|
---|
79 | // 2. Type safety. This will fix the problems that used to occur because people
|
---|
80 | // confused file paths. They used to use const char*, which could mean three
|
---|
81 | // or four different things. Bugs were introduced as people coded, right up
|
---|
82 | // to the moment Communicator 4.5 shipped.
|
---|
83 | //
|
---|
84 | // 3. Used in conjunction with nsFileStream.h (q.v.), this supports all the power
|
---|
85 | // and readability of the ansi stream syntax.
|
---|
86 | //
|
---|
87 | // Basic example:
|
---|
88 | //
|
---|
89 | // nsFilePath myPath("/Development/iotest.txt");
|
---|
90 | //
|
---|
91 | // nsOutputFileStream testStream(nsFileSpec(myPath));
|
---|
92 | // testStream << "Hello World" << nsEndl;
|
---|
93 | //
|
---|
94 | // 4. Handy methods for manipulating file specifiers safely, e.g. MakeUnique(),
|
---|
95 | // SetLeafName(), Exists().
|
---|
96 | //
|
---|
97 | // 5. Easy cross-conversion.
|
---|
98 | //
|
---|
99 | // Examples:
|
---|
100 | //
|
---|
101 | // Initialize a URL from a string
|
---|
102 | //
|
---|
103 | // nsFileURL fileURL("file:///Development/MPW/MPW%20Shell");
|
---|
104 | //
|
---|
105 | // Initialize a Unix-style path from a URL
|
---|
106 | //
|
---|
107 | // nsFilePath filePath(fileURL);
|
---|
108 | //
|
---|
109 | // Initialize a file spec from a URL
|
---|
110 | //
|
---|
111 | // nsFileSpec fileSpec(fileURL);
|
---|
112 | //
|
---|
113 | // Make the spec unique.
|
---|
114 | //
|
---|
115 | // fileSpec.MakeUnique();
|
---|
116 | //
|
---|
117 | // Assign the spec to a URL (causing conversion)
|
---|
118 | //
|
---|
119 | // fileURL = fileSpec;
|
---|
120 | //
|
---|
121 | // Assign a unix path using a string
|
---|
122 | //
|
---|
123 | // filePath = "/Development/MPW/SysErrs.err";
|
---|
124 | //
|
---|
125 | // Assign to a file spec using a unix path (causing conversion).
|
---|
126 | //
|
---|
127 | // fileSpec = filePath;
|
---|
128 | //
|
---|
129 | // Make this unique.
|
---|
130 | //
|
---|
131 | // fileSpec.MakeUnique();
|
---|
132 | //
|
---|
133 | // 6. Fixes a bug that have been there for a long time, and
|
---|
134 | // is inevitable if you use NSPR alone, where files are described as paths.
|
---|
135 | //
|
---|
136 | // The problem affects platforms (Macintosh) in which a path does not fully
|
---|
137 | // specify a file, because two volumes can have the same name. This
|
---|
138 | // is solved by holding a "private" native file spec inside the
|
---|
139 | // nsFilePath and nsFileURL classes, which is used when appropriate.
|
---|
140 | //
|
---|
141 | //========================================================================================
|
---|
142 |
|
---|
143 | #ifndef _FILESPEC_H_
|
---|
144 | #define _FILESPEC_H_
|
---|
145 |
|
---|
146 | #include "xpcomobsolete.h"
|
---|
147 | #include "nsError.h"
|
---|
148 | #include "nsString.h"
|
---|
149 | #include "nsReadableUtils.h"
|
---|
150 | #include "nsCRT.h"
|
---|
151 | #include "prtypes.h"
|
---|
152 |
|
---|
153 | //========================================================================================
|
---|
154 | // Compiler-specific macros, as needed
|
---|
155 | //========================================================================================
|
---|
156 | #if !defined(NS_USING_NAMESPACE) && (defined(__MWERKS__) || defined(_MSC_VER))
|
---|
157 | #define NS_USING_NAMESPACE
|
---|
158 | #endif
|
---|
159 |
|
---|
160 | #ifdef NS_USING_NAMESPACE
|
---|
161 |
|
---|
162 | #define NS_NAMESPACE_PROTOTYPE
|
---|
163 | #define NS_NAMESPACE namespace
|
---|
164 | #define NS_NAMESPACE_END
|
---|
165 | #define NS_EXPLICIT explicit
|
---|
166 | #else
|
---|
167 |
|
---|
168 | #define NS_NAMESPACE_PROTOTYPE static
|
---|
169 | #define NS_NAMESPACE struct
|
---|
170 | #define NS_NAMESPACE_END ;
|
---|
171 | #define NS_EXPLICIT
|
---|
172 |
|
---|
173 | #endif
|
---|
174 | //=========================== End Compiler-specific macros ===============================
|
---|
175 |
|
---|
176 | #include "nsILocalFile.h"
|
---|
177 | #include "nsCOMPtr.h"
|
---|
178 |
|
---|
179 | #if defined(XP_MAC)
|
---|
180 | #include <Files.h>
|
---|
181 | #include "nsILocalFileMac.h"
|
---|
182 | #elif defined(XP_UNIX) || defined(XP_BEOS)
|
---|
183 | #include <dirent.h>
|
---|
184 | #elif defined(XP_WIN)
|
---|
185 |
|
---|
186 | // This clashes with some of the Win32 system headers (specifically,
|
---|
187 | // winbase.h). Hopefully they'll have been included first; else we may
|
---|
188 | // have problems. We could include winbase.h before doing this;
|
---|
189 | // unfortunately, it's bring in too much crap and'd slow stuff down
|
---|
190 | // more than it's worth doing.
|
---|
191 | #ifdef CreateDirectory
|
---|
192 | #undef CreateDirectory
|
---|
193 | #endif
|
---|
194 |
|
---|
195 | #include "prio.h"
|
---|
196 | #elif defined(XP_OS2)
|
---|
197 | #define INCL_DOS
|
---|
198 | #define INCL_DOSERRORS
|
---|
199 | #define INCL_WIN
|
---|
200 | #define INCL_GPI
|
---|
201 | #include <os2.h>
|
---|
202 | #include "prio.h"
|
---|
203 | #endif
|
---|
204 |
|
---|
205 | //========================================================================================
|
---|
206 | // Here are the allowable ways to describe a file.
|
---|
207 | //========================================================================================
|
---|
208 |
|
---|
209 | class nsFileSpec; // Preferred. For i/o use nsInputFileStream, nsOutputFileStream
|
---|
210 | class nsFilePath;
|
---|
211 | class nsFileURL;
|
---|
212 | class nsNSPRPath; // This can be passed to NSPR file I/O routines, if you must.
|
---|
213 | class nsPersistentFileDescriptor; // Used for storage across program launches.
|
---|
214 |
|
---|
215 | #define kFileURLPrefix "file://"
|
---|
216 | #define kFileURLPrefixLength (7)
|
---|
217 |
|
---|
218 | class nsOutputStream;
|
---|
219 | class nsInputStream;
|
---|
220 | class nsIOutputStream;
|
---|
221 | class nsIInputStream;
|
---|
222 | class nsOutputFileStream;
|
---|
223 | class nsInputFileStream;
|
---|
224 | class nsOutputConsoleStream;
|
---|
225 |
|
---|
226 | class nsIUnicodeEncoder;
|
---|
227 | class nsIUnicodeDecoder;
|
---|
228 |
|
---|
229 | //========================================================================================
|
---|
230 | // Conversion of native file errors to nsresult values. These are really only for use
|
---|
231 | // in the file module, clients of this interface shouldn't really need them.
|
---|
232 | // Error results returned from this interface have, in the low-order 16 bits,
|
---|
233 | // native errors that are masked to 16 bits. Assumption: a native error of 0 is success
|
---|
234 | // on all platforms. Note the way we define this using an inline function. This
|
---|
235 | // avoids multiple evaluation if people go NS_FILE_RESULT(function_call()).
|
---|
236 | #define NS_FILE_RESULT(x) ns_file_convert_result((PRInt32)x)
|
---|
237 | nsresult ns_file_convert_result(PRInt32 nativeErr);
|
---|
238 | #define NS_FILE_FAILURE NS_FILE_RESULT(-1)
|
---|
239 |
|
---|
240 | //========================================================================================
|
---|
241 | class nsSimpleCharString
|
---|
242 | // An envelope for char*: reference counted. Used internally by all the nsFileSpec
|
---|
243 | // classes below.
|
---|
244 | //========================================================================================
|
---|
245 | {
|
---|
246 | public:
|
---|
247 | nsSimpleCharString();
|
---|
248 | nsSimpleCharString(const char*);
|
---|
249 | nsSimpleCharString(const nsString&);
|
---|
250 | nsSimpleCharString(const nsSimpleCharString&);
|
---|
251 | nsSimpleCharString(const char* inData, PRUint32 inLength);
|
---|
252 |
|
---|
253 | ~nsSimpleCharString();
|
---|
254 |
|
---|
255 | void operator = (const char*);
|
---|
256 | void operator = (const nsString&);
|
---|
257 | void operator = (const nsSimpleCharString&);
|
---|
258 |
|
---|
259 | operator const char*() const { return mData ? mData->mString : 0; }
|
---|
260 | operator char* ()
|
---|
261 | {
|
---|
262 | ReallocData(Length()); // requires detaching if shared...
|
---|
263 | return mData ? mData->mString : 0;
|
---|
264 | }
|
---|
265 | PRBool operator == (const char*);
|
---|
266 | PRBool operator == (const nsString&);
|
---|
267 | PRBool operator == (const nsSimpleCharString&);
|
---|
268 |
|
---|
269 | void operator += (const char* inString);
|
---|
270 | nsSimpleCharString operator + (const char* inString) const;
|
---|
271 |
|
---|
272 | char operator [](int i) const { return mData ? mData->mString[i] : '\0'; }
|
---|
273 | char& operator [](int i)
|
---|
274 | {
|
---|
275 | if (i >= (int)Length())
|
---|
276 | ReallocData((PRUint32)i + 1);
|
---|
277 | return mData->mString[i]; // caveat appelator
|
---|
278 | }
|
---|
279 | char& operator [](unsigned int i) { return (*this)[(int)i]; }
|
---|
280 |
|
---|
281 | void Catenate(const char* inString1, const char* inString2);
|
---|
282 |
|
---|
283 | void SetToEmpty();
|
---|
284 | PRBool IsEmpty() const { return Length() == 0; }
|
---|
285 |
|
---|
286 | PRUint32 Length() const { return mData ? mData->mLength : 0; }
|
---|
287 | void SetLength(PRUint32 inLength) { ReallocData(inLength); }
|
---|
288 | void CopyFrom(const char* inData, PRUint32 inLength);
|
---|
289 | void LeafReplace(char inSeparator, const char* inLeafName);
|
---|
290 | char* GetLeaf(char inSeparator) const; // use PR_Free()
|
---|
291 | void Unescape();
|
---|
292 |
|
---|
293 | protected:
|
---|
294 |
|
---|
295 | void AddRefData();
|
---|
296 | void ReleaseData();
|
---|
297 | void ReallocData(PRUint32 inLength);
|
---|
298 |
|
---|
299 | //--------------------------------------------------
|
---|
300 | // Data
|
---|
301 | //--------------------------------------------------
|
---|
302 |
|
---|
303 | protected:
|
---|
304 |
|
---|
305 | struct Data {
|
---|
306 | int mRefCount;
|
---|
307 | PRUint32 mLength;
|
---|
308 | char mString[1];
|
---|
309 | };
|
---|
310 | Data* mData;
|
---|
311 | }; // class nsSimpleCharString
|
---|
312 |
|
---|
313 | //========================================================================================
|
---|
314 | class NS_COM_OBSOLETE nsFileSpec
|
---|
315 | // This is whatever each platform really prefers to describe files as. Declared first
|
---|
316 | // because the other two types have an embedded nsFileSpec object.
|
---|
317 | //========================================================================================
|
---|
318 | {
|
---|
319 | public:
|
---|
320 | nsFileSpec();
|
---|
321 |
|
---|
322 | // These two meathods take *native* file paths.
|
---|
323 | NS_EXPLICIT nsFileSpec(const char* inNativePath, PRBool inCreateDirs = PR_FALSE);
|
---|
324 | NS_EXPLICIT nsFileSpec(const nsString& inNativePath, PRBool inCreateDirs = PR_FALSE);
|
---|
325 |
|
---|
326 |
|
---|
327 | NS_EXPLICIT nsFileSpec(const nsFilePath& inPath);
|
---|
328 | NS_EXPLICIT nsFileSpec(const nsFileURL& inURL);
|
---|
329 | nsFileSpec(const nsFileSpec& inPath);
|
---|
330 | virtual ~nsFileSpec();
|
---|
331 |
|
---|
332 | // These two operands take *native* file paths.
|
---|
333 | void operator = (const char* inNativePath);
|
---|
334 |
|
---|
335 | void operator = (const nsFilePath& inPath);
|
---|
336 | void operator = (const nsFileURL& inURL);
|
---|
337 | void operator = (const nsFileSpec& inOther);
|
---|
338 | void operator = (const nsPersistentFileDescriptor& inOther);
|
---|
339 |
|
---|
340 | PRBool operator ==(const nsFileSpec& inOther) const;
|
---|
341 | PRBool operator !=(const nsFileSpec& inOther) const;
|
---|
342 |
|
---|
343 |
|
---|
344 | // Returns a native path, and allows the
|
---|
345 | // path to be "passed" to legacy code. This practice
|
---|
346 | // is VERY EVIL and should only be used to support legacy
|
---|
347 | // code. Using it guarantees bugs on Macintosh.
|
---|
348 | // The path is cached and freed by the nsFileSpec destructor
|
---|
349 | // so do not delete (or free) it. See also nsNSPRPath below,
|
---|
350 | // if you really must pass a string to PR_OpenFile().
|
---|
351 | // Doing so will introduce two automatic bugs.
|
---|
352 | const char* GetCString() const;
|
---|
353 |
|
---|
354 | // Same as GetCString (please read the comments).
|
---|
355 | // Do not try to free this!
|
---|
356 | operator const char* () const { return GetCString(); }
|
---|
357 |
|
---|
358 | // Same as GetCString (please read the comments).
|
---|
359 | // Do not try to free this!
|
---|
360 | const char* GetNativePathCString() const { return GetCString(); }
|
---|
361 |
|
---|
362 | PRBool IsChildOf(nsFileSpec &possibleParent);
|
---|
363 |
|
---|
364 | #if defined(XP_MAC)
|
---|
365 | // For Macintosh people, this is meant to be useful in its own right as a C++ version
|
---|
366 | // of the FSSpec struct.
|
---|
367 | nsFileSpec(
|
---|
368 | short vRefNum,
|
---|
369 | long parID,
|
---|
370 | ConstStr255Param name,
|
---|
371 | PRBool resolveAlias = PR_TRUE);
|
---|
372 |
|
---|
373 | nsFileSpec(const FSSpec& inSpec, PRBool resolveAlias = PR_TRUE);
|
---|
374 | void operator = (const FSSpec& inSpec);
|
---|
375 |
|
---|
376 | operator FSSpec* () { return &mSpec; }
|
---|
377 | operator const FSSpec* const () { return &mSpec; }
|
---|
378 | operator FSSpec& () { return mSpec; }
|
---|
379 | operator const FSSpec& () const { return mSpec; }
|
---|
380 |
|
---|
381 | const FSSpec& GetFSSpec() const { return mSpec; }
|
---|
382 | FSSpec& GetFSSpec() { return mSpec; }
|
---|
383 | ConstFSSpecPtr GetFSSpecPtr() const { return &mSpec; }
|
---|
384 | FSSpecPtr GetFSSpecPtr() { return &mSpec; }
|
---|
385 | void MakeAliasSafe();
|
---|
386 | void MakeUnique(ConstStr255Param inSuggestedLeafName);
|
---|
387 | StringPtr GetLeafPName() { return mSpec.name; }
|
---|
388 | ConstStr255Param GetLeafPName() const { return mSpec.name; }
|
---|
389 |
|
---|
390 | OSErr GetCatInfo(CInfoPBRec& outInfo) const;
|
---|
391 |
|
---|
392 | OSErr SetFileTypeAndCreator(OSType type, OSType creator);
|
---|
393 | OSErr GetFileTypeAndCreator(OSType* type, OSType* creator);
|
---|
394 |
|
---|
395 | #endif // end of Macintosh utility methods.
|
---|
396 |
|
---|
397 | PRBool Valid() const { return NS_SUCCEEDED(Error()); }
|
---|
398 | nsresult Error() const
|
---|
399 | {
|
---|
400 | #if !defined(XP_MAC)
|
---|
401 | if (mPath.IsEmpty() && NS_SUCCEEDED(mError))
|
---|
402 | ((nsFileSpec*)this)->mError = NS_ERROR_NOT_INITIALIZED;
|
---|
403 | #endif
|
---|
404 | return mError;
|
---|
405 | }
|
---|
406 | PRBool Failed() const { return (PRBool)NS_FAILED(Error()); }
|
---|
407 |
|
---|
408 | //--------------------------------------------------
|
---|
409 | // Queries and path algebra. These do not modify the disk.
|
---|
410 | //--------------------------------------------------
|
---|
411 |
|
---|
412 | char* GetLeafName() const; // Allocated. Use nsCRT::free().
|
---|
413 | // inLeafName can be a relative path, so this allows
|
---|
414 | // one kind of concatenation of "paths".
|
---|
415 | void SetLeafName(const char* inLeafName);
|
---|
416 |
|
---|
417 | // Return the filespec of the parent directory. Used
|
---|
418 | // in conjunction with GetLeafName(), this lets you
|
---|
419 | // parse a path into a list of node names. Beware,
|
---|
420 | // however, that the top node is still not a name,
|
---|
421 | // but a spec. Volumes on Macintosh can have identical
|
---|
422 | // names. Perhaps could be used for an operator --() ?
|
---|
423 | void GetParent(nsFileSpec& outSpec) const;
|
---|
424 |
|
---|
425 |
|
---|
426 | // ie nsFileSpec::TimeStamp. This is 32 bits now,
|
---|
427 | // but might change, eg, to a 64-bit class. So use the
|
---|
428 | // typedef, and use a streaming operator to convert
|
---|
429 | // to a string, so that your code won't break. It's
|
---|
430 | // none of your business what the number means. Don't
|
---|
431 | // rely on the implementation.
|
---|
432 | typedef PRUint32 TimeStamp;
|
---|
433 |
|
---|
434 | // This will return different values on different
|
---|
435 | // platforms, even for the same file (eg, on a server).
|
---|
436 | // But if the platform is constant, it will increase after
|
---|
437 | // every file modification.
|
---|
438 | void GetModDate(TimeStamp& outStamp) const;
|
---|
439 |
|
---|
440 | PRBool ModDateChanged(const TimeStamp& oldStamp) const
|
---|
441 | {
|
---|
442 | TimeStamp newStamp;
|
---|
443 | GetModDate(newStamp);
|
---|
444 | return newStamp != oldStamp;
|
---|
445 | }
|
---|
446 |
|
---|
447 | PRUint32 GetFileSize() const;
|
---|
448 | PRInt64 GetDiskSpaceAvailable() const;
|
---|
449 |
|
---|
450 | nsFileSpec operator + (const char* inRelativeUnixPath) const;
|
---|
451 |
|
---|
452 | // Concatenate the relative path to this directory.
|
---|
453 | // Used for constructing the filespec of a descendant.
|
---|
454 | // This must be a directory for this to work. This differs
|
---|
455 | // from SetLeafName(), since the latter will work
|
---|
456 | // starting with a sibling of the directory and throws
|
---|
457 | // away its leaf information, whereas this one assumes
|
---|
458 | // this is a directory, and the relative path starts
|
---|
459 | // "below" this.
|
---|
460 | void operator += (const char* inRelativeUnixPath);
|
---|
461 |
|
---|
462 |
|
---|
463 | void MakeUnique();
|
---|
464 | void MakeUnique(const char* inSuggestedLeafName);
|
---|
465 |
|
---|
466 |
|
---|
467 | PRBool IsDirectory() const; // More stringent than Exists()
|
---|
468 | PRBool IsFile() const; // More stringent than Exists()
|
---|
469 | PRBool Exists() const;
|
---|
470 |
|
---|
471 | PRBool IsHidden() const;
|
---|
472 |
|
---|
473 | PRBool IsSymlink() const;
|
---|
474 |
|
---|
475 | //--------------------------------------------------
|
---|
476 | // Creation and deletion of objects. These can modify the disk.
|
---|
477 | //--------------------------------------------------
|
---|
478 |
|
---|
479 | // Called for the spec of an alias. Modifies the spec to
|
---|
480 | // point to the original. Sets mError.
|
---|
481 | nsresult ResolveSymlink(PRBool& wasSymlink);
|
---|
482 |
|
---|
483 | void CreateDirectory(int mode = 0775 /* for unix */);
|
---|
484 | void CreateDir(int mode = 0775) { CreateDirectory(mode); }
|
---|
485 | // workaround for yet another VC++ bug with long identifiers.
|
---|
486 | void Delete(PRBool inRecursive) const;
|
---|
487 | nsresult Truncate(PRInt32 aNewLength) const;
|
---|
488 | void RecursiveCopy(nsFileSpec newDir) const;
|
---|
489 |
|
---|
490 | nsresult Rename(const char* inNewName); // not const: gets updated
|
---|
491 | nsresult CopyToDir(const nsFileSpec& inNewParentDirectory) const;
|
---|
492 | nsresult MoveToDir(const nsFileSpec& inNewParentDirectory);
|
---|
493 | nsresult Execute(const char* args) const;
|
---|
494 |
|
---|
495 | protected:
|
---|
496 |
|
---|
497 | //--------------------------------------------------
|
---|
498 | // Data
|
---|
499 | //--------------------------------------------------
|
---|
500 |
|
---|
501 | protected:
|
---|
502 |
|
---|
503 | // Clear the nsFileSpec contents, resetting it
|
---|
504 | // to the uninitialized state;
|
---|
505 | void Clear();
|
---|
506 |
|
---|
507 | friend class nsFilePath;
|
---|
508 | friend class nsFileURL;
|
---|
509 | friend class nsDirectoryIterator;
|
---|
510 | #if defined(XP_MAC)
|
---|
511 | FSSpec mSpec;
|
---|
512 | #endif
|
---|
513 | nsSimpleCharString mPath;
|
---|
514 | nsresult mError;
|
---|
515 |
|
---|
516 | private:
|
---|
517 | NS_EXPLICIT nsFileSpec(const nsPersistentFileDescriptor& inURL);
|
---|
518 |
|
---|
519 | }; // class nsFileSpec
|
---|
520 |
|
---|
521 | // FOR HISTORICAL REASONS:
|
---|
522 |
|
---|
523 | typedef nsFileSpec nsNativeFileSpec;
|
---|
524 |
|
---|
525 | //========================================================================================
|
---|
526 | class NS_COM_OBSOLETE nsFileURL
|
---|
527 | // This is an escaped string that looks like "file:///foo/bar/mumble%20fish". Since URLs
|
---|
528 | // are the standard way of doing things in mozilla, this allows a string constructor,
|
---|
529 | // which just stashes the string with no conversion.
|
---|
530 | //========================================================================================
|
---|
531 | {
|
---|
532 | public:
|
---|
533 | nsFileURL(const nsFileURL& inURL);
|
---|
534 | NS_EXPLICIT nsFileURL(const char* inURLString, PRBool inCreateDirs = PR_FALSE);
|
---|
535 | NS_EXPLICIT nsFileURL(const nsString& inURLString, PRBool inCreateDirs = PR_FALSE);
|
---|
536 | NS_EXPLICIT nsFileURL(const nsFilePath& inPath);
|
---|
537 | NS_EXPLICIT nsFileURL(const nsFileSpec& inPath);
|
---|
538 | virtual ~nsFileURL();
|
---|
539 |
|
---|
540 | // nsString GetString() const { return mPath; }
|
---|
541 | // may be needed for implementation reasons,
|
---|
542 | // but should not provide a conversion constructor.
|
---|
543 |
|
---|
544 | void operator = (const nsFileURL& inURL);
|
---|
545 | void operator = (const char* inURLString);
|
---|
546 | void operator = (const nsString& inURLString)
|
---|
547 | {
|
---|
548 | *this = NS_LossyConvertUCS2toASCII(inURLString).get();
|
---|
549 | }
|
---|
550 | void operator = (const nsFilePath& inOther);
|
---|
551 | void operator = (const nsFileSpec& inOther);
|
---|
552 |
|
---|
553 | void operator +=(const char* inRelativeUnixPath);
|
---|
554 | nsFileURL operator +(const char* inRelativeUnixPath) const;
|
---|
555 | operator const char* () const { return (const char*)mURL; } // deprecated.
|
---|
556 | const char* GetURLString() const { return (const char*)mURL; }
|
---|
557 | // Not allocated, so don't free it.
|
---|
558 | const char* GetAsString() const { return (const char*)mURL; }
|
---|
559 | // Not allocated, so don't free it.
|
---|
560 |
|
---|
561 | #if defined(XP_MAC)
|
---|
562 | // Accessor to allow quick assignment to a mFileSpec
|
---|
563 | const nsFileSpec& GetFileSpec() const { return mFileSpec; }
|
---|
564 | #endif
|
---|
565 |
|
---|
566 | //--------------------------------------------------
|
---|
567 | // Data
|
---|
568 | //--------------------------------------------------
|
---|
569 |
|
---|
570 | protected:
|
---|
571 | friend class nsFilePath; // to allow construction of nsFilePath
|
---|
572 | nsSimpleCharString mURL;
|
---|
573 |
|
---|
574 | #if defined(XP_MAC)
|
---|
575 | // Since the path on the macintosh does not uniquely specify a file (volumes
|
---|
576 | // can have the same name), stash the secret nsFileSpec, too.
|
---|
577 | nsFileSpec mFileSpec;
|
---|
578 | #endif
|
---|
579 | }; // class nsFileURL
|
---|
580 |
|
---|
581 | //========================================================================================
|
---|
582 | class NS_COM_OBSOLETE nsFilePath
|
---|
583 | // This is a string that looks like "/foo/bar/mumble fish". Same as nsFileURL, but
|
---|
584 | // without the "file:// prefix", and NOT %20 ENCODED! Strings passed in must be
|
---|
585 | // valid unix-style paths in this format.
|
---|
586 | //========================================================================================
|
---|
587 | {
|
---|
588 | public:
|
---|
589 | nsFilePath(const nsFilePath& inPath);
|
---|
590 | NS_EXPLICIT nsFilePath(const char* inUnixPathString, PRBool inCreateDirs = PR_FALSE);
|
---|
591 | NS_EXPLICIT nsFilePath(const nsString& inUnixPathString, PRBool inCreateDirs = PR_FALSE);
|
---|
592 | NS_EXPLICIT nsFilePath(const nsFileURL& inURL);
|
---|
593 | NS_EXPLICIT nsFilePath(const nsFileSpec& inPath);
|
---|
594 | virtual ~nsFilePath();
|
---|
595 |
|
---|
596 |
|
---|
597 | operator const char* () const { return mPath; }
|
---|
598 | // This will return a UNIX string. If you
|
---|
599 | // need a string that can be passed into
|
---|
600 | // NSPR, take a look at the nsNSPRPath class.
|
---|
601 |
|
---|
602 | void operator = (const nsFilePath& inPath);
|
---|
603 | void operator = (const char* inUnixPathString);
|
---|
604 | void operator = (const nsString& inUnixPathString)
|
---|
605 | {
|
---|
606 | *this = NS_LossyConvertUCS2toASCII(inUnixPathString).get();
|
---|
607 | }
|
---|
608 | void operator = (const nsFileURL& inURL);
|
---|
609 | void operator = (const nsFileSpec& inOther);
|
---|
610 |
|
---|
611 | void operator +=(const char* inRelativeUnixPath);
|
---|
612 | nsFilePath operator +(const char* inRelativeUnixPath) const;
|
---|
613 |
|
---|
614 | #if defined(XP_MAC)
|
---|
615 | public:
|
---|
616 | // Accessor to allow quick assignment to a mFileSpec
|
---|
617 | const nsFileSpec& GetFileSpec() const { return mFileSpec; }
|
---|
618 | #endif
|
---|
619 |
|
---|
620 | //--------------------------------------------------
|
---|
621 | // Data
|
---|
622 | //--------------------------------------------------
|
---|
623 |
|
---|
624 | private:
|
---|
625 |
|
---|
626 | nsSimpleCharString mPath;
|
---|
627 | #if defined(XP_MAC)
|
---|
628 | // Since the path on the macintosh does not uniquely specify a file (volumes
|
---|
629 | // can have the same name), stash the secret nsFileSpec, too.
|
---|
630 | nsFileSpec mFileSpec;
|
---|
631 | #endif
|
---|
632 | }; // class nsFilePath
|
---|
633 |
|
---|
634 | //========================================================================================
|
---|
635 | class nsPersistentFileDescriptor
|
---|
636 | // To save information about a file's location in another file, initialize
|
---|
637 | // one of these from your nsFileSpec, and then write this out to your output stream.
|
---|
638 | // To retrieve the info, create one of these, read its value from an input stream.
|
---|
639 | // and then make an nsFileSpec from it.
|
---|
640 | //========================================================================================
|
---|
641 | {
|
---|
642 | public:
|
---|
643 | nsPersistentFileDescriptor() {}
|
---|
644 | // For use prior to reading in from a stream
|
---|
645 | nsPersistentFileDescriptor(const nsPersistentFileDescriptor& inEncodedData);
|
---|
646 | virtual ~nsPersistentFileDescriptor();
|
---|
647 | void operator = (const nsPersistentFileDescriptor& inEncodedData);
|
---|
648 |
|
---|
649 | // Conversions
|
---|
650 | NS_EXPLICIT nsPersistentFileDescriptor(const nsFileSpec& inSpec);
|
---|
651 | void operator = (const nsFileSpec& inSpec);
|
---|
652 |
|
---|
653 | // The following four functions are declared here (as friends). Their implementations
|
---|
654 | // are in mozilla/base/src/nsFileSpecStreaming.cpp.
|
---|
655 |
|
---|
656 | friend nsresult Read(nsIInputStream* aStream, nsPersistentFileDescriptor&);
|
---|
657 | friend nsresult Write(nsIOutputStream* aStream, const nsPersistentFileDescriptor&);
|
---|
658 | // writes the data to a file
|
---|
659 | friend NS_COM_OBSOLETE nsInputStream& operator >> (nsInputStream&, nsPersistentFileDescriptor&);
|
---|
660 | // reads the data from a file
|
---|
661 | friend NS_COM_OBSOLETE nsOutputStream& operator << (nsOutputStream&, const nsPersistentFileDescriptor&);
|
---|
662 | // writes the data to a file
|
---|
663 | friend class nsFileSpec;
|
---|
664 |
|
---|
665 | void GetData(nsAFlatCString& outData) const;
|
---|
666 | void SetData(const nsAFlatCString& inData);
|
---|
667 | void SetData(const char* inData, PRInt32 inSize);
|
---|
668 |
|
---|
669 | //--------------------------------------------------
|
---|
670 | // Data
|
---|
671 | //--------------------------------------------------
|
---|
672 |
|
---|
673 | protected:
|
---|
674 |
|
---|
675 | nsSimpleCharString mDescriptorString;
|
---|
676 |
|
---|
677 | }; // class nsPersistentFileDescriptor
|
---|
678 |
|
---|
679 | //========================================================================================
|
---|
680 | class NS_COM_OBSOLETE nsDirectoryIterator
|
---|
681 | // Example:
|
---|
682 | //
|
---|
683 | // nsFileSpec parentDir(...); // directory over whose children we shall iterate
|
---|
684 | // for (nsDirectoryIterator i(parentDir, PR_FALSE); i.Exists(); i++)
|
---|
685 | // {
|
---|
686 | // // do something with i.Spec()
|
---|
687 | // }
|
---|
688 | //
|
---|
689 | // - or -
|
---|
690 | //
|
---|
691 | // for (nsDirectoryIterator i(parentDir, PR_TRUE); i.Exists(); i--)
|
---|
692 | // {
|
---|
693 | // // do something with i.Spec()
|
---|
694 | // }
|
---|
695 | // This one passed the PR_TRUE flag which will resolve any symlink encountered.
|
---|
696 | //========================================================================================
|
---|
697 | {
|
---|
698 | public:
|
---|
699 | nsDirectoryIterator( const nsFileSpec& parent,
|
---|
700 | PRBool resoveSymLinks);
|
---|
701 | #if !defined(XP_MAC)
|
---|
702 | // Macintosh currently doesn't allocate, so needn't clean up.
|
---|
703 | virtual ~nsDirectoryIterator();
|
---|
704 | #endif
|
---|
705 | PRBool Exists() const { return mExists; }
|
---|
706 | nsDirectoryIterator& operator ++(); // moves to the next item, if any.
|
---|
707 | nsDirectoryIterator& operator ++(int) { return ++(*this); } // post-increment.
|
---|
708 | nsDirectoryIterator& operator --(); // moves to the previous item, if any.
|
---|
709 | nsDirectoryIterator& operator --(int) { return --(*this); } // post-decrement.
|
---|
710 | operator nsFileSpec&() { return mCurrent; }
|
---|
711 |
|
---|
712 | nsFileSpec& Spec() { return mCurrent; }
|
---|
713 |
|
---|
714 | private:
|
---|
715 |
|
---|
716 | #if defined(XP_MAC)
|
---|
717 | OSErr SetToIndex();
|
---|
718 | #endif
|
---|
719 |
|
---|
720 | //--------------------------------------------------
|
---|
721 | // Data
|
---|
722 | //--------------------------------------------------
|
---|
723 |
|
---|
724 | private:
|
---|
725 |
|
---|
726 | nsFileSpec mCurrent;
|
---|
727 | PRBool mExists;
|
---|
728 | PRBool mResoveSymLinks;
|
---|
729 |
|
---|
730 | #if (defined(XP_UNIX) || defined(XP_BEOS) || defined (XP_WIN) || defined(XP_OS2))
|
---|
731 | nsFileSpec mStarting;
|
---|
732 | #endif
|
---|
733 |
|
---|
734 | #if defined(XP_MAC)
|
---|
735 | short mVRefNum;
|
---|
736 | long mParID;
|
---|
737 | short mIndex;
|
---|
738 | short mMaxIndex;
|
---|
739 | #elif defined(XP_UNIX) || defined(XP_BEOS)
|
---|
740 | DIR* mDir;
|
---|
741 | #elif defined(XP_WIN) || defined(XP_OS2)
|
---|
742 | PRDir* mDir; // XXX why not use PRDir for Unix too?
|
---|
743 | #endif
|
---|
744 | }; // class nsDirectoryIterator
|
---|
745 |
|
---|
746 | //========================================================================================
|
---|
747 | class NS_COM_OBSOLETE nsNSPRPath
|
---|
748 | // This class will allow you to pass any one of the nsFile* classes directly into NSPR
|
---|
749 | // without the need to worry about whether you have the right kind of filepath or not.
|
---|
750 | // It will also take care of cleaning up any allocated memory.
|
---|
751 | //========================================================================================
|
---|
752 | {
|
---|
753 | public:
|
---|
754 | NS_EXPLICIT nsNSPRPath(const nsFileSpec& inSpec)
|
---|
755 | : mFilePath(inSpec), modifiedNSPRPath(nsnull) {}
|
---|
756 | NS_EXPLICIT nsNSPRPath(const nsFileURL& inURL)
|
---|
757 | : mFilePath(inURL), modifiedNSPRPath(nsnull) {}
|
---|
758 | NS_EXPLICIT nsNSPRPath(const nsFilePath& inUnixPath)
|
---|
759 | : mFilePath(inUnixPath), modifiedNSPRPath(nsnull) {}
|
---|
760 |
|
---|
761 | virtual ~nsNSPRPath();
|
---|
762 |
|
---|
763 | operator const char*() const;
|
---|
764 | // Returns the path
|
---|
765 | // that NSPR file routines expect on each platform.
|
---|
766 | // Concerning constness, this can modify
|
---|
767 | // modifiedNSPRPath, but it's really just "mutable".
|
---|
768 |
|
---|
769 | //--------------------------------------------------
|
---|
770 | // Data
|
---|
771 | //--------------------------------------------------
|
---|
772 |
|
---|
773 | private:
|
---|
774 |
|
---|
775 | nsFilePath mFilePath;
|
---|
776 | char* modifiedNSPRPath; // Currently used only on XP_WIN,XP_OS2
|
---|
777 | }; // class nsNSPRPath
|
---|
778 |
|
---|
779 |
|
---|
780 | NS_COM_OBSOLETE nsresult NS_FileSpecToIFile(nsFileSpec* fileSpec, nsILocalFile* *result);
|
---|
781 |
|
---|
782 | #endif // _FILESPEC_H_
|
---|