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-02-25 23:49:47 +03:00
|
|
|
#include "nsError.h"
|
1999-01-07 02:38:21 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-02-25 23:49:47 +03:00
|
|
|
void nsFileSpecHelpers::Canonify(char*& ioPath, PRBool inMakeDirs)
|
1999-01-07 02:38:21 +03:00
|
|
|
// Canonify, make absolute, and check whether directories exist
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
if (!ioPath)
|
|
|
|
return;
|
|
|
|
if (inMakeDirs)
|
|
|
|
{
|
|
|
|
const mode_t mode = 0700;
|
|
|
|
nsFileSpecHelpers::MakeAllDirectories(ioPath, mode);
|
|
|
|
}
|
|
|
|
char buffer[MAXPATHLEN];
|
|
|
|
errno = 0;
|
|
|
|
*buffer = '\0';
|
|
|
|
char* canonicalPath = realpath(ioPath, buffer);
|
|
|
|
if (!canonicalPath)
|
|
|
|
{
|
|
|
|
// Linux's realpath() is pathetically buggy. If the reason for the nil
|
|
|
|
// result is just that the leaf does not exist, strip the leaf off,
|
|
|
|
// process that, and then add the leaf back.
|
|
|
|
char* allButLeaf = nsFileSpecHelpers::StringDup(ioPath);
|
|
|
|
if (!allButLeaf)
|
|
|
|
return;
|
|
|
|
char* lastSeparator = strrchr(allButLeaf, '/');
|
|
|
|
if (lastSeparator)
|
|
|
|
{
|
|
|
|
*lastSeparator = '\0';
|
|
|
|
canonicalPath = realpath(allButLeaf, buffer);
|
|
|
|
strcat(buffer, "/");
|
|
|
|
// Add back the leaf
|
|
|
|
strcat(buffer, ++lastSeparator);
|
|
|
|
}
|
|
|
|
delete [] allButLeaf;
|
|
|
|
}
|
|
|
|
if (!canonicalPath && *ioPath != '/' && !inMakeDirs)
|
|
|
|
{
|
|
|
|
// Well, if it's a relative path, hack it ourselves.
|
|
|
|
canonicalPath = realpath(".", buffer);
|
|
|
|
if (canonicalPath)
|
|
|
|
{
|
|
|
|
strcat(canonicalPath, "/");
|
|
|
|
strcat(canonicalPath, ioPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (canonicalPath)
|
|
|
|
nsFileSpecHelpers::StringAssign(ioPath, canonicalPath);
|
|
|
|
} // 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-01-07 02:38:21 +03:00
|
|
|
nsFileSpecHelpers::LeafReplace(mPath, '/', 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-01-07 02:38:21 +03:00
|
|
|
return nsFileSpecHelpers::GetLeaf(mPath, '/');
|
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;
|
|
|
|
return 0 == stat(mPath, &st);
|
1999-02-25 23:49:47 +03:00
|
|
|
} // nsFileSpec::Exists
|
1998-12-09 11:47:30 +03:00
|
|
|
|
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;
|
|
|
|
return 0 == stat(mPath, &st) && 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;
|
|
|
|
return 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-02-25 23:49:47 +03:00
|
|
|
void nsFileSpec::GetParent(nsFileSpec& outSpec) const
|
1999-01-07 02:38:21 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
nsFileSpecHelpers::StringAssign(outSpec.mPath, mPath);
|
|
|
|
char* cp = strrchr(outSpec.mPath, '/');
|
|
|
|
if (cp)
|
|
|
|
*cp = '\0';
|
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
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
if (!inRelativePath || !mPath)
|
|
|
|
return;
|
|
|
|
|
1999-02-25 23:49:47 +03:00
|
|
|
char endChar = mPath[strlen(mPath) - 1];
|
|
|
|
if (endChar == '/')
|
|
|
|
nsFileSpecHelpers::ReallocCat(mPath, "x");
|
|
|
|
else
|
|
|
|
nsFileSpecHelpers::ReallocCat(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!
|
|
|
|
mkdir(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-02-25 23:49:47 +03:00
|
|
|
for (nsDirectoryIterator i(*this); 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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
remove(mPath);
|
1999-02-25 23:49:47 +03: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;
|
|
|
|
|
|
|
|
if (PR_Rename(mPath, inNewName) != 0)
|
|
|
|
{
|
|
|
|
return NS_FILE_FAILURE;
|
|
|
|
}
|
|
|
|
SetLeafName(inNewName);
|
|
|
|
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();
|
|
|
|
char* destPath = nsFileSpecHelpers::StringDup(inParentDirectory, ( strlen(inParentDirectory) + 1 + strlen(leafname) ) );
|
|
|
|
strcat(destPath, "/");
|
|
|
|
strcat(destPath, leafname);
|
|
|
|
delete [] leafname;
|
|
|
|
|
|
|
|
result = NS_FILE_RESULT(CrudeFileCopy(*this, destPath));
|
|
|
|
|
|
|
|
delete [] destPath;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} // nsFileSpec::Copy
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsresult nsFileSpec::Move(const nsFileSpec& inNewParentDirectory) const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
// We can only copy into a directory, and (for now) can not copy entire directories
|
|
|
|
nsresult result = NS_FILE_FAILURE;
|
|
|
|
|
|
|
|
if (inNewParentDirectory.IsDirectory() && (! IsDirectory() ) )
|
|
|
|
{
|
|
|
|
char *leafname = GetLeafName();
|
|
|
|
char* destPath
|
|
|
|
= nsFileSpecHelpers::StringDup(
|
|
|
|
inNewParentDirectory,
|
|
|
|
strlen(inNewParentDirectory) + 1 + strlen(leafname));
|
|
|
|
strcat(destPath, "/");
|
|
|
|
strcat(destPath, leafname);
|
|
|
|
delete [] leafname;
|
|
|
|
|
|
|
|
result = NS_FILE_RESULT(CrudeFileCopy(*this, destPath));
|
|
|
|
if (result == NS_OK)
|
|
|
|
{
|
1999-02-26 20:56:44 +03:00
|
|
|
// cast to fix const-ness
|
|
|
|
((nsFileSpec*)this)->Delete(PR_FALSE);
|
1999-02-25 23:49:47 +03:00
|
|
|
}
|
|
|
|
delete [] destPath;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsresult nsFileSpec::Execute(const char* inArgs ) const
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
nsresult result = NS_FILE_FAILURE;
|
|
|
|
|
|
|
|
if (! IsDirectory())
|
|
|
|
{
|
|
|
|
char* fileNameWithArgs
|
|
|
|
= nsFileSpecHelpers::StringDup(mPath, strlen(mPath) + 1 + strlen(inArgs));
|
|
|
|
strcat(fileNameWithArgs, " ");
|
|
|
|
strcat(fileNameWithArgs, inArgs);
|
|
|
|
|
|
|
|
result = NS_FILE_RESULT(system(fileNameWithArgs));
|
|
|
|
delete [] fileNameWithArgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
} // nsFileSpec::Execute
|
1999-01-07 02:38:21 +03:00
|
|
|
|
|
|
|
//========================================================================================
|
|
|
|
// nsDirectoryIterator
|
|
|
|
//========================================================================================
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsDirectoryIterator::nsDirectoryIterator(
|
1999-02-25 23:49:47 +03:00
|
|
|
const nsFileSpec& inDirectory
|
1999-01-07 02:38:21 +03:00
|
|
|
, int inIterateDirection)
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
: mCurrent(inDirectory)
|
|
|
|
, mDir(nsnull)
|
1999-02-26 22:47:48 +03:00
|
|
|
, mExists(PR_FALSE)
|
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-01-07 02:38:21 +03:00
|
|
|
mCurrent.SetLeafName(entry->d_name);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
} // nsDirectoryIterator::operator ++
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
nsDirectoryIterator& nsDirectoryIterator::operator -- ()
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
return ++(*this); // can't do it backwards.
|
|
|
|
} // nsDirectoryIterator::operator --
|