1 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
2 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
3 | *
|
---|
4 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
5 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
6 | * the License. You may obtain a copy of the License at
|
---|
7 | * http://www.mozilla.org/MPL/
|
---|
8 | *
|
---|
9 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
10 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
11 | * for the specific language governing rights and limitations under the
|
---|
12 | * License.
|
---|
13 | *
|
---|
14 | * The Original Code is the Mozilla OS/2 libraries.
|
---|
15 | *
|
---|
16 | * The Initial Developer of the Original Code is
|
---|
17 | * John Fairhurst, <john_fairhurst@iname.com>.
|
---|
18 | * Portions created by the Initial Developer are Copyright (C) 1998
|
---|
19 | * the Initial Developer. All Rights Reserved.
|
---|
20 | *
|
---|
21 | * Contributor(s):
|
---|
22 | * Henry Sobotka <sobotka@axess.com>
|
---|
23 | * 00/01/06: general review and update against Win/Unix versions;
|
---|
24 | * replaced nsFileSpec::Execute implementation with system() call
|
---|
25 | *
|
---|
26 | * Alternatively, the contents of this file may be used under the terms of
|
---|
27 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
---|
28 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
29 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
30 | * of those above. If you wish to allow use of your version of this file only
|
---|
31 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
32 | * use your version of this file under the terms of the MPL, indicate your
|
---|
33 | * decision by deleting the provisions above and replace them with the notice
|
---|
34 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
35 | * the provisions above, a recipient may use your version of this file under
|
---|
36 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
37 | *
|
---|
38 | * ***** END LICENSE BLOCK *****
|
---|
39 | *
|
---|
40 | * This Original Code has been modified by IBM Corporation.
|
---|
41 | * Modifications made by IBM described herein are
|
---|
42 | * Copyright (c) International Business Machines
|
---|
43 | * Corporation, 2000
|
---|
44 | *
|
---|
45 | * Modifications to Mozilla code or documentation
|
---|
46 | * identified per MPL Section 3.3
|
---|
47 | *
|
---|
48 | * Date Modified by Description of modification
|
---|
49 | * 03/23/2000 IBM Corp. Fixed bug where 2 char or less profile names treated as drive letters.
|
---|
50 | * 06/20/2000 IBM Corp. Make it more like Windows version.
|
---|
51 | */
|
---|
52 |
|
---|
53 | #define INCL_DOSERRORS
|
---|
54 | #define INCL_DOS
|
---|
55 | #define INCL_WINWORKPLACE
|
---|
56 | #include <os2.h>
|
---|
57 |
|
---|
58 | #ifdef XP_OS2_VACPP
|
---|
59 | #include <direct.h>
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | #include <sys/types.h>
|
---|
63 | #include <sys/stat.h>
|
---|
64 | #include <limits.h>
|
---|
65 | #include <ctype.h>
|
---|
66 | #include <io.h>
|
---|
67 |
|
---|
68 | //----------------------------------------------------------------------------------------
|
---|
69 | void nsFileSpecHelpers::Canonify(nsSimpleCharString& ioPath, PRBool inMakeDirs)
|
---|
70 | // Canonify, make absolute, and check whether directories exist. This
|
---|
71 | // takes a (possibly relative) native path and converts it into a
|
---|
72 | // fully qualified native path.
|
---|
73 | //----------------------------------------------------------------------------------------
|
---|
74 | {
|
---|
75 | if (ioPath.IsEmpty())
|
---|
76 | return;
|
---|
77 |
|
---|
78 | NS_ASSERTION(strchr((const char*)ioPath, '/') == 0,
|
---|
79 | "This smells like a Unix path. Native path expected! "
|
---|
80 | "Please fix.");
|
---|
81 | if (inMakeDirs)
|
---|
82 | {
|
---|
83 | const int mode = 0700;
|
---|
84 | nsSimpleCharString unixStylePath = ioPath;
|
---|
85 | nsFileSpecHelpers::NativeToUnix(unixStylePath);
|
---|
86 | nsFileSpecHelpers::MakeAllDirectories((const char*)unixStylePath, mode);
|
---|
87 | }
|
---|
88 | char buffer[_MAX_PATH];
|
---|
89 | errno = 0;
|
---|
90 | *buffer = '\0';
|
---|
91 | #ifdef XP_OS2
|
---|
92 | PRBool removedBackslash = PR_FALSE;
|
---|
93 | PRUint32 lenstr = ioPath.Length();
|
---|
94 | char &lastchar = ioPath[lenstr -1];
|
---|
95 |
|
---|
96 | // Strip off any trailing backslash UNLESS it's the backslash that
|
---|
97 | // comes after "X:". Note also that "\" is valid. Sheesh.
|
---|
98 | //
|
---|
99 | if( lastchar == '\\' && (lenstr != 3 || ioPath[1] != ':') && lenstr != 1)
|
---|
100 | {
|
---|
101 | lastchar = '\0';
|
---|
102 | removedBackslash = PR_TRUE;
|
---|
103 | }
|
---|
104 |
|
---|
105 | char canonicalPath[CCHMAXPATH] = "";
|
---|
106 |
|
---|
107 | DosQueryPathInfo( (char*) ioPath,
|
---|
108 | FIL_QUERYFULLNAME,
|
---|
109 | canonicalPath,
|
---|
110 | CCHMAXPATH);
|
---|
111 | #else
|
---|
112 | char* canonicalPath = _fullpath(buffer, ioPath, _MAX_PATH);
|
---|
113 | #endif
|
---|
114 |
|
---|
115 | if (canonicalPath)
|
---|
116 | {
|
---|
117 | NS_ASSERTION( canonicalPath[0] != '\0', "Uh oh...couldn't convert" );
|
---|
118 | if (canonicalPath[0] == '\0')
|
---|
119 | return;
|
---|
120 | #ifdef XP_OS2
|
---|
121 | // If we removed that backslash, add it back onto the fullpath
|
---|
122 | if (removedBackslash)
|
---|
123 | strcat( canonicalPath, "\\");
|
---|
124 | #endif
|
---|
125 | }
|
---|
126 | ioPath = canonicalPath;
|
---|
127 | } // nsFileSpecHelpers::Canonify
|
---|
128 |
|
---|
129 | //----------------------------------------------------------------------------------------
|
---|
130 | void nsFileSpecHelpers::UnixToNative(nsSimpleCharString& ioPath)
|
---|
131 | // This just does string manipulation. It doesn't check reality, or canonify, or
|
---|
132 | // anything
|
---|
133 | //----------------------------------------------------------------------------------------
|
---|
134 | {
|
---|
135 | // Allow for relative or absolute. We can do this in place, because the
|
---|
136 | // native path is never longer.
|
---|
137 |
|
---|
138 | if (ioPath.IsEmpty())
|
---|
139 | return;
|
---|
140 |
|
---|
141 | // Strip initial slash for an absolute path
|
---|
142 | char* src = (char*)ioPath;
|
---|
143 | if (*src == '/') {
|
---|
144 | if (!src[1]) {
|
---|
145 | // allocate new string by copying from ioPath[1]
|
---|
146 | nsSimpleCharString temp = src + 1;
|
---|
147 | ioPath = temp;
|
---|
148 | return;
|
---|
149 | }
|
---|
150 | // Since it was an absolute path, check for the drive letter
|
---|
151 | char* colonPointer = src + 2;
|
---|
152 | if (strstr(src, "|/") == colonPointer)
|
---|
153 | *colonPointer = ':';
|
---|
154 | // allocate new string by copying from ioPath[1]
|
---|
155 | nsSimpleCharString temp = src + 1;
|
---|
156 | ioPath = temp;
|
---|
157 | }
|
---|
158 |
|
---|
159 | src = (char*)ioPath;
|
---|
160 |
|
---|
161 | if (*src) {
|
---|
162 | // Convert '/' to '\'.
|
---|
163 | while (*++src)
|
---|
164 | {
|
---|
165 | if (*src == '/')
|
---|
166 | *src = '\\';
|
---|
167 | }
|
---|
168 | }
|
---|
169 | } // nsFileSpecHelpers::UnixToNative
|
---|
170 |
|
---|
171 | //----------------------------------------------------------------------------------------
|
---|
172 | void nsFileSpecHelpers::NativeToUnix(nsSimpleCharString& ioPath)
|
---|
173 | // This just does string manipulation. It doesn't check reality, or canonify, or
|
---|
174 | // anything. The unix path is longer, so we can't do it in place.
|
---|
175 | //----------------------------------------------------------------------------------------
|
---|
176 | {
|
---|
177 | if (ioPath.IsEmpty())
|
---|
178 | return;
|
---|
179 |
|
---|
180 | // Convert the drive-letter separator, if present
|
---|
181 | nsSimpleCharString temp("/");
|
---|
182 |
|
---|
183 | char* cp = (char*)ioPath + 1;
|
---|
184 | if (strstr(cp, ":\\") == cp)
|
---|
185 | *cp = '|'; // absolute path
|
---|
186 | else
|
---|
187 | temp[0] = '\0'; // relative path
|
---|
188 |
|
---|
189 | // Convert '\' to '/'
|
---|
190 | for (; *cp; cp++)
|
---|
191 | {
|
---|
192 | #ifdef XP_OS2
|
---|
193 | // OS2TODO - implement equivalent of IsDBCSLeadByte
|
---|
194 | #else
|
---|
195 | if(IsDBCSLeadByte(*cp) && *(cp+1) != nsnull)
|
---|
196 | {
|
---|
197 | cp++;
|
---|
198 | continue;
|
---|
199 | }
|
---|
200 | #endif
|
---|
201 | if (*cp == '\\')
|
---|
202 | *cp = '/';
|
---|
203 | }
|
---|
204 | // Add the slash in front.
|
---|
205 | temp += ioPath;
|
---|
206 | ioPath = temp;
|
---|
207 | }
|
---|
208 |
|
---|
209 | //----------------------------------------------------------------------------------------
|
---|
210 | nsFileSpec::nsFileSpec(const nsFilePath& inPath)
|
---|
211 | //----------------------------------------------------------------------------------------
|
---|
212 | {
|
---|
213 | // NS_ASSERTION(0, "nsFileSpec is unsupported - use nsIFile!");
|
---|
214 | *this = inPath;
|
---|
215 | }
|
---|
216 |
|
---|
217 | //----------------------------------------------------------------------------------------
|
---|
218 | void nsFileSpec::operator = (const nsFilePath& inPath)
|
---|
219 | //----------------------------------------------------------------------------------------
|
---|
220 | {
|
---|
221 | mPath = (const char*)inPath;
|
---|
222 | nsFileSpecHelpers::UnixToNative(mPath);
|
---|
223 | mError = NS_OK;
|
---|
224 | } // nsFileSpec::operator =
|
---|
225 |
|
---|
226 | //----------------------------------------------------------------------------------------
|
---|
227 | nsFilePath::nsFilePath(const nsFileSpec& inSpec)
|
---|
228 | //----------------------------------------------------------------------------------------
|
---|
229 | {
|
---|
230 | *this = inSpec;
|
---|
231 | } // nsFilePath::nsFilePath
|
---|
232 |
|
---|
233 | //----------------------------------------------------------------------------------------
|
---|
234 | void nsFilePath::operator = (const nsFileSpec& inSpec)
|
---|
235 | //----------------------------------------------------------------------------------------
|
---|
236 | {
|
---|
237 | mPath = inSpec.mPath;
|
---|
238 | nsFileSpecHelpers::NativeToUnix(mPath);
|
---|
239 | } // nsFilePath::operator =
|
---|
240 |
|
---|
241 | //----------------------------------------------------------------------------------------
|
---|
242 | void nsFileSpec::SetLeafName(const char* inLeafName)
|
---|
243 | //----------------------------------------------------------------------------------------
|
---|
244 | {
|
---|
245 | NS_ASSERTION(inLeafName, "Attempt to SetLeafName with a null string");
|
---|
246 | mPath.LeafReplace('\\', inLeafName);
|
---|
247 | } // nsFileSpec::SetLeafName
|
---|
248 |
|
---|
249 | //----------------------------------------------------------------------------------------
|
---|
250 | char* nsFileSpec::GetLeafName() const
|
---|
251 | //----------------------------------------------------------------------------------------
|
---|
252 | {
|
---|
253 | return mPath.GetLeaf('\\');
|
---|
254 | } // nsFileSpec::GetLeafName
|
---|
255 |
|
---|
256 | //----------------------------------------------------------------------------------------
|
---|
257 | PRBool nsFileSpec::Exists() const
|
---|
258 | //----------------------------------------------------------------------------------------
|
---|
259 | {
|
---|
260 | struct stat st;
|
---|
261 | return !mPath.IsEmpty() && 0 == stat(nsNSPRPath(*this), &st);
|
---|
262 | } // nsFileSpec::Exists
|
---|
263 |
|
---|
264 | //----------------------------------------------------------------------------------------
|
---|
265 | void nsFileSpec::GetModDate(TimeStamp& outStamp) const
|
---|
266 | //----------------------------------------------------------------------------------------
|
---|
267 | {
|
---|
268 | struct stat st;
|
---|
269 | if (!mPath.IsEmpty() && stat(nsNSPRPath(*this), &st) == 0)
|
---|
270 | outStamp = st.st_mtime;
|
---|
271 | else
|
---|
272 | outStamp = 0;
|
---|
273 | } // nsFileSpec::GetModDate
|
---|
274 |
|
---|
275 | //----------------------------------------------------------------------------------------
|
---|
276 | PRUint32 nsFileSpec::GetFileSize() const
|
---|
277 | //----------------------------------------------------------------------------------------
|
---|
278 | {
|
---|
279 | struct stat st;
|
---|
280 | if (!mPath.IsEmpty() && stat(nsNSPRPath(*this), &st) == 0)
|
---|
281 | return (PRUint32)st.st_size;
|
---|
282 | return 0;
|
---|
283 | } // nsFileSpec::GetFileSize
|
---|
284 |
|
---|
285 | //----------------------------------------------------------------------------------------
|
---|
286 | PRBool nsFileSpec::IsFile() const
|
---|
287 | //----------------------------------------------------------------------------------------
|
---|
288 | {
|
---|
289 | struct stat st;
|
---|
290 | #ifdef XP_OS2
|
---|
291 | return !mPath.IsEmpty() && 0 == stat(nsNSPRPath(*this), &st) && ( S_IFREG & st.st_mode);
|
---|
292 | #else
|
---|
293 | return !mPath.IsEmpty() && 0 == stat(nsNSPRPath(*this), &st) && (_S_IFREG & st.st_mode);
|
---|
294 | #endif
|
---|
295 | } // nsFileSpec::IsFile
|
---|
296 |
|
---|
297 | //----------------------------------------------------------------------------------------
|
---|
298 | PRBool nsFileSpec::IsDirectory() const
|
---|
299 | //----------------------------------------------------------------------------------------
|
---|
300 | {
|
---|
301 | struct stat st;
|
---|
302 | #ifdef XP_OS2
|
---|
303 | return !mPath.IsEmpty() && 0 == stat(nsNSPRPath(*this), &st) && ( S_IFDIR & st.st_mode);
|
---|
304 | #else
|
---|
305 | return !mPath.IsEmpty() && 0 == stat(nsNSPRPath(*this), &st) && (_S_IFDIR & st.st_mode);
|
---|
306 | #endif
|
---|
307 | } // nsFileSpec::IsDirectory
|
---|
308 |
|
---|
309 | //----------------------------------------------------------------------------------------
|
---|
310 | PRBool nsFileSpec::IsHidden() const
|
---|
311 | //----------------------------------------------------------------------------------------
|
---|
312 | {
|
---|
313 | PRBool hidden = PR_FALSE;
|
---|
314 | if (!mPath.IsEmpty())
|
---|
315 | {
|
---|
316 | #ifdef XP_OS2
|
---|
317 | FILESTATUS3 fs3;
|
---|
318 | APIRET rc;
|
---|
319 |
|
---|
320 | rc = DosQueryPathInfo( mPath,
|
---|
321 | FIL_STANDARD,
|
---|
322 | &fs3,
|
---|
323 | sizeof fs3);
|
---|
324 | if(!rc)
|
---|
325 | hidden = fs3.attrFile & FILE_HIDDEN ? PR_TRUE : PR_FALSE;
|
---|
326 | #else
|
---|
327 | DWORD attr = GetFileAttributes(mPath);
|
---|
328 | if (FILE_ATTRIBUTE_HIDDEN & attr)
|
---|
329 | hidden = PR_TRUE;
|
---|
330 | #endif
|
---|
331 | }
|
---|
332 | return hidden;
|
---|
333 | }
|
---|
334 | // nsFileSpec::IsHidden
|
---|
335 |
|
---|
336 | //----------------------------------------------------------------------------------------
|
---|
337 | PRBool nsFileSpec::IsSymlink() const
|
---|
338 | //----------------------------------------------------------------------------------------
|
---|
339 | {
|
---|
340 | #ifdef XP_OS2
|
---|
341 | return PR_FALSE; // No symlinks on OS/2
|
---|
342 | #else
|
---|
343 | HRESULT hres;
|
---|
344 | IShellLink* psl;
|
---|
345 |
|
---|
346 | PRBool isSymlink = PR_FALSE;
|
---|
347 |
|
---|
348 | CoInitialize(NULL);
|
---|
349 | // Get a pointer to the IShellLink interface.
|
---|
350 | hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl);
|
---|
351 | if (SUCCEEDED(hres))
|
---|
352 | {
|
---|
353 | IPersistFile* ppf;
|
---|
354 |
|
---|
355 | // Get a pointer to the IPersistFile interface.
|
---|
356 | hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);
|
---|
357 |
|
---|
358 | if (SUCCEEDED(hres))
|
---|
359 | {
|
---|
360 | WORD wsz[MAX_PATH];
|
---|
361 | // Ensure that the string is Unicode.
|
---|
362 | MultiByteToWideChar(CP_ACP, 0, mPath, -1, wsz, MAX_PATH);
|
---|
363 |
|
---|
364 | // Load the shortcut.
|
---|
365 | hres = ppf->Load(wsz, STGM_READ);
|
---|
366 | if (SUCCEEDED(hres))
|
---|
367 | {
|
---|
368 | isSymlink = PR_TRUE;
|
---|
369 | }
|
---|
370 |
|
---|
371 | // Release the pointer to the IPersistFile interface.
|
---|
372 | ppf->Release();
|
---|
373 | }
|
---|
374 |
|
---|
375 | // Release the pointer to the IShellLink interface.
|
---|
376 | psl->Release();
|
---|
377 | }
|
---|
378 |
|
---|
379 | CoUninitialize();
|
---|
380 |
|
---|
381 | return isSymlink;
|
---|
382 | #endif
|
---|
383 | }
|
---|
384 |
|
---|
385 |
|
---|
386 | //----------------------------------------------------------------------------------------
|
---|
387 | nsresult nsFileSpec::ResolveSymlink(PRBool& wasSymlink)
|
---|
388 | //----------------------------------------------------------------------------------------
|
---|
389 | {
|
---|
390 | #ifdef XP_OS2
|
---|
391 | return NS_OK; // no symlinks on OS/2
|
---|
392 | #else
|
---|
393 | wasSymlink = PR_FALSE; // assume failure
|
---|
394 |
|
---|
395 | if (Exists())
|
---|
396 | return NS_OK;
|
---|
397 |
|
---|
398 |
|
---|
399 | HRESULT hres;
|
---|
400 | IShellLink* psl;
|
---|
401 |
|
---|
402 | CoInitialize(NULL);
|
---|
403 |
|
---|
404 | // Get a pointer to the IShellLink interface.
|
---|
405 | hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl);
|
---|
406 | if (SUCCEEDED(hres))
|
---|
407 | {
|
---|
408 | IPersistFile* ppf;
|
---|
409 |
|
---|
410 | // Get a pointer to the IPersistFile interface.
|
---|
411 | hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);
|
---|
412 |
|
---|
413 | if (SUCCEEDED(hres))
|
---|
414 | {
|
---|
415 | WORD wsz[MAX_PATH];
|
---|
416 | // Ensure that the string is Unicode.
|
---|
417 | MultiByteToWideChar(CP_ACP, 0, mPath, -1, wsz, MAX_PATH);
|
---|
418 |
|
---|
419 | // Load the shortcut.
|
---|
420 | hres = ppf->Load(wsz, STGM_READ);
|
---|
421 | if (SUCCEEDED(hres))
|
---|
422 | {
|
---|
423 | wasSymlink = PR_TRUE;
|
---|
424 |
|
---|
425 | // Resolve the link.
|
---|
426 | hres = psl->Resolve(nsnull, SLR_NO_UI );
|
---|
427 | if (SUCCEEDED(hres))
|
---|
428 | {
|
---|
429 | char szGotPath[MAX_PATH];
|
---|
430 | WIN32_FIND_DATA wfd;
|
---|
431 |
|
---|
432 | // Get the path to the link target.
|
---|
433 | hres = psl->GetPath( szGotPath, MAX_PATH, &wfd, SLGP_UNCPRIORITY );
|
---|
434 |
|
---|
435 | if (SUCCEEDED(hres))
|
---|
436 | {
|
---|
437 | // Here we modify the nsFileSpec;
|
---|
438 | mPath = szGotPath;
|
---|
439 | mError = NS_OK;
|
---|
440 | }
|
---|
441 | }
|
---|
442 | }
|
---|
443 | else {
|
---|
444 | // It wasn't a shortcut. Oh well. Leave it like it was.
|
---|
445 | hres = 0;
|
---|
446 | }
|
---|
447 |
|
---|
448 | // Release the pointer to the IPersistFile interface.
|
---|
449 | ppf->Release();
|
---|
450 | }
|
---|
451 | // Release the pointer to the IShellLink interface.
|
---|
452 | psl->Release();
|
---|
453 | }
|
---|
454 |
|
---|
455 | CoUninitialize();
|
---|
456 |
|
---|
457 | if (SUCCEEDED(hres))
|
---|
458 | return NS_OK;
|
---|
459 |
|
---|
460 | return NS_FILE_FAILURE;
|
---|
461 | #endif
|
---|
462 | }
|
---|
463 |
|
---|
464 |
|
---|
465 |
|
---|
466 | //----------------------------------------------------------------------------------------
|
---|
467 | void nsFileSpec::GetParent(nsFileSpec& outSpec) const
|
---|
468 | //----------------------------------------------------------------------------------------
|
---|
469 | {
|
---|
470 | outSpec.mPath = mPath;
|
---|
471 | char* chars = (char*)outSpec.mPath;
|
---|
472 | chars[outSpec.mPath.Length() - 1] = '\0'; // avoid trailing separator, if any
|
---|
473 | char* cp = strrchr(chars, '\\');
|
---|
474 | if (cp++)
|
---|
475 | outSpec.mPath.SetLength(cp - chars); // truncate.
|
---|
476 | } // nsFileSpec::GetParent
|
---|
477 |
|
---|
478 | //----------------------------------------------------------------------------------------
|
---|
479 | void nsFileSpec::operator += (const char* inRelativePath)
|
---|
480 | //----------------------------------------------------------------------------------------
|
---|
481 | {
|
---|
482 | NS_ASSERTION(inRelativePath, "Attempt to do += with a null string");
|
---|
483 |
|
---|
484 | if (!inRelativePath || mPath.IsEmpty())
|
---|
485 | return;
|
---|
486 |
|
---|
487 | if (mPath[mPath.Length() - 1] == '\\')
|
---|
488 | mPath += "x";
|
---|
489 | else
|
---|
490 | mPath += "\\x";
|
---|
491 |
|
---|
492 | // If it's a (unix) relative path, make it native
|
---|
493 | nsSimpleCharString dosPath = inRelativePath;
|
---|
494 | nsFileSpecHelpers::UnixToNative(dosPath);
|
---|
495 | SetLeafName(dosPath);
|
---|
496 | } // nsFileSpec::operator +=
|
---|
497 |
|
---|
498 | //----------------------------------------------------------------------------------------
|
---|
499 | void nsFileSpec::CreateDirectory(int /*mode*/)
|
---|
500 | //----------------------------------------------------------------------------------------
|
---|
501 | {
|
---|
502 | // Note that mPath is canonical!
|
---|
503 | if (!mPath.IsEmpty())
|
---|
504 | #ifdef XP_OS2
|
---|
505 | // OS2TODO - vacpp complains about mkdir but PR_MkDir should be ok?
|
---|
506 | PR_MkDir(nsNSPRPath(*this), PR_CREATE_FILE);
|
---|
507 | #else
|
---|
508 | mkdir(nsNSPRPath(*this));
|
---|
509 | #endif
|
---|
510 | } // nsFileSpec::CreateDirectory
|
---|
511 |
|
---|
512 | //----------------------------------------------------------------------------------------
|
---|
513 | void nsFileSpec::Delete(PRBool inRecursive) const
|
---|
514 | //----------------------------------------------------------------------------------------
|
---|
515 | {
|
---|
516 | if (IsDirectory())
|
---|
517 | {
|
---|
518 | if (inRecursive)
|
---|
519 | {
|
---|
520 | for (nsDirectoryIterator i(*this, PR_FALSE); i.Exists(); i++)
|
---|
521 | {
|
---|
522 | nsFileSpec& child = i.Spec();
|
---|
523 | child.Delete(inRecursive);
|
---|
524 | }
|
---|
525 | }
|
---|
526 | #ifdef XP_OS2
|
---|
527 | // OS2TODO - vacpp complains if use rmdir but PR_RmDir should be ok?
|
---|
528 | PR_RmDir(nsNSPRPath(*this));
|
---|
529 | #else
|
---|
530 | rmdir(nsNSPRPath(*this));
|
---|
531 | #endif
|
---|
532 | }
|
---|
533 | else if (!mPath.IsEmpty())
|
---|
534 | {
|
---|
535 | remove(nsNSPRPath(*this));
|
---|
536 | }
|
---|
537 | } // nsFileSpec::Delete
|
---|
538 |
|
---|
539 |
|
---|
540 | //----------------------------------------------------------------------------------------
|
---|
541 | void nsFileSpec::RecursiveCopy(nsFileSpec newDir) const
|
---|
542 | //----------------------------------------------------------------------------------------
|
---|
543 | {
|
---|
544 | if (IsDirectory())
|
---|
545 | {
|
---|
546 | if (!(newDir.Exists()))
|
---|
547 | {
|
---|
548 | newDir.CreateDirectory();
|
---|
549 | }
|
---|
550 |
|
---|
551 | for (nsDirectoryIterator i(*this, PR_FALSE); i.Exists(); i++)
|
---|
552 | {
|
---|
553 | nsFileSpec& child = i.Spec();
|
---|
554 |
|
---|
555 | if (child.IsDirectory())
|
---|
556 | {
|
---|
557 | nsFileSpec tmpDirSpec(newDir);
|
---|
558 |
|
---|
559 | char *leafname = child.GetLeafName();
|
---|
560 | tmpDirSpec += leafname;
|
---|
561 | nsCRT::free(leafname);
|
---|
562 |
|
---|
563 | child.RecursiveCopy(tmpDirSpec);
|
---|
564 | }
|
---|
565 | else
|
---|
566 | {
|
---|
567 | child.RecursiveCopy(newDir);
|
---|
568 | }
|
---|
569 | }
|
---|
570 | }
|
---|
571 | else if (!mPath.IsEmpty())
|
---|
572 | {
|
---|
573 | nsFileSpec& filePath = (nsFileSpec&) *this;
|
---|
574 |
|
---|
575 | if (!(newDir.Exists()))
|
---|
576 | {
|
---|
577 | newDir.CreateDirectory();
|
---|
578 | }
|
---|
579 |
|
---|
580 | filePath.CopyToDir(newDir);
|
---|
581 | }
|
---|
582 | } // nsFileSpec::RecursiveCopy
|
---|
583 |
|
---|
584 | //----------------------------------------------------------------------------------------
|
---|
585 | nsresult
|
---|
586 | nsFileSpec::Truncate(PRInt32 aNewFileLength) const
|
---|
587 | //----------------------------------------------------------------------------------------
|
---|
588 | {
|
---|
589 | #ifdef XP_OS2
|
---|
590 | APIRET rc;
|
---|
591 | HFILE hFile;
|
---|
592 | ULONG actionTaken;
|
---|
593 |
|
---|
594 | rc = DosOpen(mPath,
|
---|
595 | &hFile,
|
---|
596 | &actionTaken,
|
---|
597 | 0,
|
---|
598 | FILE_NORMAL,
|
---|
599 | OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
|
---|
600 | OPEN_SHARE_DENYREADWRITE | OPEN_ACCESS_READWRITE,
|
---|
601 | NULL);
|
---|
602 |
|
---|
603 | if (rc != NO_ERROR)
|
---|
604 | return NS_FILE_FAILURE;
|
---|
605 |
|
---|
606 | rc = DosSetFileSize(hFile, aNewFileLength);
|
---|
607 |
|
---|
608 | if (rc == NO_ERROR)
|
---|
609 | DosClose(hFile);
|
---|
610 | else
|
---|
611 | goto error;
|
---|
612 | #else
|
---|
613 | DWORD status;
|
---|
614 | HANDLE hFile;
|
---|
615 |
|
---|
616 | // Leave it to Microsoft to open an existing file with a function
|
---|
617 | // named "CreateFile".
|
---|
618 | hFile = CreateFile(mPath,
|
---|
619 | GENERIC_WRITE,
|
---|
620 | FILE_SHARE_READ,
|
---|
621 | NULL,
|
---|
622 | OPEN_EXISTING,
|
---|
623 | FILE_ATTRIBUTE_NORMAL,
|
---|
624 | NULL);
|
---|
625 | if (hFile == INVALID_HANDLE_VALUE)
|
---|
626 | return NS_FILE_FAILURE;
|
---|
627 |
|
---|
628 | // Seek to new, desired end of file
|
---|
629 | status = SetFilePointer(hFile, aNewFileLength, NULL, FILE_BEGIN);
|
---|
630 | if (status == 0xffffffff)
|
---|
631 | goto error;
|
---|
632 |
|
---|
633 | // Truncate file at current cursor position
|
---|
634 | if (!SetEndOfFile(hFile))
|
---|
635 | goto error;
|
---|
636 |
|
---|
637 | if (!CloseHandle(hFile))
|
---|
638 | return NS_FILE_FAILURE;
|
---|
639 | #endif
|
---|
640 |
|
---|
641 | return NS_OK;
|
---|
642 |
|
---|
643 | error:
|
---|
644 | #ifdef XP_OS2
|
---|
645 | DosClose(hFile);
|
---|
646 | #else
|
---|
647 | CloseHandle(hFile);
|
---|
648 | #endif
|
---|
649 | return NS_FILE_FAILURE;
|
---|
650 |
|
---|
651 | } // nsFileSpec::Truncate
|
---|
652 |
|
---|
653 | //----------------------------------------------------------------------------------------
|
---|
654 | nsresult nsFileSpec::Rename(const char* inNewName)
|
---|
655 | //----------------------------------------------------------------------------------------
|
---|
656 | {
|
---|
657 | NS_ASSERTION(inNewName, "Attempt to Rename with a null string");
|
---|
658 |
|
---|
659 | // This function should not be used to move a file on disk.
|
---|
660 | if (strchr(inNewName, '/'))
|
---|
661 | return NS_FILE_FAILURE;
|
---|
662 |
|
---|
663 | char* oldPath = nsCRT::strdup(mPath);
|
---|
664 |
|
---|
665 | SetLeafName(inNewName);
|
---|
666 |
|
---|
667 | if (PR_Rename(oldPath, mPath) != NS_OK)
|
---|
668 | {
|
---|
669 | // Could not rename, set back to the original.
|
---|
670 | mPath = oldPath;
|
---|
671 | return NS_FILE_FAILURE;
|
---|
672 | }
|
---|
673 |
|
---|
674 | nsCRT::free(oldPath);
|
---|
675 |
|
---|
676 | return NS_OK;
|
---|
677 | } // nsFileSpec::Rename
|
---|
678 |
|
---|
679 | //----------------------------------------------------------------------------------------
|
---|
680 | nsresult nsFileSpec::CopyToDir(const nsFileSpec& inParentDirectory) const
|
---|
681 | //----------------------------------------------------------------------------------------
|
---|
682 | {
|
---|
683 | // We can only copy into a directory, and (for now) can not copy entire directories
|
---|
684 | if (inParentDirectory.IsDirectory() && (! IsDirectory() ) )
|
---|
685 | {
|
---|
686 | char *leafname = GetLeafName();
|
---|
687 | nsSimpleCharString destPath(inParentDirectory.GetCString());
|
---|
688 | destPath += "\\";
|
---|
689 | destPath += leafname;
|
---|
690 | nsCRT::free(leafname);
|
---|
691 |
|
---|
692 | // CopyFile returns non-zero if succeeds
|
---|
693 | #ifdef XP_OS2
|
---|
694 | APIRET rc;
|
---|
695 | PRBool copyOK;
|
---|
696 |
|
---|
697 | rc = DosCopy(GetCString(), (PSZ)destPath, DCPY_EXISTING);
|
---|
698 |
|
---|
699 | if (rc == NO_ERROR)
|
---|
700 | copyOK = PR_TRUE;
|
---|
701 | else
|
---|
702 | copyOK = PR_FALSE;
|
---|
703 | #else
|
---|
704 | int copyOK = CopyFile(GetCString(), destPath, PR_TRUE);
|
---|
705 | #endif
|
---|
706 | if (copyOK)
|
---|
707 | return NS_OK;
|
---|
708 | }
|
---|
709 | return NS_FILE_FAILURE;
|
---|
710 | } // nsFileSpec::CopyToDir
|
---|
711 |
|
---|
712 | //----------------------------------------------------------------------------------------
|
---|
713 | nsresult nsFileSpec::MoveToDir(const nsFileSpec& inNewParentDirectory)
|
---|
714 | //----------------------------------------------------------------------------------------
|
---|
715 | {
|
---|
716 | // We can only copy into a directory, and (for now) can not copy entire directories
|
---|
717 | if (inNewParentDirectory.IsDirectory() && (! IsDirectory() ) )
|
---|
718 | {
|
---|
719 | char *leafname = GetLeafName();
|
---|
720 | nsSimpleCharString destPath(inNewParentDirectory.GetCString());
|
---|
721 | destPath += "\\";
|
---|
722 | destPath += leafname;
|
---|
723 | nsCRT::free(leafname);
|
---|
724 |
|
---|
725 | if (DosMove(GetCString(), destPath) == NO_ERROR)
|
---|
726 | {
|
---|
727 | *this = inNewParentDirectory + GetLeafName();
|
---|
728 | return NS_OK;
|
---|
729 | }
|
---|
730 |
|
---|
731 | }
|
---|
732 | return NS_FILE_FAILURE;
|
---|
733 | } // nsFileSpec::MoveToDir
|
---|
734 |
|
---|
735 | //----------------------------------------------------------------------------------------
|
---|
736 | nsresult nsFileSpec::Execute(const char* inArgs ) const
|
---|
737 | //----------------------------------------------------------------------------------------
|
---|
738 | {
|
---|
739 | if (!IsDirectory())
|
---|
740 | {
|
---|
741 | #ifdef XP_OS2
|
---|
742 | nsresult result = NS_FILE_FAILURE;
|
---|
743 |
|
---|
744 | if (!mPath.IsEmpty())
|
---|
745 | {
|
---|
746 | nsSimpleCharString fileNameWithArgs = mPath + " " + inArgs;
|
---|
747 | result = NS_FILE_RESULT(system(fileNameWithArgs));
|
---|
748 | }
|
---|
749 | return result;
|
---|
750 | #else
|
---|
751 | nsSimpleCharString fileNameWithArgs = "\"";
|
---|
752 | fileNameWithArgs += mPath + "\" " + inArgs;
|
---|
753 | int execResult = WinExec( fileNameWithArgs, SW_NORMAL );
|
---|
754 | if (execResult > 31)
|
---|
755 | return NS_OK;
|
---|
756 | #endif
|
---|
757 | }
|
---|
758 | return NS_FILE_FAILURE;
|
---|
759 | } // nsFileSpec::Execute
|
---|
760 |
|
---|
761 |
|
---|
762 | //----------------------------------------------------------------------------------------
|
---|
763 | PRInt64 nsFileSpec::GetDiskSpaceAvailable() const
|
---|
764 | //----------------------------------------------------------------------------------------
|
---|
765 | {
|
---|
766 | PRInt64 nBytes = 0;
|
---|
767 | ULONG ulDriveNo = toupper(mPath[0]) + 1 - 'A';
|
---|
768 | FSALLOCATE fsAllocate;
|
---|
769 | APIRET rc = DosQueryFSInfo(ulDriveNo,
|
---|
770 | FSIL_ALLOC,
|
---|
771 | &fsAllocate,
|
---|
772 | sizeof(fsAllocate));
|
---|
773 |
|
---|
774 | if (rc == NO_ERROR) {
|
---|
775 | nBytes = fsAllocate.cUnitAvail;
|
---|
776 | nBytes *= fsAllocate.cSectorUnit;
|
---|
777 | nBytes *= fsAllocate.cbSector;
|
---|
778 | }
|
---|
779 |
|
---|
780 | return nBytes;
|
---|
781 | }
|
---|
782 |
|
---|
783 |
|
---|
784 |
|
---|
785 | //========================================================================================
|
---|
786 | // nsDirectoryIterator
|
---|
787 | //========================================================================================
|
---|
788 |
|
---|
789 | //----------------------------------------------------------------------------------------
|
---|
790 | nsDirectoryIterator::nsDirectoryIterator(const nsFileSpec& inDirectory, PRBool resolveSymlink)
|
---|
791 | //----------------------------------------------------------------------------------------
|
---|
792 | : mCurrent(inDirectory)
|
---|
793 | , mDir(nsnull)
|
---|
794 | , mStarting(inDirectory)
|
---|
795 | , mExists(PR_FALSE)
|
---|
796 | , mResoveSymLinks(resolveSymlink)
|
---|
797 | {
|
---|
798 | mDir = PR_OpenDir(inDirectory);
|
---|
799 | mCurrent += "dummy";
|
---|
800 | mStarting += "dummy";
|
---|
801 | ++(*this);
|
---|
802 | } // nsDirectoryIterator::nsDirectoryIterator
|
---|
803 |
|
---|
804 | //----------------------------------------------------------------------------------------
|
---|
805 | nsDirectoryIterator::~nsDirectoryIterator()
|
---|
806 | //----------------------------------------------------------------------------------------
|
---|
807 | {
|
---|
808 | if (mDir)
|
---|
809 | PR_CloseDir(mDir);
|
---|
810 | } // nsDirectoryIterator::nsDirectoryIterator
|
---|
811 |
|
---|
812 | //----------------------------------------------------------------------------------------
|
---|
813 | nsDirectoryIterator& nsDirectoryIterator::operator ++ ()
|
---|
814 | //----------------------------------------------------------------------------------------
|
---|
815 | {
|
---|
816 | mExists = PR_FALSE;
|
---|
817 | if (!mDir)
|
---|
818 | return *this;
|
---|
819 | PRDirEntry* entry = PR_ReadDir(mDir, PR_SKIP_BOTH); // Ignore '.' && '..'
|
---|
820 | if (entry)
|
---|
821 | {
|
---|
822 | mExists = PR_TRUE;
|
---|
823 | mCurrent = mStarting;
|
---|
824 | mCurrent.SetLeafName(entry->name);
|
---|
825 | if (mResoveSymLinks)
|
---|
826 | {
|
---|
827 | PRBool ignore;
|
---|
828 | mCurrent.ResolveSymlink(ignore);
|
---|
829 | }
|
---|
830 | }
|
---|
831 | return *this;
|
---|
832 | } // nsDirectoryIterator::operator ++
|
---|
833 |
|
---|
834 | //----------------------------------------------------------------------------------------
|
---|
835 | nsDirectoryIterator& nsDirectoryIterator::operator -- ()
|
---|
836 | //----------------------------------------------------------------------------------------
|
---|
837 | {
|
---|
838 | return ++(*this); // can't do it backwards.
|
---|
839 | } // nsDirectoryIterator::operator --
|
---|
840 |
|
---|