In support of #8305 (implement cache).
Add Truncate() method to nsIFileSpec and implement in nsFileSpecImpl [Mac Implementation: sdagley, r: sfraser] [Win implementation: fur, r:rogerl] [Unix implementation: yixiong.zue@intel.com, r: fur] git-svn-id: svn://10.0.0.236/trunk@53948 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
parent
c9cba49af2
commit
e3d83679ec
@ -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
|
||||
|
||||
@ -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)
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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)
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
@ -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)
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user