📄 cabinet.cpp
字号:
// I/O :
// Task : Close this file
//-------------------------------------------------------------------------
int FAR DIAMONDAPI CCabinetBase::FileClose(int hFile, int FAR *ptrErr, void FAR *pv)
{
#if defined(__TRACE_CAB_COMPRESSION__) || defined(__TRACE_CAB_EXTRACTION__)
TRACEFN(_T("CCabinetBase::FileClose(%d)\n"),hFile);
#endif
// No error yet
ASSERTX(ptrErr);
*ptrErr =NOERROR;
BOOL bResult =CloseHandle((HANDLE)hFile);
if(!bResult)
// Store the error code
*ptrErr =EBADF;
return bResult ? 0 : -1;
}
#ifdef __CAB_BUILD__
//-------------------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Create a cabinet builder object
//-------------------------------------------------------------------------
CCabinetBuilder::CCabinetBuilder(USHORT nID, ULONG cbFirstCabSize, ULONG cbCabSize, ULONG cbTreshold):
m_cbFirstCabSize(cbFirstCabSize),
m_cbCabSize(cbCabSize),
m_cbTreshold(cbTreshold),
m_nID(nID),
m_cbReserveCFHeader(0),
m_cbReserveCFFolder(0),
m_cbReserveCFData(0),
m_hContext(NULL)
{
#ifdef __TRACE_CAB_COMPRESSION__
TRACEFN(_T("CCabinetBuilder::CCabinetBuilder\n"));
#endif
// Inited, but before we can do actual compression client must call InitCabinet
}
#endif
#ifdef __CAB_BUILD__
//-------------------------------------------------------------------------
// Pre : The wild parameter pv is a pointer 2 an instance of class CCabinetBuilder
// Post :
// Globals :
// I/O :
// Task : Compression interface wants 2 delete this file
//-------------------------------------------------------------------------
int FAR DIAMONDAPI CCabinetBuilder::FileDelete(char FAR *pszFile, int FAR *ptrErr, void FAR *pv)
{
#ifdef __TRACE_CAB_COMPRESSION__
TRACEFN(_T("CCabinetBuilder::FileDelete(%s)\n"),pszFile);
#endif
// No error yet
ASSERTX(ptrErr);
*ptrErr =NOERROR;
BOOL bResult =DeleteFile(pszFile);
if(!bResult)
{
// Store the error code
DWORD dwError =GetLastError();
switch(dwError)
{
case ERROR_ACCESS_DENIED:
case ERROR_LOCK_VIOLATION:
case ERROR_SHARING_VIOLATION:
*ptrErr =EACCES;
break;
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
*ptrErr =ENOENT;
break;
default:
*ptrErr =(int)dwError;
}
}
return bResult;
}
#endif
#ifdef __CAB_BUILD__
//-------------------------------------------------------------------------
// Pre : pszTempName points 2 a buffer to receive complete tempfile name
// cbTempName is size of pszTempName buffer
// The wild parameter pv is a pointer 2 an instance of class CCabinetBuilder
// Post : Return TRUE on success
// Globals :
// I/O :
// Task : Compression interface requests a temporary filename
//-------------------------------------------------------------------------
BOOL FAR DIAMONDAPI CCabinetBuilder::GetTempFile(char *pszTempName, int cbTempName, void FAR *pv)
{
#ifdef __TRACE_CAB_COMPRESSION__
TRACEFN(_T("CCabinetBuilder::GetTempFile... "));
#endif
// Manufacture temporary path
CPath temp_path;
temp_path.TempDirectory();
if(temp_path.CreateTempName(_T("int")))
{
// Got a temp filename
string strTempFileName((LPCTSTR)temp_path);
if(cbTempName < strTempFileName.length())
// Buffer too small
return FALSE;
STRCPY(pszTempName,strTempFileName.c_str());
#ifdef __TRACE_CAB_COMPRESSION__
TRACEX(_T("%s\n"),pszTempName);
#endif
return TRUE;
}
#ifdef __TRACE_CAB_COMPRESSION__
TRACEX(_T("Failed\n"));
#endif
// Could not get a temp filename
return FALSE;
}
#endif
#ifdef __CAB_BUILD__
//-------------------------------------------------------------------------
// Pre : The wild parameter pv is a pointer 2 an instance of class CCabinetBuilder
// Post :
// Globals :
// I/O :
// Task : Opens a file and returns its datestamp, timestamp, and attributes at
// compression interface request
//-------------------------------------------------------------------------
int FAR DIAMONDAPI CCabinetBuilder::GetFileInfo(char *pszName, USHORT *pdate, USHORT *ptime, USHORT *pattribs, int FAR *ptrErr, void FAR *pv)
{
#ifdef __TRACE_CAB_COMPRESSION__
TRACEFN(_T("CCabinetBuilder::GetFileInfo\n"));
#endif
// No error yet
ASSERTX(ptrErr);
*ptrErr =NOERROR;
BY_HANDLE_FILE_INFORMATION finfo;
FILETIME filetime;
HANDLE handle;
DWORD attrs;
// We need a Win32 type handle to get file date/time
handle =CreateFile(pszName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
if(handle == INVALID_HANDLE_VALUE)
{
// Could not open file
goto LABEL_InfoError;
}
if(!GetFileInformationByHandle(handle,&finfo))
{
// Could not retrieve file info
CloseHandle(handle);
goto LABEL_InfoError;
}
FileTimeToLocalFileTime(&finfo.ftLastWriteTime,&filetime);
FileTimeToDosDateTime(&filetime,pdate,ptime);
if((attrs =GetFileAttributes(pszName)) == 0xFFFFFFFF)
{
// Failure
*pattribs =0;
}
else
{
// Mask out all other bits except these, since other
// bits are used by the cabinet format 2 indicate a
// special meaning
*pattribs =(int)(attrs & (_A_RDONLY | _A_SYSTEM | _A_HIDDEN | _A_ARCH));
}
CloseHandle(handle);
// We've got all the info we needed,
// now open the file so that the class' file utility
// functions can work with it
return FileOpen(pszName,_O_RDONLY | _O_BINARY,0,ptrErr,pv);
LABEL_InfoError:
// Store the error code
DWORD dwError =GetLastError();
switch(dwError)
{
case ERROR_ACCESS_DENIED:
case ERROR_LOCK_VIOLATION:
case ERROR_SHARING_VIOLATION:
*ptrErr =EACCES;
break;
case ERROR_INVALID_PARAMETER:
*ptrErr =EINVAL;
break;
case ERROR_TOO_MANY_OPEN_FILES:
*ptrErr =EMFILE;
break;
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
*ptrErr =ENOENT;
break;
default:
*ptrErr =(int)dwError;
}
return -1;
}
#endif
#ifdef __CAB_BUILD__
//-------------------------------------------------------------------------
// Pre : The wild parameter pv is a pointer 2 an instance of class CCabinetBuilder
// Post : Return FALSE 2 abort cabinet creation
// Globals :
// I/O :
// Task : The compression interface wishes to create a new cabinet, which
// will happen whenever the size of the cabinet is about to exceed
// the media size specified
// The cabinet name MUST be modified. Also, the disk name, media
// size, folder threshold, etc. parameters may also be modified
//-------------------------------------------------------------------------
BOOL FAR DIAMONDAPI CCabinetBuilder::GetNextCabinet(PCCAB pccab, ULONG cbPrevCab, void FAR *pv)
{
#ifdef __TRACE_CAB_COMPRESSION__
TRACEFN(_T("CCabinetBuilder::GetNextCabinet(%u)\n"),cbPrevCab);
#endif
if(pccab)
{
// Cabinet counters already incremented by FCI, updating the names
CCabinetBuilder *pcb =(CCabinetBuilder *)pv;
ASSERTX(pcb);
pccab->iDisk++;
pcb->ManufactureDiskName(pccab);
pcb->ManufactureCabinetName(pccab);
// Update the media size after the first cabinet
pccab->cb =pcb->m_cbCabSize;
}
return TRUE;
}
#endif
#ifdef __CAB_BUILD__
//---------------------------------------------------------------------------
// Pre : pszFile is name of file
// cbFile is
// Post : Return FALSE 2 abort
// Globals :
// I/O :
// Task : Simple notification that a file was committed 2 a cabinet
//---------------------------------------------------------------------------
BOOL CCabinetBuilder::NotifyPlaced(char *pszFile, long cbFile, BOOL bContinuation, int nDisk, char *pszCabinet)
{
#ifdef __TRACE_CAB_COMPRESSION__
TRACEFN(_T("CCabinetBuilder::NotifyPlaced(%s,%ld,%u,%d,%s)\n"),pszFile,cbFile,bContinuation,nDisk,pszCabinet);
#endif
return TRUE;
}
#endif
#ifdef __CAB_BUILD__
//-------------------------------------------------------------------------
// Pre : The wild parameter pv is a pointer 2 an instance of class CCabinetBuilder
// Post : Return -1 to abort
// Globals :
// I/O :
// Task : Notification from compression interface that a file was
// committed 2 a cabinet
//-------------------------------------------------------------------------
int FAR DIAMONDAPI CCabinetBuilder::NotifyFilePlaced(PCCAB pccab, char *pszFile, long cbFile, BOOL bContinuation, void FAR *pv)
{
#ifdef __TRACE_CAB_COMPRESSION__
TRACEFN(_T("CCabinetBuilder::NotifyFilePlaced(%s,%ld,%u,%d,%d)\n"),pszFile,cbFile,bContinuation,pccab->iDisk,pccab->iCab);
#endif
// Call the client's notification proc too
ASSERTX(pccab != NULL);
CCabinetBuilder *pcb =(CCabinetBuilder *)pv;
ASSERTX(pcb != NULL);
BOOL ok_2_continue =pcb->NotifyPlaced(pszFile,cbFile,bContinuation,pccab->iDisk,pccab->szCab);
return ok_2_continue ? 0 : -1;
}
#endif
#ifdef __CAB_BUILD__
//---------------------------------------------------------------------------
// Pre : cbOriginal is the size of uncompressed block
// cbCompressed is the size of the block after compression
// Post : Return FALSE 2 abort
// Globals :
// I/O :
// Task : Simple notification that FCI is compressing a block in2 a folder
// Useful 2 keep track of compression statistics
//---------------------------------------------------------------------------
BOOL CCabinetBuilder::NotifyCompress(ULONG cbOriginal, ULONG cbCompressed)
{
#ifdef __TRACE_CAB_COMPRESSION__
TRACEFN(_T("CCabinetBuilder::NotifyCompress(%lu,%lu)\n"),cbOriginal,cbCompressed);
#endif
return TRUE;
}
#endif
#ifdef __CAB_BUILD__
//---------------------------------------------------------------------------
// Pre : cbSize is the total size of folder
// cbWritten is the amount of folder copied to cabinet so far
// Post : Return FALSE 2 abort
// Globals :
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -