tar: Implement -m flag

This changes the default behavior to adjust mtimes to what is present in
the file header.
This commit is contained in:
Michael Forney 2014-11-01 20:36:39 +00:00 committed by sin
parent e1f87da43e
commit 7ed4866556

25
tar.c
View File

@ -2,9 +2,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <stdbool.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h>
#include <limits.h> #include <limits.h>
#include <grp.h> #include <grp.h>
#include <pwd.h> #include <pwd.h>
@ -50,13 +52,15 @@ static FILE *tarfile;
static ino_t tarinode; static ino_t tarinode;
static dev_t tardev; static dev_t tardev;
static bool mflag = false;
static void static void
usage(void) usage(void)
{ {
eprintf("usage: tar [-f tarfile] [-C dir] [-]x|t\n" eprintf("usage: tar [-f tarfile] [-C dir] [-]x[m]|t\n"
" tar [-f tarfile] [-C dir] [-]c dir\n" " tar [-f tarfile] [-C dir] [-]c dir\n"
" tar [-C dir] cf tarfile dir\n" " tar [-C dir] cf tarfile dir\n"
" tar [-C dir] x|tf tarfile\n"); " tar [-C dir] x[m]|tf tarfile\n");
} }
int int
@ -80,6 +84,9 @@ main(int argc, char *argv[])
case 'f': case 'f':
file = EARGF(usage()); file = EARGF(usage());
break; break;
case 'm':
mflag = true;
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
@ -109,6 +116,9 @@ main(int argc, char *argv[])
argc--, argv++; argc--, argv++;
dir = argv[0]; dir = argv[0];
break; break;
case 'm':
mflag = true;
break;
default: default:
usage(); usage();
} }
@ -225,9 +235,12 @@ unarchive(char *fname, int l, char b[Blksiz])
{ {
char lname[101]; char lname[101];
FILE *f = NULL; FILE *f = NULL;
unsigned long mode, major, minor, type; unsigned long mode, major, minor, type, mtime;
struct timeval times[2] = {0};
Header *h = (void*)b; Header *h = (void*)b;
if(!mflag)
mtime = strtoul(h->mtime, 0, 8);
unlink(fname); unlink(fname);
switch(h->type) { switch(h->type) {
case REG: case REG:
@ -277,6 +290,12 @@ unarchive(char *fname, int l, char b[Blksiz])
} }
if(f) if(f)
fclose(f); fclose(f);
if(!mflag) {
times[0].tv_sec = times[1].tv_sec = mtime;
if(utimes(fname, times))
perror(fname);
}
return 0; return 0;
} }