diff --git a/xpcom/io/nsFileSpec.h b/xpcom/io/nsFileSpec.h index a6175579b65f..aff4dd6a8d19 100644 --- a/xpcom/io/nsFileSpec.h +++ b/xpcom/io/nsFileSpec.h @@ -506,6 +506,7 @@ class NS_COM nsFileSpec void CreateDir(int mode = 0700) { CreateDirectory(mode); } // workaround for yet another VC++ bug with long identifiers. void Delete(PRBool inRecursive) const; + nsresult Truncate(PRInt32 aNewLength) const; void RecursiveCopy(nsFileSpec newDir) const; nsresult Rename(const char* inNewName); // not const: gets updated diff --git a/xpcom/io/nsFileSpecImpl.cpp b/xpcom/io/nsFileSpecImpl.cpp index e6faa3929bee..c61027144110 100644 --- a/xpcom/io/nsFileSpecImpl.cpp +++ b/xpcom/io/nsFileSpecImpl.cpp @@ -384,6 +384,13 @@ NS_IMETHODIMP nsFileSpecImpl::CreateDir() return mFileSpec.Error(); } +//---------------------------------------------------------------------------------------- +NS_IMETHODIMP nsFileSpecImpl::Truncate(PRInt32 aNewLength) +//---------------------------------------------------------------------------------------- +{ + return mFileSpec.Truncate(aNewLength); +} + //---------------------------------------------------------------------------------------- NS_IMETHODIMP nsFileSpecImpl::Rename(const char *newLeafName) //---------------------------------------------------------------------------------------- diff --git a/xpcom/io/nsFileSpecMac.cpp b/xpcom/io/nsFileSpecMac.cpp index 16a0e36933da..faee1a38a322 100644 --- a/xpcom/io/nsFileSpecMac.cpp +++ b/xpcom/io/nsFileSpecMac.cpp @@ -1031,6 +1031,32 @@ void nsFileSpec::RecursiveCopy(nsFileSpec newDir) const } } // nsFileSpec::RecursiveCopy +//---------------------------------------------------------------------------------------- +nsresult nsFileSpec::Truncate(PRInt32 aNewLength) const +//---------------------------------------------------------------------------------------- +{ + short refNum; + OSErr err; + + // First see if we have an internal error set + if (NS_FAILED(mError)) + return mError; + + // Need to open the file to trunc + if (::FSpOpenDF(&mSpec, fsWrPerm, &refNum) != noErr) + return NS_FILE_FAILURE; + + err = ::SetEOF(refNum, aNewLength); + + // Close the file unless we got an error that it was already closed + if (err != fnOpnErr) + (void)::FSClose(refNum); + + if (err != noErr) + return NS_FILE_FAILURE; + + return NS_OK; +} // nsFileSpec::Truncate //---------------------------------------------------------------------------------------- nsresult nsFileSpec::Rename(const char* inNewName) diff --git a/xpcom/io/nsFileSpecUnix.cpp b/xpcom/io/nsFileSpecUnix.cpp index 2d255008945a..40e0c856f3cd 100644 --- a/xpcom/io/nsFileSpecUnix.cpp +++ b/xpcom/io/nsFileSpecUnix.cpp @@ -341,6 +341,22 @@ void nsFileSpec::RecursiveCopy(nsFileSpec newDir) const } // nsFileSpec::RecursiveCopy +//---------------------------------------------------------------------------------------- +nsresult nsFileSpec::Truncate(const PRInt32 offset) const +//---------------------------------------------------------------------------------------- +{ + char* Path = nsCRT::strdup(mPath); + + int rv = truncate(Path, offset) ; + + nsCRT::free(Path) ; + + if(!rv) + return NS_OK ; + else + return NS_ERROR_FAILURE ; +} // nsFileSpec::Truncate + //---------------------------------------------------------------------------------------- nsresult nsFileSpec::Rename(const char* inNewName) //---------------------------------------------------------------------------------------- diff --git a/xpcom/io/nsFileSpecWin.cpp b/xpcom/io/nsFileSpecWin.cpp index e10e33889d52..6f95ccb34aa6 100644 --- a/xpcom/io/nsFileSpecWin.cpp +++ b/xpcom/io/nsFileSpecWin.cpp @@ -1,4 +1,4 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * The contents of this file are subject to the Netscape Public * License Version 1.1 (the "License"); you may not use this file @@ -19,7 +19,7 @@ * * Contributor(s): */ - + // This file is included by nsFileSpec.cp, and includes the Windows-specific // implementations. @@ -471,6 +471,46 @@ void nsFileSpec::RecursiveCopy(nsFileSpec newDir) const } } // nsFileSpec::RecursiveCopy +//---------------------------------------------------------------------------------------- +nsresult +nsFileSpec::Truncate(const PRInt32 aNewFileLength) const +//---------------------------------------------------------------------------------------- +{ + DWORD status; + HANDLE hFile; + + // Leave it to Microsoft to open an existing file with a function + // named "CreateFile". + hFile = CreateFile(mPath, + GENERIC_WRITE, + FILE_SHARE_READ, + NULL, + OPEN_EXISTING, + FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, + NULL); + if (hFile == INVALID_HANDLE_VALUE) + return NS_FILE_FAILURE; + + // Seek to new, desired end of file + status = SetFilePointer(hFile, aNewFileLength, NULL, FILE_BEGIN); + if (status == 0xffffffff) + goto error; + + // Truncate file at current cursor position + if (!SetEndOfFile(hFile)) + goto error; + + if (!CloseHandle(hFile)) + return NS_FILE_FAILURE; + + return NS_OK; + + error: + CloseHandle(hFile); + return NS_FILE_FAILURE; + +} // nsFileSpec::Truncate + //---------------------------------------------------------------------------------------- nsresult nsFileSpec::Rename(const char* inNewName) //---------------------------------------------------------------------------------------- diff --git a/xpcom/io/nsIFileSpec.idl b/xpcom/io/nsIFileSpec.idl index 33a4ce20eab3..63774fd9dd44 100644 --- a/xpcom/io/nsIFileSpec.idl +++ b/xpcom/io/nsIFileSpec.idl @@ -102,6 +102,7 @@ interface nsIFileSpec : nsISupports boolean isSymlink(); void resolveSymlink(); + void truncate(in long aNewLength); void rename([const] in string newLeafName); void copyToDir([const] in nsIFileSpec newParentDir); void moveToDir([const] in nsIFileSpec newParentDir);