1
0
mirror of https://github.com/upx/upx synced 2025-09-28 19:06:07 +08:00

Support multiple backup generations.

committer: mfx <mfx> 968544834 +0000
This commit is contained in:
Markus F.X.J. Oberhumer 2000-09-10 00:13:54 +00:00
parent 557305cdec
commit 67e233176c
3 changed files with 39 additions and 12 deletions

View File

@ -325,13 +325,28 @@ char *center_string(const char *name, size_t s)
bool file_exists(const char *name) bool file_exists(const char *name)
{ {
int fd; int fd, r;
struct stat st;
/* return true if we can open it */
fd = open(name,O_RDONLY); fd = open(name,O_RDONLY);
if (fd < 0) if (fd >= 0)
return 0; {
(void) close(fd); (void) close(fd);
return 1; return true;
}
/* return true if we can stat it */
memset(&st, 0, sizeof(st));
#if defined(HAVE_LSTAT)
r = lstat(name,&st);
#else
r = stat(name,&st);
#endif
if (r != -1)
return true;
return false;
} }
@ -363,9 +378,10 @@ bool maketempname(char *ofilename, const char *ifilename,
} }
void makebakname(char *ofilename, const char *ifilename) bool makebakname(char *ofilename, const char *ifilename, bool force)
{ {
char *ofext = NULL, *ofname; char *ofext = NULL, *ofname;
int ofile;
strcpy(ofilename, ifilename); strcpy(ofilename, ifilename);
for (ofname = fn_basename(ofilename); *ofname; ofname++) for (ofname = fn_basename(ofilename); *ofname; ofname++)
@ -374,11 +390,24 @@ void makebakname(char *ofilename, const char *ifilename)
ofext = ofname; ofext = ofname;
} }
if (ofext == NULL) if (ofext == NULL)
strcat(ofilename, ".~"); {
ofext = ofilename + strlen(ofilename);
strcpy(ofext, ".~");
}
else if (strlen(ofext) < 1 + 3) else if (strlen(ofext) < 1 + 3)
strcat(ofilename, "~"); strcat(ofilename, "~");
else else
ofext[strlen(ofext)-1] = '~'; ofext[strlen(ofext)-1] = '~';
if (!force)
return true;
if (file_exists(ofilename))
for (ofile = 0; ofile < 999; ofile++)
{
sprintf(ofext, ".%03d", ofile);
if (!file_exists(ofilename))
return true;
}
return false;
} }

View File

@ -43,8 +43,8 @@ bool fn_has_ext(const char *name, const char *ext);
bool file_exists(const char *name); bool file_exists(const char *name);
bool maketempname(char *ofilename, const char *ifilename, bool maketempname(char *ofilename, const char *ifilename,
const char *ext, bool force); const char *ext, bool force=true);
void makebakname(char *ofilename, const char *ifilename); bool makebakname(char *ofilename, const char *ifilename, bool force=true);
bool isafile(int fd); bool isafile(int fd);
unsigned get_ratio(unsigned long packedsize, unsigned long size, unsigned get_ratio(unsigned long packedsize, unsigned long size,

View File

@ -99,7 +99,7 @@ void do_one_file(const char *iname, char *oname)
if (opt->output_name) if (opt->output_name)
strcpy(tname,opt->output_name); strcpy(tname,opt->output_name);
else else
maketempname(tname,iname,".upx",1); maketempname(tname,iname,".upx");
if (opt->force >= 2) if (opt->force >= 2)
{ {
#if defined(HAVE_CHMOD) #if defined(HAVE_CHMOD)
@ -177,8 +177,6 @@ void do_one_file(const char *iname, char *oname)
// make backup // make backup
char bakname[PATH_MAX+1]; char bakname[PATH_MAX+1];
makebakname(bakname,iname); makebakname(bakname,iname);
if (file_exists(bakname))
maketempname(bakname,iname,".000",1);
File::rename(iname,bakname); File::rename(iname,bakname);
} }
File::rename(oname,iname); File::rename(oname,iname);