📄 bzip2.c
字号:
{
fprintf ( stderr,
"\n%s: Data integrity error when decompressing.\n",
progName );
showFileNames();
cadvise();
cleanUpAndFail( 2 );
}
/*---------------------------------------------*/
static
void compressedStreamEOF ( void )
{
if (noisy) {
fprintf ( stderr,
"\n%s: Compressed file ends unexpectedly;\n\t"
"perhaps it is corrupted? *Possible* reason follows.\n",
progName );
perror ( progName );
showFileNames();
cadvise();
}
cleanUpAndFail( 2 );
}
/*---------------------------------------------*/
static
void ioError ( void )
{
fprintf ( stderr,
"\n%s: I/O or other error, bailing out. "
"Possible reason follows.\n",
progName );
perror ( progName );
showFileNames();
cleanUpAndFail( 1 );
}
/*---------------------------------------------*/
static
void mySignalCatcher ( IntNative n )
{
fprintf ( stderr,
"\n%s: Control-C or similar caught, quitting.\n",
progName );
cleanUpAndFail(1);
}
/*---------------------------------------------*/
static
void mySIGSEGVorSIGBUScatcher ( IntNative n )
{
if (opMode == OM_Z)
fprintf (
stderr,
"\n%s: Caught a SIGSEGV or SIGBUS whilst compressing.\n"
"\n"
" Possible causes are (most likely first):\n"
" (1) This computer has unreliable memory or cache hardware\n"
" (a surprisingly common problem; try a different machine.)\n"
" (2) A bug in the compiler used to create this executable\n"
" (unlikely, if you didn't compile bzip2 yourself.)\n"
" (3) A real bug in bzip2 -- I hope this should never be the case.\n"
" The user's manual, Section 4.3, has more info on (1) and (2).\n"
" \n"
" If you suspect this is a bug in bzip2, or are unsure about (1)\n"
" or (2), feel free to report it to me at: jseward@bzip.org.\n"
" Section 4.3 of the user's manual describes the info a useful\n"
" bug report should have. If the manual is available on your\n"
" system, please try and read it before mailing me. If you don't\n"
" have the manual or can't be bothered to read it, mail me anyway.\n"
"\n",
progName );
else
fprintf (
stderr,
"\n%s: Caught a SIGSEGV or SIGBUS whilst decompressing.\n"
"\n"
" Possible causes are (most likely first):\n"
" (1) The compressed data is corrupted, and bzip2's usual checks\n"
" failed to detect this. Try bzip2 -tvv my_file.bz2.\n"
" (2) This computer has unreliable memory or cache hardware\n"
" (a surprisingly common problem; try a different machine.)\n"
" (3) A bug in the compiler used to create this executable\n"
" (unlikely, if you didn't compile bzip2 yourself.)\n"
" (4) A real bug in bzip2 -- I hope this should never be the case.\n"
" The user's manual, Section 4.3, has more info on (2) and (3).\n"
" \n"
" If you suspect this is a bug in bzip2, or are unsure about (2)\n"
" or (3), feel free to report it to me at: jseward@bzip.org.\n"
" Section 4.3 of the user's manual describes the info a useful\n"
" bug report should have. If the manual is available on your\n"
" system, please try and read it before mailing me. If you don't\n"
" have the manual or can't be bothered to read it, mail me anyway.\n"
"\n",
progName );
showFileNames();
if (opMode == OM_Z)
cleanUpAndFail( 3 ); else
{ cadvise(); cleanUpAndFail( 2 ); }
}
/*---------------------------------------------*/
static
void outOfMemory ( void )
{
fprintf ( stderr,
"\n%s: couldn't allocate enough memory\n",
progName );
showFileNames();
cleanUpAndFail(1);
}
/*---------------------------------------------*/
static
void configError ( void )
{
fprintf ( stderr,
"bzip2: I'm not configured correctly for this platform!\n"
"\tI require Int32, Int16 and Char to have sizes\n"
"\tof 4, 2 and 1 bytes to run properly, and they don't.\n"
"\tProbably you can fix this by defining them correctly,\n"
"\tand recompiling. Bye!\n" );
setExit(3);
exit(exitValue);
}
/*---------------------------------------------------*/
/*--- The main driver machinery ---*/
/*---------------------------------------------------*/
/* All rather crufty. The main problem is that input files
are stat()d multiple times before use. This should be
cleaned up.
*/
/*---------------------------------------------*/
static
void pad ( Char *s )
{
Int32 i;
if ( (Int32)strlen(s) >= longestFileName ) return;
for (i = 1; i <= longestFileName - (Int32)strlen(s); i++)
fprintf ( stderr, " " );
}
/*---------------------------------------------*/
static
void copyFileName ( Char* to, Char* from )
{
if ( strlen(from) > FILE_NAME_LEN-10 ) {
fprintf (
stderr,
"bzip2: file name\n`%s'\n"
"is suspiciously (more than %d chars) long.\n"
"Try using a reasonable file name instead. Sorry! :-)\n",
from, FILE_NAME_LEN-10
);
setExit(1);
exit(exitValue);
}
strncpy(to,from,FILE_NAME_LEN-10);
to[FILE_NAME_LEN-10]='\0';
}
/*---------------------------------------------*/
static
Bool fileExists ( Char* name )
{
FILE *tmp = fopen ( name, "rb" );
Bool exists = (tmp != NULL);
if (tmp != NULL) fclose ( tmp );
return exists;
}
/*---------------------------------------------*/
/* Open an output file safely with O_EXCL and good permissions.
This avoids a race condition in versions < 1.0.2, in which
the file was first opened and then had its interim permissions
set safely. We instead use open() to create the file with
the interim permissions required. (--- --- rw-).
For non-Unix platforms, if we are not worrying about
security issues, simple this simply behaves like fopen.
*/
FILE* fopen_output_safely ( Char* name, const char* mode )
{
# if BZ_UNIX
FILE* fp;
IntNative fh;
fh = open(name, O_WRONLY|O_CREAT|O_EXCL, S_IWUSR|S_IRUSR);
if (fh == -1) return NULL;
fp = fdopen(fh, mode);
if (fp == NULL) close(fh);
return fp;
# else
return fopen(name, mode);
# endif
}
/*---------------------------------------------*/
/*--
if in doubt, return True
--*/
static
Bool notAStandardFile ( Char* name )
{
IntNative i;
struct MY_STAT statBuf;
i = MY_LSTAT ( name, &statBuf );
if (i != 0) return True;
if (MY_S_ISREG(statBuf.st_mode)) return False;
return True;
}
/*---------------------------------------------*/
/*--
rac 11/21/98 see if file has hard links to it
--*/
static
Int32 countHardLinks ( Char* name )
{
IntNative i;
struct MY_STAT statBuf;
i = MY_LSTAT ( name, &statBuf );
if (i != 0) return 0;
return (statBuf.st_nlink - 1);
}
/*---------------------------------------------*/
/* Copy modification date, access date, permissions and owner from the
source to destination file. We have to copy this meta-info off
into fileMetaInfo before starting to compress / decompress it,
because doing it afterwards means we get the wrong access time.
To complicate matters, in compress() and decompress() below, the
sequence of tests preceding the call to saveInputFileMetaInfo()
involves calling fileExists(), which in turn establishes its result
by attempting to fopen() the file, and if successful, immediately
fclose()ing it again. So we have to assume that the fopen() call
does not cause the access time field to be updated.
Reading of the man page for stat() (man 2 stat) on RedHat 7.2 seems
to imply that merely doing open() will not affect the access time.
Therefore we merely need to hope that the C library only does
open() as a result of fopen(), and not any kind of read()-ahead
cleverness.
It sounds pretty fragile to me. Whether this carries across
robustly to arbitrary Unix-like platforms (or even works robustly
on this one, RedHat 7.2) is unknown to me. Nevertheless ...
*/
#if BZ_UNIX
static
struct MY_STAT fileMetaInfo;
#endif
static
void saveInputFileMetaInfo ( Char *srcName )
{
# if BZ_UNIX
IntNative retVal;
/* Note use of stat here, not lstat. */
retVal = MY_STAT( srcName, &fileMetaInfo );
ERROR_IF_NOT_ZERO ( retVal );
# endif
}
static
void applySavedMetaInfoToOutputFile ( Char *dstName )
{
# if BZ_UNIX
IntNative retVal;
struct utimbuf uTimBuf;
uTimBuf.actime = fileMetaInfo.st_atime;
uTimBuf.modtime = fileMetaInfo.st_mtime;
retVal = chmod ( dstName, fileMetaInfo.st_mode );
ERROR_IF_NOT_ZERO ( retVal );
retVal = utime ( dstName, &uTimBuf );
ERROR_IF_NOT_ZERO ( retVal );
retVal = chown ( dstName, fileMetaInfo.st_uid, fileMetaInfo.st_gid );
/* chown() will in many cases return with EPERM, which can
be safely ignored.
*/
# endif
}
/*---------------------------------------------*/
static
Bool containsDubiousChars ( Char* name )
{
# if BZ_UNIX
/* On unix, files can contain any characters and the file expansion
* is performed by the shell.
*/
return False;
# else /* ! BZ_UNIX */
/* On non-unix (Win* platforms), wildcard characters are not allowed in
* filenames.
*/
for (; *name != '\0'; name++)
if (*name == '?' || *name == '*') return True;
return False;
# endif /* BZ_UNIX */
}
/*---------------------------------------------*/
#define BZ_N_SUFFIX_PAIRS 4
Char* zSuffix[BZ_N_SUFFIX_PAIRS]
= { ".bz2", ".bz", ".tbz2", ".tbz" };
Char* unzSuffix[BZ_N_SUFFIX_PAIRS]
= { "", "", ".tar", ".tar" };
static
Bool hasSuffix ( Char* s, Char* suffix )
{
Int32 ns = strlen(s);
Int32 nx = strlen(suffix);
if (ns < nx) return False;
if (strcmp(s + ns - nx, suffix) == 0) return True;
return False;
}
static
Bool mapSuffix ( Char* name,
Char* oldSuffix, Char* newSuffix )
{
if (!hasSuffix(name,oldSuffix)) return False;
name[strlen(name)-strlen(oldSuffix)] = 0;
strcat ( name, newSuffix );
return True;
}
/*---------------------------------------------*/
static
void compress ( Char *name )
{
FILE *inStr;
FILE *outStr;
Int32 n, i;
struct MY_STAT statBuf;
deleteOutputOnInterrupt = False;
if (name == NULL && srcMode != SM_I2O)
panic ( "compress: bad modes\n" );
switch (srcMode) {
case SM_I2O:
copyFileName ( inName, "(stdin)" );
copyFileName ( outName, "(stdout)" );
break;
case SM_F2F:
copyFileName ( inName, name );
copyFileName ( outName, name );
strcat ( outName, ".bz2" );
break;
case SM_F2O:
copyFileName ( inName, name );
copyFileName ( outName, "(stdout)" );
break;
}
if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
if (noisy)
fprintf ( stderr, "%s: There are no files matching `%s'.\n",
progName, inName );
setExit(1);
return;
}
if ( srcMode != SM_I2O && !fileExists ( inName ) ) {
fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
progName, inName, strerror(errno) );
setExit(1);
return;
}
for (i = 0; i < BZ_N_SUFFIX_PAIRS; i++) {
if (hasSuffix(inName, zSuffix[i])) {
if (noisy)
fprintf ( stderr,
"%s: Input file %s already has %s suffix.\n",
progName, inName, zSuffix[i] );
setExit(1);
return;
}
}
if ( srcMode == SM_F2F || srcMode == SM_F2O ) {
MY_STAT(inName, &statBuf);
if ( MY_S_ISDIR(statBuf.st_mode) ) {
fprintf( stderr,
"%s: Input file %s is a directory.\n",
progName,inName);
setExit(1);
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -