1999-01-26 23:49:33 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
1998-12-08 02:15:50 +00:00
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.0 (the "NPL"); you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
|
1998-12-08 22:45:42 +00:00
|
|
|
// This file is included by nsFileSpec.cp, and includes the Windows-specific
|
1998-12-08 02:15:50 +00:00
|
|
|
// implementations.
|
|
|
|
|
1999-01-07 01:46:22 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <direct.h>
|
1999-03-13 07:07:33 +00:00
|
|
|
#include <limits.h>
|
1999-01-26 23:49:33 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "prio.h"
|
1999-02-25 20:49:47 +00:00
|
|
|
#include "nsError.h"
|
|
|
|
|
|
|
|
#include "windows.h"
|
|
|
|
|
|
|
|
#ifdef UNICODE
|
|
|
|
#define CreateDirectoryW CreateDirectory
|
|
|
|
#else
|
|
|
|
#define CreateDirectoryA CreateDirectory
|
|
|
|
#endif
|
1999-01-06 23:38:21 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-19 23:09:39 +00:00
|
|
|
void nsFileSpecHelpers::Canonify(nsSimpleCharString& ioPath, PRBool inMakeDirs)
|
1999-01-26 23:49:33 +00:00
|
|
|
// Canonify, make absolute, and check whether directories exist. This
|
|
|
|
// takes a (possibly relative) native path and converts it into a
|
|
|
|
// fully qualified native path.
|
1999-01-06 23:38:21 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-19 23:09:39 +00:00
|
|
|
if (ioPath.IsEmpty())
|
|
|
|
return;
|
1999-03-09 22:33:36 +00:00
|
|
|
|
1999-05-12 00:17:53 +00:00
|
|
|
NS_ASSERTION(strchr((const char*)ioPath, '/') == 0,
|
|
|
|
"This smells like a Unix path. Native path expected! "
|
|
|
|
"Please fix.");
|
|
|
|
if (inMakeDirs)
|
1999-03-19 23:09:39 +00:00
|
|
|
{
|
|
|
|
const int mode = 0700;
|
|
|
|
nsSimpleCharString unixStylePath = ioPath;
|
|
|
|
nsFileSpecHelpers::NativeToUnix(unixStylePath);
|
|
|
|
nsFileSpecHelpers::MakeAllDirectories((const char*)unixStylePath, mode);
|
|
|
|
}
|
|
|
|
char buffer[_MAX_PATH];
|
|
|
|
errno = 0;
|
|
|
|
*buffer = '\0';
|
|
|
|
char* canonicalPath = _fullpath(buffer, ioPath, _MAX_PATH);
|
|
|
|
|
|
|
|
NS_ASSERTION( canonicalPath[0] != '\0', "Uh oh...couldn't convert" );
|
|
|
|
if (canonicalPath[0] == '\0')
|
|
|
|
return;
|
|
|
|
|
|
|
|
ioPath = canonicalPath;
|
|
|
|
} // nsFileSpecHelpers::Canonify
|
1999-01-26 23:49:33 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-19 23:09:39 +00:00
|
|
|
void nsFileSpecHelpers::UnixToNative(nsSimpleCharString& ioPath)
|
1999-01-26 23:49:33 +00:00
|
|
|
// This just does string manipulation. It doesn't check reality, or canonify, or
|
|
|
|
// anything
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
// Allow for relative or absolute. We can do this in place, because the
|
|
|
|
// native path is never longer.
|
|
|
|
|
1999-03-19 23:09:39 +00:00
|
|
|
if (ioPath.IsEmpty())
|
1999-01-26 23:49:33 +00:00
|
|
|
return;
|
|
|
|
|
1999-03-19 23:09:39 +00:00
|
|
|
// Strip initial slash for an absolute path
|
|
|
|
char* src = (char*)ioPath;
|
|
|
|
if (*src == '/')
|
|
|
|
{
|
|
|
|
// Since it was an absolute path, check for the drive letter
|
|
|
|
char* colonPointer = src + 2;
|
|
|
|
if (strstr(src, "|/") == colonPointer)
|
|
|
|
*colonPointer = ':';
|
|
|
|
// allocate new string by copying from ioPath[1]
|
|
|
|
nsSimpleCharString temp = src + 1;
|
|
|
|
ioPath = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
src = (char*)ioPath;
|
1999-01-26 23:49:33 +00:00
|
|
|
|
|
|
|
// Convert '/' to '\'.
|
1999-03-19 23:09:39 +00:00
|
|
|
while (*++src)
|
1999-01-26 23:49:33 +00:00
|
|
|
{
|
1999-03-19 23:09:39 +00:00
|
|
|
if (*src == '/')
|
|
|
|
*src = '\\';
|
1999-01-26 23:49:33 +00:00
|
|
|
}
|
1999-03-19 23:09:39 +00:00
|
|
|
} // nsFileSpecHelpers::UnixToNative
|
1999-01-26 23:49:33 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-19 23:09:39 +00:00
|
|
|
void nsFileSpecHelpers::NativeToUnix(nsSimpleCharString& ioPath)
|
1999-01-26 23:49:33 +00:00
|
|
|
// This just does string manipulation. It doesn't check reality, or canonify, or
|
|
|
|
// anything. The unix path is longer, so we can't do it in place.
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-19 23:09:39 +00:00
|
|
|
if (ioPath.IsEmpty())
|
1999-01-26 23:49:33 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Convert the drive-letter separator, if present
|
1999-03-19 23:09:39 +00:00
|
|
|
nsSimpleCharString temp("/");
|
1999-01-26 23:49:33 +00:00
|
|
|
|
1999-03-19 23:09:39 +00:00
|
|
|
char* cp = (char*)ioPath + 1;
|
|
|
|
if (strstr(cp, ":\\") == cp)
|
1999-01-26 23:49:33 +00:00
|
|
|
*cp = '|'; // absolute path
|
1999-03-19 23:09:39 +00:00
|
|
|
else
|
|
|
|
temp[0] = '\0'; // relative path
|
1999-01-26 23:49:33 +00:00
|
|
|
|
|
|
|
// Convert '\' to '/'
|
|
|
|
for (; *cp; cp++)
|
|
|
|
{
|
|
|
|
if (*cp == '\\')
|
|
|
|
*cp = '/';
|
|
|
|
}
|
|
|
|
// Add the slash in front.
|
1999-03-19 23:09:39 +00:00
|
|
|
temp += ioPath;
|
|
|
|
ioPath = temp;
|
1999-01-06 23:38:21 +00:00
|
|
|
}
|
|
|
|
|
1998-12-08 02:15:50 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
nsFileSpec::nsFileSpec(const nsFilePath& inPath)
|
1998-12-08 02:15:50 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
*this = inPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
void nsFileSpec::operator = (const nsFilePath& inPath)
|
1998-12-08 02:15:50 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-19 23:09:39 +00:00
|
|
|
mPath = (const char*)inPath;
|
1999-01-26 23:49:33 +00:00
|
|
|
nsFileSpecHelpers::UnixToNative(mPath);
|
1999-02-25 20:49:47 +00:00
|
|
|
mError = NS_OK;
|
|
|
|
} // nsFileSpec::operator =
|
1998-12-08 02:15:50 +00:00
|
|
|
|
1998-12-09 01:04:53 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
nsFilePath::nsFilePath(const nsFileSpec& inSpec)
|
1998-12-09 01:04:53 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1998-12-09 08:47:30 +00:00
|
|
|
*this = inSpec;
|
|
|
|
} // nsFilePath::nsFilePath
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
void nsFilePath::operator = (const nsFileSpec& inSpec)
|
1998-12-09 08:47:30 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-19 23:09:39 +00:00
|
|
|
mPath = inSpec.mPath;
|
1999-01-26 23:49:33 +00:00
|
|
|
nsFileSpecHelpers::NativeToUnix(mPath);
|
1998-12-09 08:47:30 +00:00
|
|
|
} // nsFilePath::operator =
|
1998-12-09 01:04:53 +00:00
|
|
|
|
1998-12-08 02:15:50 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
void nsFileSpec::SetLeafName(const char* inLeafName)
|
1998-12-08 02:15:50 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-19 23:09:39 +00:00
|
|
|
mPath.LeafReplace('\\', inLeafName);
|
1999-02-25 20:49:47 +00:00
|
|
|
} // nsFileSpec::SetLeafName
|
1998-12-08 02:15:50 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
char* nsFileSpec::GetLeafName() const
|
1998-12-08 02:15:50 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-19 23:09:39 +00:00
|
|
|
return mPath.GetLeaf('\\');
|
1999-02-25 20:49:47 +00:00
|
|
|
} // nsFileSpec::GetLeafName
|
1998-12-08 02:15:50 +00:00
|
|
|
|
1998-12-09 01:04:53 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
PRBool nsFileSpec::Exists() const
|
1998-12-09 01:04:53 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-01-06 23:38:21 +00:00
|
|
|
struct stat st;
|
1999-05-08 01:18:23 +00:00
|
|
|
return !mPath.IsEmpty() && 0 == stat(nsNSPRPath(*this), &st);
|
1999-02-25 20:49:47 +00:00
|
|
|
} // nsFileSpec::Exists
|
1998-12-09 01:04:53 +00:00
|
|
|
|
1999-03-10 21:02:58 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsFileSpec::GetModDate(TimeStamp& outStamp) const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
struct stat st;
|
1999-05-08 01:18:23 +00:00
|
|
|
if (!mPath.IsEmpty() && stat(nsNSPRPath(*this), &st) == 0)
|
1999-03-10 21:02:58 +00:00
|
|
|
outStamp = st.st_mtime;
|
|
|
|
else
|
|
|
|
outStamp = 0;
|
|
|
|
} // nsFileSpec::GetModDate
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
PRUint32 nsFileSpec::GetFileSize() const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
struct stat st;
|
1999-05-08 01:18:23 +00:00
|
|
|
if (!mPath.IsEmpty() && stat(nsNSPRPath(*this), &st) == 0)
|
1999-03-10 21:02:58 +00:00
|
|
|
return (PRUint32)st.st_size;
|
|
|
|
return 0;
|
|
|
|
} // nsFileSpec::GetFileSize
|
|
|
|
|
1999-01-06 23:38:21 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
PRBool nsFileSpec::IsFile() const
|
1999-01-06 23:38:21 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-01-26 23:49:33 +00:00
|
|
|
struct stat st;
|
1999-05-08 01:18:23 +00:00
|
|
|
return !mPath.IsEmpty() && 0 == stat(nsNSPRPath(*this), &st) && (_S_IFREG & st.st_mode);
|
1999-02-25 20:49:47 +00:00
|
|
|
} // nsFileSpec::IsFile
|
1999-01-06 23:38:21 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
PRBool nsFileSpec::IsDirectory() const
|
1999-01-06 23:38:21 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-01-26 23:49:33 +00:00
|
|
|
struct stat st;
|
1999-05-08 01:18:23 +00:00
|
|
|
return !mPath.IsEmpty() && 0 == stat(nsNSPRPath(*this), &st) && (_S_IFDIR & st.st_mode);
|
1999-02-25 20:49:47 +00:00
|
|
|
} // nsFileSpec::IsDirectory
|
1999-01-06 23:38:21 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
void nsFileSpec::GetParent(nsFileSpec& outSpec) const
|
1999-01-06 23:38:21 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-19 23:09:39 +00:00
|
|
|
outSpec.mPath = mPath;
|
1999-04-20 19:09:22 +00:00
|
|
|
char* chars = (char*)outSpec.mPath;
|
|
|
|
chars[outSpec.mPath.Length() - 1] = '\0'; // avoid trailing separator, if any
|
|
|
|
char* cp = strrchr(chars, '\\');
|
|
|
|
if (cp++)
|
1999-04-20 19:31:11 +00:00
|
|
|
outSpec.mPath.SetLength(cp - chars); // truncate.
|
1999-02-25 20:49:47 +00:00
|
|
|
} // nsFileSpec::GetParent
|
1999-01-06 23:38:21 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
void nsFileSpec::operator += (const char* inRelativePath)
|
1999-01-06 23:38:21 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-19 23:09:39 +00:00
|
|
|
if (!inRelativePath || mPath.IsEmpty())
|
1999-01-06 23:38:21 +00:00
|
|
|
return;
|
|
|
|
|
1999-03-19 23:09:39 +00:00
|
|
|
if (mPath[mPath.Length() - 1] == '\\')
|
|
|
|
mPath += "x";
|
1999-02-25 20:49:47 +00:00
|
|
|
else
|
1999-03-19 23:09:39 +00:00
|
|
|
mPath += "\\x";
|
1999-03-13 06:38:57 +00:00
|
|
|
|
|
|
|
// If it's a (unix) relative path, make it native
|
1999-03-19 23:09:39 +00:00
|
|
|
nsSimpleCharString dosPath = inRelativePath;
|
1999-03-13 06:38:57 +00:00
|
|
|
nsFileSpecHelpers::UnixToNative(dosPath);
|
|
|
|
SetLeafName(dosPath);
|
1999-02-25 20:49:47 +00:00
|
|
|
} // nsFileSpec::operator +=
|
1999-01-06 23:38:21 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 20:49:47 +00:00
|
|
|
void nsFileSpec::CreateDirectory(int /*mode*/)
|
1999-01-06 23:38:21 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
// Note that mPath is canonical!
|
1999-05-03 20:29:01 +00:00
|
|
|
if (!mPath.IsEmpty())
|
1999-05-08 01:18:23 +00:00
|
|
|
mkdir(nsNSPRPath(*this));
|
1999-02-25 20:49:47 +00:00
|
|
|
} // nsFileSpec::CreateDirectory
|
1999-01-06 23:38:21 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-28 01:36:48 +00:00
|
|
|
void nsFileSpec::Delete(PRBool inRecursive) const
|
1999-01-06 23:38:21 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-02-28 01:36:48 +00:00
|
|
|
if (IsDirectory())
|
1999-01-06 23:38:21 +00:00
|
|
|
{
|
|
|
|
if (inRecursive)
|
1999-01-26 23:49:33 +00:00
|
|
|
{
|
1999-02-28 01:36:48 +00:00
|
|
|
for (nsDirectoryIterator i(*this); i.Exists(); i++)
|
|
|
|
{
|
|
|
|
nsFileSpec& child = (nsFileSpec&)i;
|
|
|
|
child.Delete(inRecursive);
|
|
|
|
}
|
1999-01-26 23:49:33 +00:00
|
|
|
}
|
1999-05-08 01:18:23 +00:00
|
|
|
rmdir(nsNSPRPath(*this));
|
1999-01-26 23:49:33 +00:00
|
|
|
}
|
1999-05-03 20:29:01 +00:00
|
|
|
else if (!mPath.IsEmpty())
|
1999-01-26 23:49:33 +00:00
|
|
|
{
|
1999-05-08 01:18:23 +00:00
|
|
|
remove(nsNSPRPath(*this));
|
1999-01-26 23:49:33 +00:00
|
|
|
}
|
1999-02-25 20:49:47 +00:00
|
|
|
} // nsFileSpec::Delete
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsresult nsFileSpec::Rename(const char* inNewName)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
// This function should not be used to move a file on disk.
|
|
|
|
if (strchr(inNewName, '/'))
|
|
|
|
return NS_FILE_FAILURE;
|
|
|
|
|
1999-04-26 19:05:00 +00:00
|
|
|
char* oldPath = nsCRT::strdup(mPath);
|
1999-03-21 06:22:45 +00:00
|
|
|
|
|
|
|
SetLeafName(inNewName);
|
|
|
|
|
|
|
|
if (PR_Rename(oldPath, mPath) != NS_OK)
|
1999-02-25 20:49:47 +00:00
|
|
|
{
|
1999-03-21 06:22:45 +00:00
|
|
|
// Could not rename, set back to the original.
|
|
|
|
mPath = oldPath;
|
1999-02-25 20:49:47 +00:00
|
|
|
return NS_FILE_FAILURE;
|
|
|
|
}
|
1999-03-21 06:22:45 +00:00
|
|
|
|
1999-04-26 19:05:00 +00:00
|
|
|
nsCRT::free(oldPath);
|
1999-03-21 06:22:45 +00:00
|
|
|
|
1999-02-25 20:49:47 +00:00
|
|
|
return NS_OK;
|
|
|
|
} // nsFileSpec::Rename
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsresult nsFileSpec::Copy(const nsFileSpec& inParentDirectory) const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
// We can only copy into a directory, and (for now) can not copy entire directories
|
|
|
|
if (inParentDirectory.IsDirectory() && (! IsDirectory() ) )
|
|
|
|
{
|
|
|
|
char *leafname = GetLeafName();
|
1999-03-19 23:09:39 +00:00
|
|
|
nsSimpleCharString destPath(inParentDirectory.GetCString());
|
|
|
|
destPath += "\\";
|
|
|
|
destPath += leafname;
|
1999-04-26 19:05:00 +00:00
|
|
|
nsCRT::free(leafname);
|
1999-02-25 20:49:47 +00:00
|
|
|
|
|
|
|
// CopyFile returns non-zero if succeeds
|
1999-04-26 19:34:39 +00:00
|
|
|
int copyOK = CopyFile(GetCString(), destPath, PR_TRUE);
|
1999-02-25 20:49:47 +00:00
|
|
|
if (copyOK)
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_FILE_FAILURE;
|
|
|
|
} // nsFileSpec::Copy
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-21 06:22:45 +00:00
|
|
|
nsresult nsFileSpec::Move(const nsFileSpec& inNewParentDirectory)
|
1999-02-25 20:49:47 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
// We can only copy into a directory, and (for now) can not copy entire directories
|
1999-03-19 23:09:39 +00:00
|
|
|
if (inNewParentDirectory.IsDirectory() && (! IsDirectory() ) )
|
1999-02-25 20:49:47 +00:00
|
|
|
{
|
|
|
|
char *leafname = GetLeafName();
|
1999-03-19 23:09:39 +00:00
|
|
|
nsSimpleCharString destPath(inNewParentDirectory.GetCString());
|
|
|
|
destPath += "\\";
|
|
|
|
destPath += leafname;
|
1999-04-26 19:05:00 +00:00
|
|
|
nsCRT::free(leafname);
|
1999-02-25 20:49:47 +00:00
|
|
|
|
|
|
|
// MoveFile returns non-zero if succeeds
|
1999-03-19 23:09:39 +00:00
|
|
|
int copyOK = MoveFile(GetCString(), destPath);
|
1999-03-21 06:22:45 +00:00
|
|
|
|
1999-02-25 20:49:47 +00:00
|
|
|
if (copyOK)
|
1999-03-21 06:22:45 +00:00
|
|
|
{
|
|
|
|
*this = inNewParentDirectory + GetLeafName();
|
1999-02-25 20:49:47 +00:00
|
|
|
return NS_OK;
|
1999-03-21 06:22:45 +00:00
|
|
|
}
|
|
|
|
|
1999-02-25 20:49:47 +00:00
|
|
|
}
|
|
|
|
return NS_FILE_FAILURE;
|
|
|
|
} // nsFileSpec::Move
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsresult nsFileSpec::Execute(const char* inArgs ) const
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-19 23:09:39 +00:00
|
|
|
{
|
|
|
|
if (!IsDirectory())
|
1999-02-25 20:49:47 +00:00
|
|
|
{
|
1999-03-19 23:09:39 +00:00
|
|
|
nsSimpleCharString fileNameWithArgs = mPath + " " + inArgs;
|
|
|
|
int execResult = WinExec( fileNameWithArgs, SW_NORMAL );
|
1999-02-25 20:49:47 +00:00
|
|
|
if (execResult > 31)
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_FILE_FAILURE;
|
|
|
|
} // nsFileSpec::Execute
|
1999-01-06 23:38:21 +00:00
|
|
|
|
1999-03-13 06:38:57 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
PRUint32 nsFileSpec::GetDiskSpaceAvailable() const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
char aDrive[_MAX_DRIVE + 2];
|
1999-03-19 23:09:39 +00:00
|
|
|
_splitpath( (const char*)mPath, aDrive, NULL, NULL, NULL);
|
1999-03-13 06:38:57 +00:00
|
|
|
|
|
|
|
if (aDrive[0] == '\0')
|
|
|
|
{
|
|
|
|
// The back end is always trying to pass us paths that look
|
|
|
|
// like /c|/netscape/mail. See if we've got one of them
|
1999-03-19 23:09:39 +00:00
|
|
|
if (mPath.Length() > 2 && mPath[0] == '/' && mPath[2] == '|')
|
1999-03-13 06:38:57 +00:00
|
|
|
{
|
|
|
|
aDrive[0] = mPath[1];
|
|
|
|
aDrive[1] = ':';
|
|
|
|
aDrive[2] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Return bogus large number and hope for the best
|
|
|
|
return ULONG_MAX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
strcat(aDrive, "\\");
|
|
|
|
|
1999-03-13 07:07:33 +00:00
|
|
|
DWORD dwSectorsPerCluster = 0;
|
|
|
|
DWORD dwBytesPerSector = 0;
|
|
|
|
DWORD dwFreeClusters = 0;
|
|
|
|
DWORD dwTotalClusters = 0;
|
1999-03-13 06:38:57 +00:00
|
|
|
if (!GetDiskFreeSpace(aDrive,
|
|
|
|
&dwSectorsPerCluster,
|
|
|
|
&dwBytesPerSector,
|
|
|
|
&dwFreeClusters,
|
|
|
|
&dwTotalClusters))
|
|
|
|
{
|
|
|
|
return ULONG_MAX; // Return bogus large number and hope for the best
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can now figure free disk space.
|
|
|
|
return dwFreeClusters * dwSectorsPerCluster * dwBytesPerSector;
|
|
|
|
} // nsFileSpec::GetDiskSpaceAvailable()
|
|
|
|
|
1999-01-06 23:38:21 +00:00
|
|
|
//========================================================================================
|
|
|
|
// nsDirectoryIterator
|
|
|
|
//========================================================================================
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsDirectoryIterator::nsDirectoryIterator(
|
1999-02-25 20:49:47 +00:00
|
|
|
const nsFileSpec& inDirectory
|
1999-01-06 23:38:21 +00:00
|
|
|
, int inIterateDirection)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
: mCurrent(inDirectory)
|
|
|
|
, mDir(nsnull)
|
1999-02-28 01:36:48 +00:00
|
|
|
, mExists(PR_FALSE)
|
1999-01-06 23:38:21 +00:00
|
|
|
{
|
1999-01-26 23:49:33 +00:00
|
|
|
mDir = PR_OpenDir(inDirectory);
|
|
|
|
mCurrent += "dummy";
|
1999-01-06 23:38:21 +00:00
|
|
|
++(*this);
|
|
|
|
} // nsDirectoryIterator::nsDirectoryIterator
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsDirectoryIterator::~nsDirectoryIterator()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
if (mDir)
|
1999-01-26 23:49:33 +00:00
|
|
|
PR_CloseDir(mDir);
|
1999-01-06 23:38:21 +00:00
|
|
|
} // nsDirectoryIterator::nsDirectoryIterator
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsDirectoryIterator& nsDirectoryIterator::operator ++ ()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-02-28 01:36:48 +00:00
|
|
|
mExists = PR_FALSE;
|
1999-01-06 23:38:21 +00:00
|
|
|
if (!mDir)
|
|
|
|
return *this;
|
1999-02-28 01:36:48 +00:00
|
|
|
PRDirEntry* entry = PR_ReadDir(mDir, PR_SKIP_BOTH); // Ignore '.' && '..'
|
1999-01-06 23:38:21 +00:00
|
|
|
if (entry)
|
1999-01-26 23:49:33 +00:00
|
|
|
{
|
1999-02-28 01:36:48 +00:00
|
|
|
mExists = PR_TRUE;
|
1999-01-26 23:49:33 +00:00
|
|
|
mCurrent.SetLeafName(entry->name);
|
|
|
|
}
|
1999-01-06 23:38:21 +00:00
|
|
|
return *this;
|
|
|
|
} // nsDirectoryIterator::operator ++
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsDirectoryIterator& nsDirectoryIterator::operator -- ()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
return ++(*this); // can't do it backwards.
|
|
|
|
} // nsDirectoryIterator::operator --
|
|
|
|
|