📄 tar.c
字号:
pfxlen = sl - name; if (pfxlen <= sizeof hp->prefix && namlen-1 - pfxlen <= Namsiz) break; osl = sl; *osl = '\0'; sl = strrchr(name, '/'); *osl = '/'; } if (sl == nil) { fprint(2, "%s: name can't be split to fit tar header: %s\n", argv0, name); return -1; } *sl = '\0'; strncpy(hp->prefix, name, sizeof hp->prefix); *sl++ = '/'; strncpy(hp->name, sl, sizeof hp->name); if (slname) s_free(slname); return 0;}static intmkhdr(Hdr *hp, Dir *dir, char *file){ /* * these fields run together, so we format them in order and don't use * snprint. */ sprint(hp->mode, "%6lo ", dir->mode & 0777); sprint(hp->uid, "%6o ", aruid); sprint(hp->gid, "%6o ", argid); if (dir->length >= (Off)1<<32) { static int printed; if (!printed) { printed = 1; fprint(2, "%s: storing large sizes in \"base 256\"\n", argv0); } hp->size[0] = Binsize; /* emit so-called `base 256' representation of size */ putbe((uchar *)hp->size+1, dir->length, sizeof hp->size - 2); hp->size[sizeof hp->size - 1] = ' '; } else sprint(hp->size, "%11lluo ", dir->length); sprint(hp->mtime, "%11luo ", dir->mtime); hp->linkflag = (dir->mode&DMDIR? LF_DIR: LF_PLAIN1); putfullname(hp, file); if (posix) { strncpy(hp->magic, "ustar", sizeof hp->magic); strncpy(hp->version, "00", sizeof hp->version); strncpy(hp->uname, dir->uid, sizeof hp->uname); strncpy(hp->gname, dir->gid, sizeof hp->gname); } sprint(hp->chksum, "%6luo", chksum(hp)); return 0;}static void addtoar(int ar, char *file, char *shortf);static voidaddtreetoar(int ar, char *file, char *shortf, int fd){ int n; Dir *dent, *dirents; String *name = s_new(); n = dirreadall(fd, &dirents); if (n < 0) fprint(2, "%s: dirreadall %s: %r\n", argv0, file); close(fd); if (n <= 0) return; if (chdir(shortf) < 0) sysfatal("chdir %s: %r", file); if (DEBUG) fprint(2, "chdir %s\t# %s\n", shortf, file); for (dent = dirents; dent < dirents + n; dent++) { s_reset(name); s_append(name, file); s_append(name, "/"); s_append(name, dent->name); addtoar(ar, s_to_c(name), dent->name); } s_free(name); free(dirents); /* * this assumes that shortf is just one component, which is true * during directory descent, but not necessarily true of command-line * arguments. Our caller (or addtoar's) must reset the working * directory if necessary. */ if (chdir("..") < 0) sysfatal("chdir %s/..: %r", file); if (DEBUG) fprint(2, "chdir ..\n");}static voidaddtoar(int ar, char *file, char *shortf){ int n, fd, isdir; long bytes; ulong blksleft, blksread; Hdr *hbp; Dir *dir; String *name = nil; if (shortf[0] == '#') { name = s_new(); s_append(name, "./"); s_append(name, shortf); shortf = s_to_c(name); } fd = open(shortf, OREAD); if (fd < 0) { fprint(2, "%s: can't open %s: %r\n", argv0, file); if (name) s_free(name); return; } dir = dirfstat(fd); if (dir == nil) sysfatal("can't fstat %s: %r", file); hbp = getblkz(ar); isdir = !!(dir->qid.type&QTDIR); if (mkhdr(hbp, dir, file) < 0) { putbackblk(ar); free(dir); close(fd); if (name) s_free(name); return; } putblk(ar); blksleft = BYTES2TBLKS(dir->length); free(dir); if (isdir) addtreetoar(ar, file, shortf, fd); else { for (; blksleft > 0; blksleft -= blksread) { hbp = getblke(ar); blksread = gothowmany(blksleft); bytes = blksread * Tblock; n = readn(fd, hbp->data, bytes); if (n < 0) sysfatal("error reading %s: %r", file); /* * ignore EOF. zero any partial block to aid * compression and emergency recovery of data. */ if (n < Tblock) memset(hbp->data + n, 0, bytes - n); putblkmany(ar, blksread); } close(fd); if (verbose) fprint(2, "%s\n", file); } if (name) s_free(name);}static char *replace(char **argv){ int i, ar; ulong blksleft, blksread; Off bytes; Hdr *hp; Compress *comp = nil; Pushstate ps; if (usefile && docreate) { ar = create(usefile, OWRITE, 0666); if (docompress) comp = compmethod(usefile); } else if (usefile) ar = open(usefile, ORDWR); else ar = Stdout; if (comp) ar = push(ar, comp->comp, Output, &ps); if (ar < 0) sysfatal("can't open archive %s: %r", usefile); if (usefile && !docreate) { /* skip quickly to the end */ while ((hp = readhdr(ar)) != nil) { bytes = arsize(hp); for (blksleft = BYTES2TBLKS(bytes); blksleft > 0 && getblkrd(ar, Justnxthdr) != nil; blksleft -= blksread) { blksread = gothowmany(blksleft); putreadblks(ar, blksread); } } /* * we have just read the end-of-archive Tblock. * now seek back over the (big) archive block containing it, * and back up curblk ptr over end-of-archive Tblock in memory. */ if (seek(ar, blkoff, 0) < 0) sysfatal("can't seek back over end-of-archive: %r"); curblk--; } for (i = 0; argv[i] != nil; i++) { addtoar(ar, argv[i], argv[i]); chdir(origdir); /* for correctness & profiling */ } /* write end-of-archive marker */ getblkz(ar); putblk(ar); getblkz(ar); putlastblk(ar); if (comp) return pushclose(&ps); if (ar > Stderr) close(ar); return nil;}/* * tar [xt] *//* is pfx a file-name prefix of name? */static intprefix(char *name, char *pfx){ int pfxlen = strlen(pfx); char clpfx[Maxname+1]; if (pfxlen > Maxname) return 0; strcpy(clpfx, pfx); cleanname(clpfx); return strncmp(pfx, name, pfxlen) == 0 && (name[pfxlen] == '\0' || name[pfxlen] == '/');}static intmatch(char *name, char **argv){ int i; char clname[Maxname+1]; if (argv[0] == nil) return 1; strcpy(clname, name); cleanname(clname); for (i = 0; argv[i] != nil; i++) if (prefix(clname, argv[i])) return 1; return 0;}static voidcantcreate(char *s, int mode){ int len; static char *last; /* * Always print about files. Only print about directories * we haven't printed about. (Assumes archive is ordered * nicely.) */ if(mode&DMDIR){ if(last){ /* already printed this directory */ if(strcmp(s, last) == 0) return; /* printed a higher directory, so printed this one */ len = strlen(s); if(memcmp(s, last, len) == 0 && last[len] == '/') return; } /* save */ free(last); last = strdup(s); } fprint(2, "%s: can't create %s: %r\n", argv0, s);}static intmakedir(char *s){ int f; if (access(s, AEXIST) == 0) return -1; f = create(s, OREAD, DMDIR | 0777); if (f >= 0) close(f); else cantcreate(s, DMDIR); return f;}static intmkpdirs(char *s){ int err; char *p; p = s; err = 0; while (!err && (p = strchr(p+1, '/')) != nil) { *p = '\0'; err = (access(s, AEXIST) < 0 && makedir(s) < 0); *p = '/'; } return -err;}/* Call access but preserve the error string. */static intxaccess(char *name, int mode){ char err[ERRMAX]; int rv; err[0] = 0; errstr(err, sizeof err); rv = access(name, mode); errstr(err, sizeof err); return rv;}/* copy a file from the archive into the filesystem *//* fname is result of name(), so has two extra bytes at beginning */static voidextract1(int ar, Hdr *hp, char *fname){ int wrbytes, fd = -1, dir = 0; long mtime = strtol(hp->mtime, nil, 8); ulong mode = strtoul(hp->mode, nil, 8) & 0777; Off bytes = hdrsize(hp); /* for printing */ ulong blksread, blksleft = BYTES2TBLKS(arsize(hp)); Hdr *hbp; if (isdir(hp)) { mode |= DMDIR|0700; dir = 1; } switch (hp->linkflag) { case LF_LINK: case LF_SYMLINK1: case LF_SYMLINK2: case LF_FIFO: blksleft = 0; break; } if (relative) { if(fname[0] == '/') *--fname = '.'; else if(fname[0] == '#'){ *--fname = '/'; *--fname = '.'; } } if (verb == Xtract) { cleanname(fname); switch (hp->linkflag) { case LF_LINK: case LF_SYMLINK1: case LF_SYMLINK2: fprint(2, "%s: can't make (sym)link %s\n", argv0, fname); break; case LF_FIFO: fprint(2, "%s: can't make fifo %s\n", argv0, fname); break; default: if (!keepexisting || access(fname, AEXIST) < 0) { int rw = (dir? OREAD: OWRITE); fd = create(fname, rw, mode); if (fd < 0) { mkpdirs(fname); fd = create(fname, rw, mode); } if (fd < 0 && (!dir || xaccess(fname, AEXIST) < 0)) cantcreate(fname, mode); } if (fd >= 0 && verbose) fprint(2, "%s\n", fname); break; } } else if (verbose) { char *cp = ctime(mtime); print("%M %8lld %-12.12s %-4.4s %s\n", mode, bytes, cp+4, cp+24, fname); } else print("%s\n", fname); if (blksleft == 0) bytes = 0; for (; blksleft > 0; blksleft -= blksread) { hbp = getblkrd(ar, (fd >= 0? Alldata: Justnxthdr)); if (hbp == nil) sysfatal("unexpected EOF on archive extracting %s", fname); blksread = gothowmany(blksleft); if (blksread <= 0) fprint(2, "%s: got %ld blocks reading %s!\n", argv0, blksread, fname); wrbytes = Tblock*blksread; if(wrbytes > bytes) wrbytes = bytes; if (fd >= 0 && write(fd, hbp->data, wrbytes) != wrbytes) sysfatal("write error on %s: %r", fname); putreadblks(ar, blksread); bytes -= wrbytes; } if (bytes > 0) fprint(2, "%s: %lld bytes uncopied at eof; %s not fully extracted\n", argv0, bytes, fname); if (fd >= 0) { /* * directories should be wstated after we're done * creating files in them. */ if (settime) { Dir nd; nulldir(&nd); nd.mtime = mtime; dirfwstat(fd, &nd); if (isustar(hp)) { nulldir(&nd); nd.gid = hp->gname; dirfwstat(fd, &nd); } } close(fd); }}static voidskip(int ar, Hdr *hp, char *fname){ ulong blksleft, blksread; Hdr *hbp; for (blksleft = BYTES2TBLKS(arsize(hp)); blksleft > 0; blksleft -= blksread) { hbp = getblkrd(ar, Justnxthdr); if (hbp == nil) sysfatal("unexpected EOF on archive extracting %s", fname); blksread = gothowmany(blksleft); putreadblks(ar, blksread); }}static char *extract(char **argv){ int ar; char *longname; Hdr *hp; Compress *comp = nil; Pushstate ps; if (usefile) { ar = open(usefile, OREAD); comp = compmethod(usefile); } else ar = Stdin; if (comp) ar = push(ar, comp->decomp, Input, &ps); if (ar < 0) sysfatal("can't open archive %s: %r", usefile); while ((hp = readhdr(ar)) != nil) { longname = name(hp); if (match(longname, argv)) extract1(ar, hp, longname); else skip(ar, hp, longname); } if (comp) return pushclose(&ps); if (ar > Stderr) close(ar); return nil;}voidmain(int argc, char *argv[]){ int errflg = 0; char *ret = nil; fmtinstall('M', dirmodefmt); TARGBEGIN { case 'c': docreate++; verb = Replace; break; case 'f': usefile = EARGF(usage()); break; case 'g': argid = strtoul(EARGF(usage()), 0, 0); break; case 'k': keepexisting++; break; case 'm': /* compatibility */ settime = 0; break; case 'p': posix++; break; case 'P': posix = 0; break; case 'r': verb = Replace; break; case 'R': relative = 0; break; case 't': verb = Toc; break; case 'T': settime++; break; case 'u': aruid = strtoul(EARGF(usage()), 0, 0); break; case 'v': verbose++; break; case 'x': verb = Xtract; break; case 'z': docompress++; break; case '-': break; default: fprint(2, "tar: unknown letter %C\n", TARGC()); errflg++; break; } TARGEND if (argc < 0 || errflg) usage(); initblks(); switch (verb) { case Toc: case Xtract: ret = extract(argv); break; case Replace: if (getwd(origdir, sizeof origdir) == nil) strcpy(origdir, "/tmp"); ret = replace(argv); break; default: usage(); break; } exits(ret);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -