Don't truncate the output file before making sure that we can

read at least 1 byte from the input file without problems.  This
fixes a bug in uncompress(1) that causes the accidental removal
of files that happen to have the same name as the output file,
even when the uncompression fails and is aborted, i.e.:

$ echo hello world > hello
$ touch hello.Z
$ ls -l hello*
-rw-rw-r--  1 giorgos  giorgos  12 Jun 14 13:33 hello
-rw-rw-r--  1 giorgos  giorgos   0 Jun 14 13:33 hello.Z
$ ./uncompress -f hello
uncompress: hello.Z: Inappropriate file type or format
$ ls -l hello*
-rw-rw-r--  1 giorgos  giorgos  0 Jun 14 13:33 hello.Z
$

PR:		46787
Submitted by:	keramida
This commit is contained in:
Tom Rhodes 2003-06-14 13:41:31 +00:00
parent f212249acf
commit 33ffdd8115
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=116336

View File

@ -300,14 +300,9 @@ decompress(const char *in, const char *out, int bits)
isreg = oreg = !exists || S_ISREG(sb.st_mode);
ifp = ofp = NULL;
if ((ofp = fopen(out, "w")) == NULL) {
cwarn("%s", out);
return;
}
if ((ifp = zopen(in, "r", bits)) == NULL) {
cwarn("%s", in);
goto err;
return;
}
if (stat(in, &sb)) {
cwarn("%s", in);
@ -316,6 +311,22 @@ decompress(const char *in, const char *out, int bits)
if (!S_ISREG(sb.st_mode))
isreg = 0;
/*
* Try to read the first few uncompressed bytes from the input file
* before blindly truncating the output file.
*/
if ((nr = fread(buf, 1, sizeof(buf), ifp)) == 0) {
cwarn("%s", in);
(void)fclose(ifp);
return;
}
if ((ofp = fopen(out, "w")) == NULL ||
(nr != 0 && fwrite(buf, 1, nr, ofp) != nr)) {
cwarn("%s", out);
(void)fclose(ifp);
return;
}
while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
if (fwrite(buf, 1, nr, ofp) != nr) {
cwarn("%s", out);