Now adjusting the nsFileSpec after a Move().

Also fixing a bug with Rename().  Now it should successfully take
partial pathnames on Unix and Windows.
This commit is contained in:
dougt%netscape.com 1999-03-21 06:22:45 +00:00
parent bccf315074
commit aae2baf3ab
8 changed files with 76 additions and 20 deletions

View File

@ -458,7 +458,7 @@ class NS_BASE nsFileSpec
return Rename(newName);
}
nsresult Copy(const nsFileSpec& inNewParentDirectory) const;
nsresult Move(const nsFileSpec& inNewParentDirectory) const;
nsresult Move(const nsFileSpec& inNewParentDirectory);
nsresult Execute(const char* args) const;
nsresult Execute(const nsString& args) const
{

View File

@ -816,7 +816,7 @@ nsresult nsFileSpec::Copy(const nsFileSpec& newParentDir) const
} // nsFileSpec::Copy
//----------------------------------------------------------------------------------------
nsresult nsFileSpec::Move(const nsFileSpec& newParentDir) const
nsresult nsFileSpec::Move(const nsFileSpec& newParentDir)
//----------------------------------------------------------------------------------------
{
// We can only move into a directory
@ -827,6 +827,9 @@ nsresult nsFileSpec::Move(const nsFileSpec& newParentDir) const
nsresult result = NS_FILE_RESULT(::FSpMoveRenameCompat(&mSpec,
&newParentDir.mSpec,
const_cast<StringPtr>(GetLeafPName())));
if ( NS_SUCCEEDED(result) )
*this = newParentDir + GetLeafName();
return result;
} // nsFileSpec::Move

View File

@ -231,11 +231,19 @@ nsresult nsFileSpec::Rename(const char* inNewName)
if (strchr(inNewName, '/'))
return NS_FILE_FAILURE;
if (PR_Rename(mPath, inNewName) != 0)
char* oldPath = PL_strdup(mPath);
SetLeafName(inNewName);
if (PR_Rename(oldPath, mPath) != NS_OK)
{
// Could not rename, set back to the original.
mPath = oldPath;
return NS_FILE_FAILURE;
}
SetLeafName(inNewName);
delete [] oldPath;
return NS_OK;
} // nsFileSpec::Rename
@ -313,7 +321,7 @@ nsresult nsFileSpec::Copy(const nsFileSpec& inParentDirectory) const
} // nsFileSpec::Copy
//----------------------------------------------------------------------------------------
nsresult nsFileSpec::Move(const nsFileSpec& inNewParentDirectory) const
nsresult nsFileSpec::Move(const nsFileSpec& inNewParentDirectory)
//----------------------------------------------------------------------------------------
{
// We can only copy into a directory, and (for now) can not copy entire directories
@ -331,8 +339,11 @@ nsresult nsFileSpec::Move(const nsFileSpec& inNewParentDirectory) const
if (result == NS_OK)
{
// cast to fix const-ness
((nsFileSpec*)this)->Delete(PR_FALSE);
}
((nsFileSpec*)this)->Delete(PR_FALSE);
*this = inNewParentDirectory + GetLeafName();
}
delete [] destPath;
}
return result;
}

View File

@ -284,11 +284,19 @@ nsresult nsFileSpec::Rename(const char* inNewName)
if (strchr(inNewName, '/'))
return NS_FILE_FAILURE;
if (PR_Rename(*this, inNewName) != NS_OK)
char* oldPath = PL_strdup(mPath);
SetLeafName(inNewName);
if (PR_Rename(oldPath, mPath) != NS_OK)
{
// Could not rename, set back to the original.
mPath = oldPath;
return NS_FILE_FAILURE;
}
SetLeafName(inNewName);
delete [] oldPath;
return NS_OK;
} // nsFileSpec::Rename
@ -314,7 +322,7 @@ nsresult nsFileSpec::Copy(const nsFileSpec& inParentDirectory) const
} // nsFileSpec::Copy
//----------------------------------------------------------------------------------------
nsresult nsFileSpec::Move(const nsFileSpec& inNewParentDirectory) const
nsresult nsFileSpec::Move(const nsFileSpec& inNewParentDirectory)
//----------------------------------------------------------------------------------------
{
// We can only copy into a directory, and (for now) can not copy entire directories
@ -328,8 +336,14 @@ nsresult nsFileSpec::Move(const nsFileSpec& inNewParentDirectory) const
// MoveFile returns non-zero if succeeds
int copyOK = MoveFile(GetCString(), destPath);
if (copyOK)
{
*this = inNewParentDirectory + GetLeafName();
return NS_OK;
}
delete [] destPath;
}
return NS_FILE_FAILURE;
} // nsFileSpec::Move

View File

@ -458,7 +458,7 @@ class NS_BASE nsFileSpec
return Rename(newName);
}
nsresult Copy(const nsFileSpec& inNewParentDirectory) const;
nsresult Move(const nsFileSpec& inNewParentDirectory) const;
nsresult Move(const nsFileSpec& inNewParentDirectory);
nsresult Execute(const char* args) const;
nsresult Execute(const nsString& args) const
{

View File

@ -816,7 +816,7 @@ nsresult nsFileSpec::Copy(const nsFileSpec& newParentDir) const
} // nsFileSpec::Copy
//----------------------------------------------------------------------------------------
nsresult nsFileSpec::Move(const nsFileSpec& newParentDir) const
nsresult nsFileSpec::Move(const nsFileSpec& newParentDir)
//----------------------------------------------------------------------------------------
{
// We can only move into a directory
@ -827,6 +827,9 @@ nsresult nsFileSpec::Move(const nsFileSpec& newParentDir) const
nsresult result = NS_FILE_RESULT(::FSpMoveRenameCompat(&mSpec,
&newParentDir.mSpec,
const_cast<StringPtr>(GetLeafPName())));
if ( NS_SUCCEEDED(result) )
*this = newParentDir + GetLeafName();
return result;
} // nsFileSpec::Move

View File

@ -231,11 +231,19 @@ nsresult nsFileSpec::Rename(const char* inNewName)
if (strchr(inNewName, '/'))
return NS_FILE_FAILURE;
if (PR_Rename(mPath, inNewName) != 0)
char* oldPath = PL_strdup(mPath);
SetLeafName(inNewName);
if (PR_Rename(oldPath, mPath) != NS_OK)
{
// Could not rename, set back to the original.
mPath = oldPath;
return NS_FILE_FAILURE;
}
SetLeafName(inNewName);
delete [] oldPath;
return NS_OK;
} // nsFileSpec::Rename
@ -313,7 +321,7 @@ nsresult nsFileSpec::Copy(const nsFileSpec& inParentDirectory) const
} // nsFileSpec::Copy
//----------------------------------------------------------------------------------------
nsresult nsFileSpec::Move(const nsFileSpec& inNewParentDirectory) const
nsresult nsFileSpec::Move(const nsFileSpec& inNewParentDirectory)
//----------------------------------------------------------------------------------------
{
// We can only copy into a directory, and (for now) can not copy entire directories
@ -331,8 +339,11 @@ nsresult nsFileSpec::Move(const nsFileSpec& inNewParentDirectory) const
if (result == NS_OK)
{
// cast to fix const-ness
((nsFileSpec*)this)->Delete(PR_FALSE);
}
((nsFileSpec*)this)->Delete(PR_FALSE);
*this = inNewParentDirectory + GetLeafName();
}
delete [] destPath;
}
return result;
}

View File

@ -284,11 +284,19 @@ nsresult nsFileSpec::Rename(const char* inNewName)
if (strchr(inNewName, '/'))
return NS_FILE_FAILURE;
if (PR_Rename(*this, inNewName) != NS_OK)
char* oldPath = PL_strdup(mPath);
SetLeafName(inNewName);
if (PR_Rename(oldPath, mPath) != NS_OK)
{
// Could not rename, set back to the original.
mPath = oldPath;
return NS_FILE_FAILURE;
}
SetLeafName(inNewName);
delete [] oldPath;
return NS_OK;
} // nsFileSpec::Rename
@ -314,7 +322,7 @@ nsresult nsFileSpec::Copy(const nsFileSpec& inParentDirectory) const
} // nsFileSpec::Copy
//----------------------------------------------------------------------------------------
nsresult nsFileSpec::Move(const nsFileSpec& inNewParentDirectory) const
nsresult nsFileSpec::Move(const nsFileSpec& inNewParentDirectory)
//----------------------------------------------------------------------------------------
{
// We can only copy into a directory, and (for now) can not copy entire directories
@ -328,8 +336,14 @@ nsresult nsFileSpec::Move(const nsFileSpec& inNewParentDirectory) const
// MoveFile returns non-zero if succeeds
int copyOK = MoveFile(GetCString(), destPath);
if (copyOK)
{
*this = inNewParentDirectory + GetLeafName();
return NS_OK;
}
delete [] destPath;
}
return NS_FILE_FAILURE;
} // nsFileSpec::Move