Avoid infinite loop in tar

When the tar file is written in one directory archived by tar
the function archive enters in an infinite loop due to de
tar file written. This patch avoid this case checking the
inode of the tar file before of adding it to the archive.
This commit is contained in:
Roberto E. Vargas Caballero 2013-07-20 18:08:58 +02:00 committed by David Galos
parent c1730c78de
commit f636ac791b

10
tar.c
View File

@ -47,6 +47,7 @@ static void c(char *);
static void xt(int (*)(char*, int, char[Blksiz])); static void xt(int (*)(char*, int, char[Blksiz]));
static FILE *tarfile; static FILE *tarfile;
static ino_t tarinode;
static void static void
usage(void) usage(void)
@ -117,9 +118,14 @@ main(int argc, char *argv[])
usage(); usage();
if(file) { if(file) {
struct stat st;
tarfile = fopen(file, (mode == 'c') ? "wb" : "rb"); tarfile = fopen(file, (mode == 'c') ? "wb" : "rb");
if(!tarfile) if(!tarfile)
eprintf("tar: open '%s':", file); eprintf("tar: open '%s':", file);
if (lstat(file, &st) < 0)
eprintf("tar: stat '%s':", file);
tarinode = st.st_ino;
} else { } else {
tarfile = (mode == 'c') ? stdout : stdin; tarfile = (mode == 'c') ? stdout : stdin;
} }
@ -155,6 +161,10 @@ archive(const char* path, const struct stat* sta, int type)
mode_t mode; mode_t mode;
lstat(path, &st); lstat(path, &st);
if (st.st_ino == tarinode) {
fprintf(stderr, "ignoring '%s'\n", path);
return 0;
}
pw = getpwuid(st.st_uid); pw = getpwuid(st.st_uid);
gr = getgrgid(st.st_gid); gr = getgrgid(st.st_gid);