/* * Return (possibly in a static buffer) the name of the `source' for a * file. If we have `options source', or if the file is marked `always * source', this is always the path from the `file' line; otherwise we * get the .o from the obj-directory. */ static const char * srcpath(struct files *fi) { /* Always have source, don't support object dirs for kernel builds. */ struct nvlist *nv, *nv1; char *expand, *source; /* Search path list for files we will want to use */ if (fi->fi_nvpath->nv_next == NULL) { nv = fi->fi_nvpath; goto onlyone; } for (nv = fi->fi_nvpath; nv; nv = nv->nv_next) { expand = expandname(nv->nv_name); source = sourcepath(expand ? expand : nv->nv_name); if (access(source, R_OK) == 0) { /* XXX poolalloc() prevents freeing old nv_name */ if (expand) nv->nv_name = intern(expand); break; } free(expand); free(source); } if (nv == NULL) nv = fi->fi_nvpath; /* * Now that we know which path is selected, delete all the * other paths to skip the access() checks next time. */ while ((nv1 = fi->fi_nvpath)) { nv1 = nv1->nv_next; if (fi->fi_nvpath != nv) nvfree(fi->fi_nvpath); fi->fi_nvpath = nv1; } fi->fi_nvpath = nv; nv->nv_next = NULL; onlyone: return (nv->nv_name); }
/* function to expand path names: * * if name starts with the following symbols * * / unchanged * ~/ "home directory name" * ~fred "home directory path for fred" * ~fred/etc "home directory path for fred/etc" * * for all other cases look in the environment variables for the strings * between the /'s and expand them if found. Environment can include $ but * it is not necessary * * Arguments * namein - unexpanded path * nameout - expanded path (size FILENAME_MAX+1) * * Returns: * 1 == success, 0 == failure */ int expandpath(char *namein, char *nameout) { char tempbuf[FILENAME_MAX+1], tempb[FILENAME_MAX+1]; char *tokptr, *strptr = tempbuf, *slash; char *nameoutp = nameout; int len = FILENAME_MAX, tmp; if ( namein == NULL ) return 0; /* if (strlen(namein) > FILENAME_MAX) return 0; */ strncpy ( tempbuf, namein, FILENAME_MAX); *nameout = '\0'; /* nasty special case: namein starts with "/" and it gets lost so stick it in nameout */ if ( namein[0] == '/' ) { strcpy ( nameout, "/"); nameoutp++; len--; } while ( len > 0 && ( tokptr = strtok ( strptr, "/" )) ) { if ( expandname ( tempb, tokptr ) == 0 ) return 0; strncpy( nameoutp, tempb, len); tmp = strlen(tempb); nameoutp += tmp; len -= tmp + 1; if (len > 1) { strcpy(nameoutp++, "/"); } strptr = NULL; } if (NULL != (slash = strrchr ( nameout, '/' ))) *slash = '\0'; return 1; }
int ustar_rd(ARCHD *arcn, char *buf) { HD_USTAR *hd = (HD_USTAR *)buf; char *dest; int cnt = 0; dev_t devmajor; dev_t devminor; unsigned long long val; /* * we only get proper sized buffers */ if (ustar_id(buf, BLKMULT) < 0) return(-1); #ifndef SMALL reset: #endif memset(arcn, 0, sizeof(*arcn)); arcn->org_name = arcn->name; arcn->sb.st_nlink = 1; #ifndef SMALL /* Process Extended headers. */ if (hd->typeflag == XHDRTYPE || hd->typeflag == GHDRTYPE) { if (rd_xheader(arcn, hd->typeflag == GHDRTYPE, (off_t)asc_ul(hd->size, sizeof(hd->size), OCT)) < 0) return (-1); /* Update and check the ustar header. */ if (rd_wrbuf(buf, BLKMULT) != BLKMULT) return (-1); if (ustar_id(buf, BLKMULT) < 0) return(-1); /* if the next block is another extension, reset the values */ if (hd->typeflag == XHDRTYPE || hd->typeflag == GHDRTYPE) goto reset; } #endif if (!arcn->nlen) { /* * See if the filename is split into two parts. if, so join * the parts. We copy the prefix first and add a / between * the prefix and name. */ dest = arcn->name; if (*(hd->prefix) != '\0') { cnt = fieldcpy(dest, sizeof(arcn->name) - 1, hd->prefix, sizeof(hd->prefix)); dest += cnt; *dest++ = '/'; cnt++; } else cnt = 0; if (hd->typeflag != LONGLINKTYPE && hd->typeflag != LONGNAMETYPE) { arcn->nlen = cnt + expandname(dest, sizeof(arcn->name) - cnt, &gnu_name_string, hd->name, sizeof(hd->name)); } } if (!arcn->ln_nlen && hd->typeflag != LONGLINKTYPE && hd->typeflag != LONGNAMETYPE) { arcn->ln_nlen = expandname(arcn->ln_name, sizeof(arcn->ln_name), &gnu_link_string, hd->linkname, sizeof(hd->linkname)); } /* * follow the spec to the letter. we should only have mode bits, strip * off all other crud we may be passed. */ arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode, sizeof(hd->mode), OCT) & 0xfff); arcn->sb.st_size = (off_t)asc_ull(hd->size, sizeof(hd->size), OCT); val = asc_ull(hd->mtime, sizeof(hd->mtime), OCT); if ((time_t)val < 0 || (time_t)val != val) arcn->sb.st_mtime = INT_MAX; /* XXX 2038 */ else arcn->sb.st_mtime = val; arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime; /* * If we can find the ascii names for gname and uname in the password * and group files we will use the uid's and gid they bind. Otherwise * we use the uid and gid values stored in the header. (This is what * the posix spec wants). */ hd->gname[sizeof(hd->gname) - 1] = '\0'; if (Nflag || gid_name(hd->gname, &(arcn->sb.st_gid)) < 0) arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT); hd->uname[sizeof(hd->uname) - 1] = '\0'; if (Nflag || uid_name(hd->uname, &(arcn->sb.st_uid)) < 0) arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT); /* * set the defaults, these may be changed depending on the file type */ arcn->pad = 0; arcn->skip = 0; arcn->sb.st_rdev = (dev_t)0; /* * set the mode and PAX type according to the typeflag in the header */ switch (hd->typeflag) { case FIFOTYPE: arcn->type = PAX_FIF; arcn->sb.st_mode |= S_IFIFO; break; case DIRTYPE: arcn->type = PAX_DIR; arcn->sb.st_mode |= S_IFDIR; arcn->sb.st_nlink = 2; /* * Some programs that create ustar archives append a '/' * to the pathname for directories. This clearly violates * ustar specs, but we will silently strip it off anyway. */ if (arcn->name[arcn->nlen - 1] == '/') arcn->name[--arcn->nlen] = '\0'; break; case BLKTYPE: case CHRTYPE: /* * this type requires the rdev field to be set. */ if (hd->typeflag == BLKTYPE) { arcn->type = PAX_BLK; arcn->sb.st_mode |= S_IFBLK; } else { arcn->type = PAX_CHR; arcn->sb.st_mode |= S_IFCHR; } devmajor = (dev_t)asc_ul(hd->devmajor,sizeof(hd->devmajor),OCT); devminor = (dev_t)asc_ul(hd->devminor,sizeof(hd->devminor),OCT); arcn->sb.st_rdev = TODEV(devmajor, devminor); break; case SYMTYPE: case LNKTYPE: if (hd->typeflag == SYMTYPE) { arcn->type = PAX_SLK; arcn->sb.st_mode |= S_IFLNK; } else { arcn->type = PAX_HLK; /* * so printing looks better */ arcn->sb.st_mode |= S_IFREG; arcn->sb.st_nlink = 2; } break; case LONGLINKTYPE: case LONGNAMETYPE: /* * GNU long link/file; we tag these here and let the * pax internals deal with it -- too ugly otherwise. */ arcn->type = hd->typeflag == LONGLINKTYPE ? PAX_GLL : PAX_GLF; arcn->pad = TAR_PAD(arcn->sb.st_size); arcn->skip = arcn->sb.st_size; break; case CONTTYPE: case AREGTYPE: case REGTYPE: default: /* * these types have file data that follows. Set the skip and * pad fields. */ arcn->type = PAX_REG; arcn->pad = TAR_PAD(arcn->sb.st_size); arcn->skip = arcn->sb.st_size; arcn->sb.st_mode |= S_IFREG; break; } return(0); }
int tar_rd(ARCHD *arcn, char *buf) { HD_TAR *hd; unsigned long long val; char *pt; /* * we only get proper sized buffers passed to us */ if (tar_id(buf, BLKMULT) < 0) return(-1); memset(arcn, 0, sizeof(*arcn)); arcn->org_name = arcn->name; arcn->sb.st_nlink = 1; /* * copy out the name and values in the stat buffer */ hd = (HD_TAR *)buf; if (hd->linkflag != LONGLINKTYPE && hd->linkflag != LONGNAMETYPE) { arcn->nlen = expandname(arcn->name, sizeof(arcn->name), &gnu_name_string, hd->name, sizeof(hd->name)); arcn->ln_nlen = expandname(arcn->ln_name, sizeof(arcn->ln_name), &gnu_link_string, hd->linkname, sizeof(hd->linkname)); } arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode,sizeof(hd->mode),OCT) & 0xfff); arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT); arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT); arcn->sb.st_size = (off_t)asc_ull(hd->size, sizeof(hd->size), OCT); val = asc_ull(hd->mtime, sizeof(hd->mtime), OCT); if ((time_t)val < 0 || (time_t)val != val) arcn->sb.st_mtime = INT_MAX; /* XXX 2038 */ else arcn->sb.st_mtime = val; arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime; /* * have to look at the last character, it may be a '/' and that is used * to encode this as a directory */ pt = &(arcn->name[arcn->nlen - 1]); arcn->pad = 0; arcn->skip = 0; switch (hd->linkflag) { case SYMTYPE: /* * symbolic link, need to get the link name and set the type in * the st_mode so -v printing will look correct. */ arcn->type = PAX_SLK; arcn->sb.st_mode |= S_IFLNK; break; case LNKTYPE: /* * hard link, need to get the link name, set the type in the * st_mode and st_nlink so -v printing will look better. */ arcn->type = PAX_HLK; arcn->sb.st_nlink = 2; /* * no idea of what type this thing really points at, but * we set something for printing only. */ arcn->sb.st_mode |= S_IFREG; break; case LONGLINKTYPE: case LONGNAMETYPE: /* * GNU long link/file; we tag these here and let the * pax internals deal with it -- too ugly otherwise. */ arcn->type = hd->linkflag == LONGLINKTYPE ? PAX_GLL : PAX_GLF; arcn->pad = TAR_PAD(arcn->sb.st_size); arcn->skip = arcn->sb.st_size; break; case DIRTYPE: /* * It is a directory, set the mode for -v printing */ arcn->type = PAX_DIR; arcn->sb.st_mode |= S_IFDIR; arcn->sb.st_nlink = 2; break; case AREGTYPE: case REGTYPE: default: /* * If we have a trailing / this is a directory and NOT a file. */ arcn->ln_name[0] = '\0'; arcn->ln_nlen = 0; if (*pt == '/') { /* * it is a directory, set the mode for -v printing */ arcn->type = PAX_DIR; arcn->sb.st_mode |= S_IFDIR; arcn->sb.st_nlink = 2; } else { /* * have a file that will be followed by data. Set the * skip value to the size field and calculate the size * of the padding. */ arcn->type = PAX_REG; arcn->sb.st_mode |= S_IFREG; arcn->pad = TAR_PAD(arcn->sb.st_size); arcn->skip = arcn->sb.st_size; } break; } /* * strip off any trailing slash. */ if (*pt == '/') { *pt = '\0'; --arcn->nlen; } return(0); }