1998-12-08 05:15:50 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1999-01-07 02:38:21 +03:00
|
|
|
// This file is included by nsFileSpec.cpp, and includes the Unix-specific
|
|
|
|
// implementations.
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/param.h>
|
1999-01-08 00:03:42 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <dirent.h>
|
1999-01-07 02:38:21 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
1999-03-13 09:38:57 +03:00
|
|
|
#include <limits.h>
|
1999-02-25 23:49:47 +03:00
|
|
|
#include "nsError.h"
|
1999-05-08 10:24:27 +04:00
|
|
|
#include "prio.h" /* for PR_Rename */
|
1999-01-07 02:38:21 +03:00
|
|
|
|
1999-07-31 23:33:36 +04:00
|
|
|
#if defined(_SCO_DS)
|
1999-04-10 05:13:02 +04:00
|
|
|
#define _SVID3 /* for statvfs.h */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_STATVFS_H
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_VFS_H
|
|
|
|
#include <sys/vfs.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_STATFS_H
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_MOUNT_H
|
|
|
|
#include <sys/mount.h>
|
1999-09-05 04:49:01 +04:00
|
|
|
#ifdef __bsdi__
|
|
|
|
#undef Free
|
|
|
|
#endif
|
1999-04-10 05:13:02 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_STATVFS
|
|
|
|
#define STATFS statvfs
|
1999-03-13 09:38:57 +03:00
|
|
|
#else
|
1999-04-10 05:13:02 +04:00
|
|
|
#define STATFS statfs
|
1999-03-13 09:38:57 +03:00
|
|
|
#endif
|
1999-07-13 02:36:24 +04:00
|
|
|
|
|
|
|
#ifndef MAXPATHLEN
|
|
|
|
#define MAXPATHLEN 1024 /* Guessing this is okay. Works for SCO. */
|
|
|
|
#endif
|
1999-04-10 05:13:02 +04:00
|
|
|
|
1999-07-22 03:15:15 +04:00
|
|
|
#if defined(__QNX__)
|
|
|
|
#include <unix.h> /* for realpath */
|
|
|
|
#define f_bavail f_bfree
|
|
|
|
#endif
|
|
|
|
|
1999-04-10 05:13:02 +04:00
|
|
|
#if defined(SUNOS4)
|
|
|
|
extern "C" int statfs(char *, struct statfs *);
|
1999-03-13 09:38:57 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(OSF1)
|
|
|
|
extern "C" int statvfs(const char *, struct statvfs *);
|
|
|
|
#endif
|
1999-04-10 05:13:02 +04:00
|
|
|
|
1999-04-07 00:10:50 +04:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-20 02:09:39 +03:00
|
|
|
void nsFileSpecHelpers::Canonify(nsSimpleCharString& ioPath, PRBool inMakeDirs)
|
1999-01-07 02:38:21 +03:00
|
|
|
// Canonify, make absolute, and check whether directories exist
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
if (ioPath.IsEmpty())
|
1999-01-07 02:38:21 +03:00
|
|
|
return;
|
|
|
|
if (inMakeDirs)
|
|
|
|
{
|
|
|
|
const mode_t mode = 0700;
|
1999-09-13 09:09:53 +04:00
|
|
|
nsFileSpecHelpers::MakeAllDirectories((char*)ioPath, mode);
|
1999-01-07 02:38:21 +03:00
|
|
|
}
|
1999-09-04 03:44:59 +04:00
|
|
|
|
|
|
|
errno = 0; // needed?
|
|
|
|
|
|
|
|
if (ioPath[0] != '/')
|
1999-01-07 02:38:21 +03:00
|
|
|
{
|
1999-09-04 03:44:59 +04:00
|
|
|
// the ioPath that was passed in is relative. We must cat it to the cwd.
|
|
|
|
char buffer[MAXPATHLEN];
|
|
|
|
|
|
|
|
(void) getcwd(buffer, MAXPATHLEN);
|
|
|
|
|
|
|
|
strcat(buffer, "/");
|
1999-09-13 09:09:53 +04:00
|
|
|
strcat(buffer, (char *)ioPath);
|
1999-09-04 03:44:59 +04:00
|
|
|
|
|
|
|
ioPath = buffer;
|
1999-01-07 02:38:21 +03:00
|
|
|
}
|
|
|
|
} // nsFileSpecHelpers::Canonify
|
1998-12-08 05:15:50 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
void nsFileSpec::SetLeafName(const char* inLeafName)
|
1998-12-08 05:15:50 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
mPath.LeafReplace('/', inLeafName);
|
1999-02-25 23:49:47 +03:00
|
|
|
} // nsFileSpec::SetLeafName
|
1998-12-08 05:15:50 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
char* nsFileSpec::GetLeafName() const
|
1998-12-08 05:15:50 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
return mPath.GetLeaf('/');
|
1999-02-25 23:49:47 +03:00
|
|
|
} // nsFileSpec::GetLeafName
|
1998-12-08 05:15:50 +03:00
|
|
|
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
PRBool nsFileSpec::Exists() const
|
1998-12-09 11:47:30 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-01-07 02:38:21 +03:00
|
|
|
struct stat st;
|
1999-05-04 00:29:01 +04:00
|
|
|
return !mPath.IsEmpty() && 0 == stat(mPath, &st);
|
1999-02-25 23:49:47 +03:00
|
|
|
} // nsFileSpec::Exists
|
1998-12-09 11:47:30 +03:00
|
|
|
|
1999-03-11 00:02:58 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsFileSpec::GetModDate(TimeStamp& outStamp) const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
struct stat st;
|
1999-05-04 00:29:01 +04:00
|
|
|
if (!mPath.IsEmpty() && stat(mPath, &st) == 0)
|
1999-03-11 00:02:58 +03:00
|
|
|
outStamp = st.st_mtime;
|
|
|
|
else
|
|
|
|
outStamp = 0;
|
|
|
|
} // nsFileSpec::GetModDate
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
PRUint32 nsFileSpec::GetFileSize() const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
struct stat st;
|
1999-05-04 00:29:01 +04:00
|
|
|
if (!mPath.IsEmpty() && stat(mPath, &st) == 0)
|
1999-03-11 00:02:58 +03:00
|
|
|
return (PRUint32)st.st_size;
|
|
|
|
return 0;
|
|
|
|
} // nsFileSpec::GetFileSize
|
|
|
|
|
1999-01-07 02:38:21 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
PRBool nsFileSpec::IsFile() const
|
1999-01-07 02:38:21 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
struct stat st;
|
1999-05-04 03:14:28 +04:00
|
|
|
return !mPath.IsEmpty() && stat(mPath, &st) == 0 && S_ISREG(st.st_mode);
|
1999-02-25 23:49:47 +03:00
|
|
|
} // nsFileSpec::IsFile
|
1999-01-07 02:38:21 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
PRBool nsFileSpec::IsDirectory() const
|
1999-01-07 02:38:21 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
struct stat st;
|
1999-05-04 00:29:01 +04:00
|
|
|
return !mPath.IsEmpty() && 0 == stat(mPath, &st) && S_ISDIR(st.st_mode);
|
1999-02-25 23:49:47 +03:00
|
|
|
} // nsFileSpec::IsDirectory
|
1999-01-07 02:38:21 +03:00
|
|
|
|
1999-07-21 21:56:01 +04:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
PRBool nsFileSpec::IsHidden() const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-08-24 22:32:06 +04:00
|
|
|
PRBool hidden = PR_FALSE;
|
1999-07-21 21:56:01 +04:00
|
|
|
char *leafname = GetLeafName();
|
|
|
|
if (nsnull != leafname)
|
|
|
|
{
|
1999-08-30 15:40:29 +04:00
|
|
|
// rjc: don't return ".", "..", or any file/directory that begins with a "."
|
|
|
|
|
|
|
|
/* if ((!strcmp(leafname, ".")) || (!strcmp(leafname, ".."))) */
|
|
|
|
if (leafname[0] == '.')
|
1999-07-21 21:56:01 +04:00
|
|
|
{
|
1999-08-24 22:32:06 +04:00
|
|
|
hidden = PR_TRUE;
|
1999-07-21 21:56:01 +04:00
|
|
|
}
|
|
|
|
nsCRT::free(leafname);
|
|
|
|
}
|
|
|
|
return hidden;
|
|
|
|
} // nsFileSpec::IsHidden
|
|
|
|
|
1999-07-27 02:09:42 +04:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
PRBool nsFileSpec::IsSymlink() const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if (!mPath.IsEmpty() && stat(mPath, &st) == 0 && S_ISLNK(st.st_mode))
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
return PR_FALSE;
|
|
|
|
} // nsFileSpec::IsSymlink
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsresult nsFileSpec::ResolveSymlink(PRBool& wasAliased)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
wasAliased = PR_FALSE;
|
|
|
|
|
|
|
|
char resolvedPath[MAXPATHLEN];
|
1999-09-13 09:09:53 +04:00
|
|
|
int charCount = readlink((char *)mPath, (char*)&resolvedPath, MAXPATHLEN);
|
1999-07-27 02:09:42 +04:00
|
|
|
if (0 < charCount)
|
|
|
|
{
|
|
|
|
if (MAXPATHLEN > charCount)
|
|
|
|
resolvedPath[charCount] = '\0';
|
|
|
|
|
|
|
|
wasAliased = PR_TRUE;
|
1999-07-30 00:14:42 +04:00
|
|
|
|
|
|
|
/* if it's not an absolute path,
|
|
|
|
replace the leaf with what got resolved */
|
|
|
|
if (resolvedPath[0] != '/') {
|
1999-07-29 08:31:04 +04:00
|
|
|
SetLeafName(resolvedPath);
|
1999-07-30 00:14:42 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mPath = (char*)&resolvedPath;
|
|
|
|
}
|
|
|
|
|
1999-09-13 09:09:53 +04:00
|
|
|
char* canonicalPath = realpath((char *)mPath, resolvedPath);
|
1999-07-30 00:14:42 +04:00
|
|
|
NS_ASSERTION(canonicalPath, "realpath failed");
|
|
|
|
if (canonicalPath) {
|
|
|
|
mPath = (char*)&resolvedPath;
|
1999-07-29 08:31:04 +04:00
|
|
|
}
|
|
|
|
else {
|
1999-07-30 00:14:42 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-07-29 08:31:04 +04:00
|
|
|
}
|
1999-07-27 02:09:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
} // nsFileSpec::ResolveSymlink
|
|
|
|
|
|
|
|
|
1999-01-07 02:38:21 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
void nsFileSpec::GetParent(nsFileSpec& outSpec) const
|
1999-01-07 02:38:21 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
outSpec.mPath = mPath;
|
1999-04-20 23:09:22 +04:00
|
|
|
char* chars = (char*)outSpec.mPath;
|
|
|
|
chars[outSpec.mPath.Length() - 1] = '\0'; // avoid trailing separator, if any
|
|
|
|
char* cp = strrchr(chars, '/');
|
1999-03-20 02:09:39 +03:00
|
|
|
if (cp++)
|
1999-04-20 23:31:11 +04:00
|
|
|
outSpec.mPath.SetLength(cp - chars); // truncate.
|
1999-02-25 23:49:47 +03:00
|
|
|
} // nsFileSpec::GetParent
|
1999-01-07 02:38:21 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
void nsFileSpec::operator += (const char* inRelativePath)
|
1999-01-07 02:38:21 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
if (!inRelativePath || mPath.IsEmpty())
|
1999-01-07 02:38:21 +03:00
|
|
|
return;
|
|
|
|
|
1999-09-13 09:09:53 +04:00
|
|
|
char endChar = mPath[(int)(strlen((char *)mPath) - 1)];
|
1999-02-25 23:49:47 +03:00
|
|
|
if (endChar == '/')
|
1999-03-20 02:09:39 +03:00
|
|
|
mPath += "x";
|
1999-02-25 23:49:47 +03:00
|
|
|
else
|
1999-03-20 02:09:39 +03:00
|
|
|
mPath += "/x";
|
1999-01-07 02:38:21 +03:00
|
|
|
SetLeafName(inRelativePath);
|
1999-02-25 23:49:47 +03:00
|
|
|
} // nsFileSpec::operator +=
|
1999-01-07 02:38:21 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
void nsFileSpec::CreateDirectory(int mode)
|
1999-01-07 02:38:21 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
// Note that mPath is canonical!
|
1999-05-04 00:29:01 +04:00
|
|
|
if (mPath.IsEmpty())
|
|
|
|
return;
|
1999-09-13 09:09:53 +04:00
|
|
|
mkdir((char *)mPath, mode);
|
1999-02-25 23:49:47 +03:00
|
|
|
} // nsFileSpec::CreateDirectory
|
1999-01-07 02:38:21 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-28 04:36:48 +03:00
|
|
|
void nsFileSpec::Delete(PRBool inRecursive) const
|
1999-01-07 02:38:21 +03:00
|
|
|
// To check if this worked, call Exists() afterwards, see?
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
if (IsDirectory())
|
|
|
|
{
|
|
|
|
if (inRecursive)
|
|
|
|
{
|
1999-07-27 02:09:42 +04:00
|
|
|
for (nsDirectoryIterator i(*this, PR_FALSE); i.Exists(); i++)
|
1999-01-07 02:38:21 +03:00
|
|
|
{
|
1999-02-25 23:49:47 +03:00
|
|
|
nsFileSpec& child = (nsFileSpec&)i;
|
1999-01-07 02:38:21 +03:00
|
|
|
child.Delete(inRecursive);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rmdir(mPath);
|
|
|
|
}
|
1999-05-04 00:29:01 +04:00
|
|
|
else if (!mPath.IsEmpty())
|
1999-01-07 02:38:21 +03:00
|
|
|
remove(mPath);
|
1999-02-25 23:49:47 +03:00
|
|
|
} // nsFileSpec::Delete
|
|
|
|
|
1999-07-05 05:39:42 +04:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void nsFileSpec::RecursiveCopy(nsFileSpec newDir) const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
if (IsDirectory())
|
|
|
|
{
|
|
|
|
if (!(newDir.Exists()))
|
|
|
|
{
|
|
|
|
newDir.CreateDirectory();
|
|
|
|
}
|
|
|
|
|
1999-07-27 02:09:42 +04:00
|
|
|
for (nsDirectoryIterator i(*this, PR_FALSE); i.Exists(); i++)
|
1999-07-05 05:39:42 +04:00
|
|
|
{
|
|
|
|
nsFileSpec& child = (nsFileSpec&)i;
|
|
|
|
|
|
|
|
if (child.IsDirectory())
|
|
|
|
{
|
|
|
|
nsFileSpec tmpDirSpec(newDir);
|
|
|
|
|
|
|
|
char *leafname = child.GetLeafName();
|
|
|
|
tmpDirSpec += leafname;
|
|
|
|
nsCRT::free(leafname);
|
|
|
|
|
|
|
|
child.RecursiveCopy(tmpDirSpec);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
child.RecursiveCopy(newDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!mPath.IsEmpty())
|
|
|
|
{
|
|
|
|
nsFileSpec& filePath = (nsFileSpec&) *this;
|
|
|
|
|
|
|
|
if (!(newDir.Exists()))
|
|
|
|
{
|
|
|
|
newDir.CreateDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath.Copy(newDir);
|
|
|
|
}
|
|
|
|
} // nsFileSpec::RecursiveCopy
|
|
|
|
|
|
|
|
|
1999-02-25 23:49:47 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsresult nsFileSpec::Rename(const char* inNewName)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
// This function should not be used to move a file on disk.
|
1999-05-04 00:29:01 +04:00
|
|
|
if (mPath.IsEmpty() || strchr(inNewName, '/'))
|
1999-02-25 23:49:47 +03:00
|
|
|
return NS_FILE_FAILURE;
|
|
|
|
|
1999-09-13 09:09:53 +04:00
|
|
|
char* oldPath = nsCRT::strdup((char *)mPath);
|
1999-03-21 09:22:45 +03:00
|
|
|
|
|
|
|
SetLeafName(inNewName);
|
|
|
|
|
1999-09-13 09:09:53 +04:00
|
|
|
if (PR_Rename(oldPath, (char *)mPath) != NS_OK)
|
1999-02-25 23:49:47 +03:00
|
|
|
{
|
1999-03-21 09:22:45 +03:00
|
|
|
// Could not rename, set back to the original.
|
|
|
|
mPath = oldPath;
|
1999-02-25 23:49:47 +03:00
|
|
|
return NS_FILE_FAILURE;
|
|
|
|
}
|
1999-03-21 09:22:45 +03:00
|
|
|
|
1999-04-26 23:05:00 +04:00
|
|
|
nsCRT::free(oldPath);
|
1999-03-21 09:22:45 +03:00
|
|
|
|
1999-02-25 23:49:47 +03:00
|
|
|
return NS_OK;
|
|
|
|
} // nsFileSpec::Rename
|
|
|
|
|
1999-02-28 04:36:48 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
static int CrudeFileCopy(const char* in, const char* out)
|
1999-02-28 04:36:48 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
{
|
|
|
|
struct stat in_stat;
|
|
|
|
int stat_result = -1;
|
|
|
|
|
|
|
|
char buf [1024];
|
|
|
|
FILE *ifp, *ofp;
|
|
|
|
int rbytes, wbytes;
|
|
|
|
|
|
|
|
if (!in || !out)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
stat_result = stat (in, &in_stat);
|
|
|
|
|
|
|
|
ifp = fopen (in, "r");
|
|
|
|
if (!ifp)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ofp = fopen (out, "w");
|
|
|
|
if (!ofp)
|
|
|
|
{
|
|
|
|
fclose (ifp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((rbytes = fread (buf, 1, sizeof(buf), ifp)) > 0)
|
|
|
|
{
|
|
|
|
while (rbytes > 0)
|
|
|
|
{
|
|
|
|
if ( (wbytes = fwrite (buf, 1, rbytes, ofp)) < 0 )
|
|
|
|
{
|
|
|
|
fclose (ofp);
|
|
|
|
fclose (ifp);
|
|
|
|
unlink(out);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
rbytes -= wbytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose (ofp);
|
|
|
|
fclose (ifp);
|
|
|
|
|
|
|
|
if (stat_result == 0)
|
|
|
|
chmod (out, in_stat.st_mode & 0777);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} // nsFileSpec::Rename
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsresult nsFileSpec::Copy(const nsFileSpec& inParentDirectory) const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
// We can only copy into a directory, and (for now) can not copy entire directories
|
|
|
|
nsresult result = NS_FILE_FAILURE;
|
|
|
|
|
|
|
|
if (inParentDirectory.IsDirectory() && (! IsDirectory() ) )
|
|
|
|
{
|
|
|
|
char *leafname = GetLeafName();
|
1999-03-20 02:09:39 +03:00
|
|
|
nsSimpleCharString destPath(inParentDirectory.GetCString());
|
|
|
|
destPath += "/";
|
|
|
|
destPath += leafname;
|
1999-04-26 23:05:00 +04:00
|
|
|
nsCRT::free(leafname);
|
1999-09-13 09:09:53 +04:00
|
|
|
result = NS_FILE_RESULT(CrudeFileCopy(GetCString(), (char *)destPath));
|
1999-02-25 23:49:47 +03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} // nsFileSpec::Copy
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-03-21 09:22:45 +03:00
|
|
|
nsresult nsFileSpec::Move(const nsFileSpec& inNewParentDirectory)
|
1999-02-25 23:49:47 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
// We can only copy into a directory, and (for now) can not copy entire directories
|
|
|
|
nsresult result = NS_FILE_FAILURE;
|
|
|
|
|
1999-03-20 02:09:39 +03:00
|
|
|
if (inNewParentDirectory.IsDirectory() && !IsDirectory())
|
1999-02-25 23:49:47 +03:00
|
|
|
{
|
|
|
|
char *leafname = GetLeafName();
|
1999-03-20 02:09:39 +03:00
|
|
|
nsSimpleCharString destPath(inNewParentDirectory.GetCString());
|
|
|
|
destPath += "/";
|
|
|
|
destPath += leafname;
|
1999-04-26 23:05:00 +04:00
|
|
|
nsCRT::free(leafname);
|
1999-02-25 23:49:47 +03:00
|
|
|
|
1999-09-13 09:09:53 +04:00
|
|
|
result = NS_FILE_RESULT(CrudeFileCopy(GetCString(), (char*)destPath));
|
1999-02-25 23:49:47 +03:00
|
|
|
if (result == NS_OK)
|
1999-03-20 02:09:39 +03:00
|
|
|
{
|
|
|
|
// cast to fix const-ness
|
1999-03-21 09:22:45 +03:00
|
|
|
((nsFileSpec*)this)->Delete(PR_FALSE);
|
|
|
|
|
|
|
|
*this = inNewParentDirectory + GetLeafName();
|
|
|
|
}
|
1999-02-25 23:49:47 +03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsresult nsFileSpec::Execute(const char* inArgs ) const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
nsresult result = NS_FILE_FAILURE;
|
|
|
|
|
1999-05-04 00:29:01 +04:00
|
|
|
if (!mPath.IsEmpty() && !IsDirectory())
|
1999-02-25 23:49:47 +03:00
|
|
|
{
|
1999-03-20 02:09:39 +03:00
|
|
|
nsSimpleCharString fileNameWithArgs = mPath + " " + inArgs;
|
1999-09-13 09:09:53 +04:00
|
|
|
result = NS_FILE_RESULT(system((char *)fileNameWithArgs));
|
1999-02-25 23:49:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
} // nsFileSpec::Execute
|
1999-01-07 02:38:21 +03:00
|
|
|
|
1999-03-13 09:38:57 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
1999-09-11 00:03:56 +04:00
|
|
|
PRInt64 nsFileSpec::GetDiskSpaceAvailable() const
|
1999-03-13 09:38:57 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-09-11 00:58:46 +04:00
|
|
|
PRInt64 bytes; /* XXX dougt needs to fix this */
|
|
|
|
LL_I2L(bytes , LONG_MAX); // initialize to all the space in the world?
|
|
|
|
|
|
|
|
|
1999-07-27 02:09:42 +04:00
|
|
|
#if defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_STATVFS_H)
|
|
|
|
|
1999-03-13 09:38:57 +03:00
|
|
|
char curdir [MAXPATHLEN];
|
1999-03-20 02:09:39 +03:00
|
|
|
if (mPath.IsEmpty())
|
1999-03-13 09:38:57 +03:00
|
|
|
{
|
|
|
|
(void) getcwd(curdir, MAXPATHLEN);
|
|
|
|
if (!curdir)
|
1999-09-09 23:37:29 +04:00
|
|
|
return bytes; /* hope for the best as we did in cheddar */
|
1999-03-13 09:38:57 +03:00
|
|
|
}
|
|
|
|
else
|
1999-03-20 02:09:39 +03:00
|
|
|
sprintf(curdir, "%.200s", (const char*)mPath);
|
1999-03-13 09:38:57 +03:00
|
|
|
|
|
|
|
struct STATFS fs_buf;
|
1999-07-22 05:31:43 +04:00
|
|
|
#if defined(__QNX__) && !defined(HAVE_STATVFS) /* Maybe this should be handled differently? */
|
1999-07-22 03:15:15 +04:00
|
|
|
if (STATFS(curdir, &fs_buf, 0, 0) < 0)
|
|
|
|
#else
|
1999-03-13 09:38:57 +03:00
|
|
|
if (STATFS(curdir, &fs_buf) < 0)
|
1999-07-22 03:15:15 +04:00
|
|
|
#endif
|
1999-09-09 23:37:29 +04:00
|
|
|
return bytes; /* hope for the best as we did in cheddar */
|
1999-03-13 09:38:57 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG_DISK_SPACE
|
|
|
|
printf("DiskSpaceAvailable: %d bytes\n",
|
|
|
|
fs_buf.f_bsize * (fs_buf.f_bavail - 1));
|
|
|
|
#endif
|
1999-07-27 02:09:42 +04:00
|
|
|
|
1999-09-09 23:37:29 +04:00
|
|
|
LL_I2L( bytes, (fs_buf.f_bsize * (fs_buf.f_bavail - 1) ) );
|
|
|
|
return bytes;
|
1999-07-27 02:09:42 +04:00
|
|
|
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
** This platform doesn't have statfs or statvfs, so we don't have much
|
|
|
|
** choice but to "hope for the best as we did in cheddar".
|
|
|
|
*/
|
1999-09-09 23:37:29 +04:00
|
|
|
return bytes;
|
1999-07-27 02:09:42 +04:00
|
|
|
#endif /* HAVE_SYS_STATFS_H or HAVE_SYS_STATVFS_H */
|
|
|
|
|
1999-03-13 09:38:57 +03:00
|
|
|
} // nsFileSpec::GetDiskSpace()
|
|
|
|
|
1999-01-07 02:38:21 +03:00
|
|
|
//========================================================================================
|
|
|
|
// nsDirectoryIterator
|
|
|
|
//========================================================================================
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-07-27 02:09:42 +04:00
|
|
|
nsDirectoryIterator::nsDirectoryIterator(const nsFileSpec& inDirectory, PRBool resolveSymLinks)
|
1999-01-07 02:38:21 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
: mCurrent(inDirectory)
|
1999-02-26 22:47:48 +03:00
|
|
|
, mExists(PR_FALSE)
|
1999-07-27 02:09:42 +04:00
|
|
|
, mResoveSymLinks(resolveSymLinks)
|
1999-07-30 00:14:42 +04:00
|
|
|
, mStarting(inDirectory)
|
|
|
|
, mDir(nsnull)
|
|
|
|
|
1999-01-07 02:38:21 +03:00
|
|
|
{
|
1999-07-29 08:31:04 +04:00
|
|
|
mStarting += "sysygy"; // save off the starting directory
|
1999-01-07 02:38:21 +03:00
|
|
|
mCurrent += "sysygy"; // prepare the path for SetLeafName
|
|
|
|
mDir = opendir((const char*)nsFilePath(inDirectory));
|
|
|
|
++(*this);
|
|
|
|
} // nsDirectoryIterator::nsDirectoryIterator
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsDirectoryIterator::~nsDirectoryIterator()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
if (mDir)
|
|
|
|
closedir(mDir);
|
|
|
|
} // nsDirectoryIterator::nsDirectoryIterator
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsDirectoryIterator& nsDirectoryIterator::operator ++ ()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-02-26 22:47:48 +03:00
|
|
|
mExists = PR_FALSE;
|
1999-01-07 02:38:21 +03:00
|
|
|
if (!mDir)
|
|
|
|
return *this;
|
|
|
|
char* dot = ".";
|
|
|
|
char* dotdot = "..";
|
|
|
|
struct dirent* entry = readdir(mDir);
|
|
|
|
if (entry && strcmp(entry->d_name, dot) == 0)
|
|
|
|
entry = readdir(mDir);
|
|
|
|
if (entry && strcmp(entry->d_name, dotdot) == 0)
|
|
|
|
entry = readdir(mDir);
|
|
|
|
if (entry)
|
|
|
|
{
|
1999-02-26 22:47:48 +03:00
|
|
|
mExists = PR_TRUE;
|
1999-07-29 08:31:04 +04:00
|
|
|
mCurrent = mStarting; // restore mCurrent to be the starting directory. ResolveSymlink() may have taken us to another directory
|
1999-01-07 02:38:21 +03:00
|
|
|
mCurrent.SetLeafName(entry->d_name);
|
1999-07-27 02:09:42 +04:00
|
|
|
if (mResoveSymLinks)
|
|
|
|
{
|
|
|
|
PRBool ignore;
|
|
|
|
mCurrent.ResolveSymlink(ignore);
|
|
|
|
}
|
1999-01-07 02:38:21 +03:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
} // nsDirectoryIterator::operator ++
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsDirectoryIterator& nsDirectoryIterator::operator -- ()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
return ++(*this); // can't do it backwards.
|
|
|
|
} // nsDirectoryIterator::operator --
|