3 * File state machine to handle a payload from a package.
13 /*@access rpmTransactionSet @*/
18 #define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
22 /* XXX Failure to remove is not (yet) cause for failure. */
23 /*@-exportlocal -exportheadervar@*/
24 int strict_erasures = 0;
25 /*@=exportlocal =exportheadervar@*/
27 rpmTransactionSet fsmGetTs(const FSM_t fsm) {
28 const FSMI_t iter = fsm->iter;
30 return (iter ? iter->ts : NULL);
34 TFI_t fsmGetFi(const FSM_t fsm)
36 const FSMI_t iter = fsm->iter;
38 return (iter ? iter->fi : NULL);
42 #define SUFFIX_RPMORIG ".rpmorig"
43 #define SUFFIX_RPMSAVE ".rpmsave"
44 #define SUFFIX_RPMNEW ".rpmnew"
47 * Build path to file from file info, ornamented with subdir and suffix.
48 * @param fsm file state machine data
49 * @param st file stat info
50 * @param subdir subdir to use (NULL disables)
51 * @param suffix suffix to use (NULL disables)
52 * @retval path to file
54 static /*@only@*//*@null@*/
55 const char * fsmFsPath(/*@special@*/ /*@null@*/ const FSM_t fsm,
56 /*@null@*/ const struct stat * st,
57 /*@null@*/ const char * subdir,
58 /*@null@*/ const char * suffix)
59 /*@uses fsm->dirName, fsm->baseName */
61 const char * s = NULL;
66 /*@-nullpass@*/ /* LCL: subdir/suffix != NULL */
67 nb = strlen(fsm->dirName) +
68 (st && subdir && !S_ISDIR(st->st_mode) ? strlen(subdir) : 0) +
69 (st && suffix && !S_ISDIR(st->st_mode) ? strlen(suffix) : 0) +
70 strlen(fsm->baseName) + 1;
72 t = stpcpy(t, fsm->dirName);
73 if (st && subdir && !S_ISDIR(st->st_mode))
74 t = stpcpy(t, subdir);
75 t = stpcpy(t, fsm->baseName);
76 if (st && suffix && !S_ISDIR(st->st_mode))
77 t = stpcpy(t, suffix);
84 * Destroy file info iterator.
85 * @param p file info iterator
88 static /*@null@*/ void * mapFreeIterator(/*@only@*//*@null@*/const void * p)
90 return _free((void *)p);
94 * Create file info iterator.
95 * @param a transaction set
96 * @param b transaction element file info
97 * @return file info iterator
100 mapInitIterator(/*@kept@*/ const void * a, /*@kept@*/ const void * b)
102 rpmTransactionSet ts = (void *)a;
103 TFI_t fi = (void *)b;
106 iter = xcalloc(1, sizeof(*iter));
111 iter->reverse = (fi->type == TR_REMOVED && fi->action != FA_COPYOUT);
112 iter->i = (iter->reverse ? (fi->fc - 1) : 0);
113 iter->isave = iter->i;
118 * Return next index into file info.
119 * @param a file info iterator
120 * @return next index, -1 on termination
122 static int mapNextIterator(void * a) {
124 const TFI_t fi = iter->fi;
128 if (iter->i >= 0) i = iter->i--;
130 if (iter->i < fi->fc) i = iter->i++;
138 static int cpioStrCmp(const void * a, const void * b)
141 const char * afn = *(const char **)a;
142 const char * bfn = *(const char **)b;
144 /* Match rpm-4.0 payloads with ./ prefixes. */
145 if (afn[0] == '.' && afn[1] == '/') afn += 2;
146 if (bfn[0] == '.' && bfn[1] == '/') bfn += 2;
148 /* If either path is absolute, make it relative. */
149 if (afn[0] == '/') afn += 1;
150 if (bfn[0] == '/') bfn += 1;
152 return strcmp(afn, bfn);
156 * Locate archive path in file info.
157 * @param a file info iterator
158 * @param fsmPath archive path
159 * @return index into file info, -1 if archive path was not found
161 static int mapFind(void * a, const char * fsmPath)
165 const TFI_t fi = iter->fi;
168 if (fi && fi->fc > 0 && fi->apath && fsmPath && *fsmPath) {
169 const char ** p = NULL;
171 if (fi->apath != NULL)
172 p = bsearch(&fsmPath, fi->apath, fi->fc, sizeof(fsmPath),
175 fprintf(stderr, "*** not mapped %s\n", fsmPath);
177 iter->i = p - fi->apath;
178 ix = mapNextIterator(iter);
185 * Directory name iterator.
187 typedef struct dnli_s {
188 /*@dependent@*/ TFI_t fi;
189 /*@only@*/ /*@null@*/ char * active;
196 * Destroy directory name iterator.
197 * @param a directory name iterator
198 * @retval NULL always
200 static /*@null@*/ void * dnlFreeIterator(/*@only@*//*@null@*/ const void * a)
204 DNLI_t dnli = (void *)a;
205 if (dnli->active) free(dnli->active);
212 static inline int dnlCount(const DNLI_t dnli)
215 return (dnli ? dnli->fi->dc : 0);
220 static inline int dnlIndex(const DNLI_t dnli)
223 return (dnli ? dnli->isave : -1);
227 * Create directory name iterator.
228 * @param fsm file state machine data
229 * @param reverse traverse directory names in reverse order?
230 * @return directory name iterator
232 static /*@only@*/ void * dnlInitIterator(/*@special@*/ const FSM_t fsm,
234 /*@uses fsm->iter @*/
237 TFI_t fi = fsmGetFi(fsm);
243 dnli = xcalloc(1, sizeof(*dnli));
245 dnli->reverse = reverse;
246 dnli->i = (reverse ? fi->dc : 0);
249 dnli->active = xcalloc(fi->dc, sizeof(*dnli->active));
251 /* Identify parent directories not skipped. */
252 for (i = 0; i < fi->fc; i++)
253 if (!XFA_SKIPPING(fi->actions[i])) dnli->active[fi->dil[i]] = 1;
255 /* Exclude parent directories that are explicitly included. */
256 for (i = 0; i < fi->fc; i++) {
257 int dil, dnlen, bnlen;
259 if (!S_ISDIR(fi->fmodes[i]))
263 dnlen = strlen(fi->dnl[dil]);
264 bnlen = strlen(fi->bnl[i]);
266 for (j = 0; j < fi->dc; j++) {
270 if (!dnli->active[j] || j == dil) continue;
273 if (jlen != (dnlen+bnlen+1)) continue;
274 if (strncmp(dnl, fi->dnl[dil], dnlen)) continue;
275 if (strncmp(dnl+dnlen, fi->bnl[i], bnlen)) continue;
276 if (dnl[dnlen+bnlen] != '/' || dnl[dnlen+bnlen+1] != '\0')
278 /* This directory is included in the package. */
280 /*@innerbreak@*/ break;
284 /* Print only once per package. */
287 for (i = 0; i < fi->dc; i++) {
288 if (!dnli->active[i]) continue;
291 rpmMessage(RPMMESS_DEBUG,
292 _("========= Directories not explictly included in package:\n"));
294 rpmMessage(RPMMESS_DEBUG, _("%9d %s\n"), i, fi->dnl[i]);
297 rpmMessage(RPMMESS_DEBUG, "=========\n");
304 * Return next directory name (from file info).
305 * @param dnli directory name iterator
306 * @return next directory name
308 static /*@observer@*/ const char * dnlNextIterator(/*@null@*/ DNLI_t dnli)
311 const char * dn = NULL;
319 i = (!dnli->reverse ? dnli->i++ : --dnli->i);
320 } while (i >= 0 && i < fi->dc && !dnli->active[i]);
322 if (i >= 0 && i < fi->dc)
332 * Save hard link in chain.
333 * @param fsm file state machine data
334 * @return Is chain only partially filled?
337 static int saveHardLink(/*@special@*/ /*@partial@*/ FSM_t fsm)
338 /*@uses fsm->links, fsm->ix, fsm->sb, fsm->goal, fsm->nsuffix @*/
339 /*@defines fsm->li @*/
340 /*@releases fsm->path @*/
343 struct stat * st = &fsm->sb;
348 /* Find hard link set. */
349 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
350 if (fsm->li->inode == st->st_ino && fsm->li->dev == st->st_dev)
354 /* New hard link encountered, add new link to set. */
355 if (fsm->li == NULL) {
356 fsm->li = xcalloc(1, sizeof(*fsm->li));
357 fsm->li->next = NULL;
358 fsm->li->nlink = st->st_nlink;
359 fsm->li->dev = st->st_dev;
360 fsm->li->inode = st->st_ino;
361 fsm->li->linkIndex = -1;
362 fsm->li->createdPath = -1;
364 fsm->li->filex = xcalloc(st->st_nlink, sizeof(fsm->li->filex[0]));
365 memset(fsm->li->filex, -1, (st->st_nlink * sizeof(fsm->li->filex[0])));
366 fsm->li->nsuffix = xcalloc(st->st_nlink, sizeof(*fsm->li->nsuffix));
368 if (fsm->goal == FSM_PKGBUILD)
369 fsm->li->linksLeft = st->st_nlink;
370 if (fsm->goal == FSM_PKGINSTALL)
371 fsm->li->linksLeft = 0;
374 fsm->li->next = fsm->links;
376 fsm->links = fsm->li;
379 if (fsm->goal == FSM_PKGBUILD) --fsm->li->linksLeft;
380 fsm->li->filex[fsm->li->linksLeft] = fsm->ix;
381 /*@-observertrans -dependenttrans@*/
382 fsm->li->nsuffix[fsm->li->linksLeft] = fsm->nsuffix;
383 /*@=observertrans =dependenttrans@*/
384 if (fsm->goal == FSM_PKGINSTALL) fsm->li->linksLeft++;
387 fprintf(stderr, "*** %p link[%d:%d] %d filex %d %s\n", fsm->li, fsm->li->linksLeft, st->st_nlink, (int)st->st_size, fsm->li->filex[fsm->li->linksLeft], fsm->li->files[fsm->li->linksLeft]);
390 if (fsm->goal == FSM_PKGBUILD)
391 return (fsm->li->linksLeft > 0);
393 if (fsm->goal != FSM_PKGINSTALL)
396 if (!(st->st_size || fsm->li->linksLeft == st->st_nlink))
399 /* Here come the bits, time to choose a non-skipped file name. */
400 { TFI_t fi = fsmGetFi(fsm);
402 for (j = fsm->li->linksLeft - 1; j >= 0; j--) {
403 ix = fsm->li->filex[j];
404 if (ix < 0 || XFA_SKIPPING(fi->actions[ix]))
410 /* Are all links skipped or not encountered yet? */
412 return 1; /* XXX W2DO? */
414 /* Save the non-skipped file name and map index. */
415 fsm->li->linkIndex = j;
416 fsm->path = _free(fsm->path);
418 rc = fsmStage(fsm, FSM_MAP);
419 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
426 * Destroy set of hard links.
427 * @param li set of hard links
429 static /*@null@*/ void * freeHardLink(/*@only@*/ /*@null@*/ struct hardLink * li)
433 li->nsuffix = _free(li->nsuffix); /* XXX elements are shared */
434 li->filex = _free(li->filex);
441 FSM_t fsm = xcalloc(1, sizeof(*fsm));
445 FSM_t freeFSM(FSM_t fsm)
448 fsm->path = _free(fsm->path);
449 while ((fsm->li = fsm->links) != NULL) {
450 fsm->links = fsm->li->next;
451 fsm->li->next = NULL;
452 fsm->li = freeHardLink(fsm->li);
454 fsm->dnlx = _free(fsm->dnlx);
455 fsm->ldn = _free(fsm->ldn);
456 fsm->iter = mapFreeIterator(fsm->iter);
461 int fsmSetup(FSM_t fsm, fileStage goal,
462 const rpmTransactionSet ts, const TFI_t fi, FD_t cfd,
463 unsigned int * archiveSize, const char ** failedFile)
470 fsm->cfd = fdLink(cfd, "persist (fsm)");
471 pos = fdGetCpioPos(fsm->cfd);
472 fdSetCpioPos(fsm->cfd, 0);
474 fsm->iter = mapInitIterator(ts, fi);
476 if (fsm->goal == FSM_PKGINSTALL) {
477 if (ts && ts->notify) {
478 (void)ts->notify(fi->h, RPMCALLBACK_INST_START, 0, fi->archiveSize,
479 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
484 fsm->archiveSize = archiveSize;
485 if (fsm->archiveSize)
486 *fsm->archiveSize = 0;
487 fsm->failedFile = failedFile;
489 *fsm->failedFile = NULL;
492 memset(fsm->sufbuf, 0, sizeof(fsm->sufbuf));
493 if (fsm->goal == FSM_PKGINSTALL) {
494 if (ts && ts->id > 0)
495 sprintf(fsm->sufbuf, ";%08x", (unsigned)ts->id);
499 rc = fsmStage(fsm, FSM_CREATE);
501 rc = fsmStage(fsm, fsm->goal);
503 if (fsm->archiveSize && rc == 0)
504 *fsm->archiveSize = (fdGetCpioPos(fsm->cfd) - pos);
509 int fsmTeardown(FSM_t fsm) {
513 rc = fsmStage(fsm, FSM_DESTROY);
515 fsm->iter = mapFreeIterator(fsm->iter);
517 fsm->cfd = fdFree(fsm->cfd, "persist (fsm)");
520 fsm->failedFile = NULL;
521 /*@-nullstate@*/ /* FIX: fsm->iter null annotation? */
526 int fsmMapPath(FSM_t fsm)
528 TFI_t fi = fsmGetFi(fsm); /* XXX const except for fstates */
535 fsm->action = FA_UNKNOWN;
539 if (fi && i >= 0 && i < fi->fc) {
541 fsm->astriplen = fi->astriplen;
542 fsm->action = (fi->actions ? fi->actions[i] : fi->action);
543 fsm->fflags = (fi->fflags ? fi->fflags[i] : fi->flags);
544 fsm->mapFlags = (fi->fmapflags ? fi->fmapflags[i] : fi->mapflags);
546 /* src rpms have simple base name in payload. */
547 fsm->dirName = fi->dnl[fi->dil[i]];
548 fsm->baseName = fi->bnl[i];
550 switch (fsm->action) {
553 case FA_SKIPMULTILIB: /* XXX RPMFILE_STATE_MULTILIB? */
554 fprintf(stderr, "*** %s:%s %s\n", fiTypeString(fi), fileActionString(fsm->action), (fsm->path ? fsm->path : ""));
557 fprintf(stderr, "*** %s:%s %s\n", fiTypeString(fi), fileActionString(fsm->action), (fsm->path ? fsm->path : ""));
564 assert(fi->type == TR_ADDED);
568 if (fi->fstates && fi->type == TR_ADDED)
569 fi->fstates[i] = RPMFILE_STATE_NOTINSTALLED;
572 case FA_SKIPNETSHARED:
573 if (fi->fstates && fi->type == TR_ADDED)
574 fi->fstates[i] = RPMFILE_STATE_NETSHARED;
580 fsm->osuffix = SUFFIX_RPMORIG;
583 fsm->osuffix = SUFFIX_RPMSAVE;
589 assert(fi->type == TR_ADDED);
590 fsm->nsuffix = SUFFIX_RPMNEW;
594 assert(fi->type == TR_ADDED);
595 fsm->osuffix = SUFFIX_RPMSAVE;
598 assert(fi->type == TR_REMOVED);
601 fprintf(stderr, "*** %s:%s %s\n", fiTypeString(fi), fileActionString(fsm->action), (fsm->path ? fsm->path : ""));
605 if ((fsm->mapFlags & CPIO_MAP_PATH) || fsm->nsuffix) {
606 const struct stat * st = &fsm->sb;
607 fsm->path = _free(fsm->path);
608 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
609 fsm->path = fsmFsPath(fsm, st, fsm->subdir,
610 (fsm->suffix ? fsm->suffix : fsm->nsuffix));
617 int fsmMapAttrs(FSM_t fsm)
619 struct stat * st = &fsm->sb;
620 TFI_t fi = fsmGetFi(fsm);
623 if (fi && i >= 0 && i < fi->fc) {
625 (S_ISDIR(st->st_mode) ? fi->dperms : fi->fperms);
627 (fi->fmodes ? fi->fmodes[i] : perms);
629 (fi->fuids ? fi->fuids[i] : fi->uid); /* XXX chmod u-s */
631 (fi->fgids ? fi->fgids[i] : fi->gid); /* XXX chmod g-s */
633 if (fsm->mapFlags & CPIO_MAP_MODE)
634 st->st_mode = (st->st_mode & S_IFMT) | finalMode;
635 if (fsm->mapFlags & CPIO_MAP_UID)
636 st->st_uid = finalUid;
637 if (fsm->mapFlags & CPIO_MAP_GID)
638 st->st_gid = finalGid;
640 fsm->fmd5sum = (fi->fmd5s ? fi->fmd5s[i] : NULL);
647 * Create file from payload stream.
648 * @todo Legacy: support brokenEndian MD5 checks?
649 * @param fsm file state machine data
650 * @return 0 on success
652 static int expandRegular(/*@special@*/ FSM_t fsm)
654 /*@modifies fsm, fileSystem @*/
656 const char * fmd5sum;
657 const struct stat * st = &fsm->sb;
658 int left = st->st_size;
661 rc = fsmStage(fsm, FSM_WOPEN);
665 /* XXX md5sum's will break on repackaging that includes modified files. */
666 fmd5sum = fsm->fmd5sum;
668 /* XXX This doesn't support brokenEndian checks. */
669 if (st->st_size > 0 && fmd5sum)
670 fdInitMD5(fsm->wfd, 0);
674 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
675 rc = fsmStage(fsm, FSM_DREAD);
679 rc = fsmStage(fsm, FSM_WRITE);
685 /* don't call this with fileSize == fileComplete */
687 (void) fsmStage(fsm, FSM_NOTIFY);
690 if (st->st_size > 0 && fmd5sum) {
691 const char * md5sum = NULL;
693 (void) Fflush(fsm->wfd);
694 fdFiniMD5(fsm->wfd, (void **)&md5sum, NULL, 1);
696 if (md5sum == NULL) {
697 rc = CPIOERR_MD5SUM_MISMATCH;
699 if (strcmp(md5sum, fmd5sum))
700 rc = CPIOERR_MD5SUM_MISMATCH;
701 md5sum = _free(md5sum);
706 (void) fsmStage(fsm, FSM_WCLOSE);
711 * Write next item to payload stream.
712 * @param fsm file state machine data
713 * @param writeData should data be written?
714 * @return 0 on success
716 static int writeFile(/*@special@*/ FSM_t fsm, int writeData)
717 /*@uses fsm->path, fsm->opath, fsm->sb, fsm->osb, fsm->cfd @*/
718 /*@modifies fsm, fileSystem @*/
720 const char * path = fsm->path;
721 const char * opath = fsm->opath;
722 struct stat * st = &fsm->sb;
723 struct stat * ost = &fsm->osb;
724 size_t pos = fdGetCpioPos(fsm->cfd);
725 char * symbuf = NULL;
729 st->st_size = (writeData ? ost->st_size : 0);
730 if (S_ISDIR(st->st_mode)) {
732 } else if (S_ISLNK(st->st_mode)) {
734 * While linux puts the size of a symlink in the st_size field,
735 * I don't think that's a specified standard.
737 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
738 rc = fsmStage(fsm, FSM_READLINK);
740 st->st_size = fsm->rdnb;
741 symbuf = alloca_strdup(fsm->rdbuf); /* XXX save readlink return. */
744 if (fsm->mapFlags & CPIO_MAP_ABSOLUTE) {
745 int nb = strlen(fsm->dirName) + strlen(fsm->baseName) + sizeof(".");
746 char * t = alloca(nb);
749 if (fsm->mapFlags & CPIO_MAP_ADDDOT)
751 t = stpcpy( stpcpy(t, fsm->dirName), fsm->baseName);
752 } else if (fsm->mapFlags & CPIO_MAP_PATH) {
753 TFI_t fi = fsmGetFi(fsm);
755 (fi->apath ? fi->apath[fsm->ix] + fi->striplen : fi->bnl[fsm->ix]);
758 rc = fsmStage(fsm, FSM_HWRITE);
762 if (writeData && S_ISREG(st->st_mode)) {
765 void * mapped = (void *)-1;
769 rc = fsmStage(fsm, FSM_ROPEN);
772 /* XXX unbuffered mmap generates *lots* of fdio debugging */
775 mapped = mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, Fileno(fsm->rfd), 0);
776 if (mapped != (void *)-1) {
778 fsm->rdbuf = (char *) mapped;
779 fsm->rdlen = nmapped = st->st_size;
787 if (mapped != (void *)-1) {
792 fsm->rdlen = (left > fsm->rdsize ? fsm->rdsize : left),
793 rc = fsmStage(fsm, FSM_READ);
797 /* XXX DWRITE uses rdnb for I/O length. */
798 rc = fsmStage(fsm, FSM_DWRITE);
805 if (mapped != (void *)-1) {
806 /*@-noeffect@*/ (void) munmap(mapped, nmapped) /*@=noeffect@*/;
811 } else if (writeData && S_ISLNK(st->st_mode)) {
812 /* XXX DWRITE uses rdnb for I/O length. */
813 strcpy(fsm->rdbuf, symbuf); /* XXX restore readlink buffer. */
814 fsm->rdnb = strlen(symbuf);
815 rc = fsmStage(fsm, FSM_DWRITE);
819 rc = fsmStage(fsm, FSM_PAD);
822 { const rpmTransactionSet ts = fsmGetTs(fsm);
823 TFI_t fi = fsmGetFi(fsm);
824 if (ts && fi && ts->notify) {
825 size_t size = (fdGetCpioPos(fsm->cfd) - pos);
826 /*@-modunconnomods@*/
827 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS, size, size,
828 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
829 /*@=modunconnomods@*/
837 (void) fsmStage(fsm, FSM_RCLOSE);
838 /*@-dependenttrans@*/
841 /*@=dependenttrans@*/
846 * Write set of linked files to payload stream.
847 * @param fsm file state machine data
848 * @return 0 on success
850 static int writeLinkedFile(/*@special@*/ FSM_t fsm)
851 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->li, fsm->failedFile @*/
852 /*@modifies fsm, fileSystem @*/
854 const char * path = fsm->path;
855 const char * nsuffix = fsm->nsuffix;
856 int iterIndex = fsm->ix;
865 for (i = fsm->li->nlink - 1; i >= 0; i--) {
866 if (fsm->li->filex[i] < 0) continue;
868 fsm->ix = fsm->li->filex[i];
869 rc = fsmStage(fsm, FSM_MAP);
871 /* Write data after last link. */
872 rc = writeFile(fsm, (i == 0));
873 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
875 *fsm->failedFile = xstrdup(fsm->path);
878 fsm->path = _free(fsm->path);
879 fsm->li->filex[i] = -1;
883 fsm->nsuffix = nsuffix;
889 * Create pending hard links to existing file.
890 * @param fsm file state machine data
891 * @return 0 on success
893 static int fsmMakeLinks(/*@special@*/ FSM_t fsm)
894 /*@uses fsm->path, fsm->opath, fsm->nsuffix, fsm->ix, fsm->li @*/
895 /*@modifies fsm, fileSystem @*/
897 const char * path = fsm->path;
898 const char * opath = fsm->opath;
899 const char * nsuffix = fsm->nsuffix;
900 int iterIndex = fsm->ix;
910 fsm->ix = fsm->li->filex[fsm->li->createdPath];
911 rc = fsmStage(fsm, FSM_MAP);
912 fsm->opath = fsm->path;
914 for (i = 0; i < fsm->li->nlink; i++) {
915 if (fsm->li->filex[i] < 0) continue;
916 if (i == fsm->li->createdPath) continue;
918 fsm->ix = fsm->li->filex[i];
919 rc = fsmStage(fsm, FSM_MAP);
920 rc = fsmStage(fsm, FSM_VERIFY);
922 if (rc != CPIOERR_LSTAT_FAILED) break;
924 /* XXX link(fsm->opath, fsm->path) */
925 rc = fsmStage(fsm, FSM_LINK);
926 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
928 *fsm->failedFile = xstrdup(fsm->path);
931 fsm->li->linksLeft--;
933 fsm->opath = _free(fsm->opath);
936 fsm->nsuffix = nsuffix;
943 * Commit hard linked file set atomically.
944 * @param fsm file state machine data
945 * @return 0 on success
947 static int fsmCommitLinks(/*@special@*/ FSM_t fsm)
948 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->sb,
949 fsm->li, fsm->links @*/
950 /*@modifies fsm, fileSystem @*/
952 const char * path = fsm->path;
953 const char * nsuffix = fsm->nsuffix;
954 int iterIndex = fsm->ix;
955 struct stat * st = &fsm->sb;
963 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
964 if (fsm->li->inode == st->st_ino && fsm->li->dev == st->st_dev)
968 for (i = 0; i < fsm->li->nlink; i++) {
969 if (fsm->li->filex[i] < 0) continue;
970 fsm->ix = fsm->li->filex[i];
971 rc = fsmStage(fsm, FSM_MAP);
972 rc = fsmStage(fsm, FSM_COMMIT);
973 fsm->path = _free(fsm->path);
974 fsm->li->filex[i] = -1;
978 fsm->nsuffix = nsuffix;
984 * Remove (if created) directories not explicitly included in package.
985 * @param fsm file state machine data
986 * @return 0 on success
989 static int fsmRmdirs(/*@special@*/ FSM_t fsm)
990 /*@uses fsm->path, fsm->dnlx, fsm->ldn, fsm->rdbuf, fsm->iter @*/
991 /*@modifies fsm, fileSystem @*/
993 const char * path = fsm->path;
994 void * dnli = dnlInitIterator(fsm, 1);
995 char * dn = fsm->rdbuf;
996 int dc = dnlCount(dnli);
1001 /*@-observertrans -dependenttrans@*/
1002 if (fsm->ldn != NULL && fsm->dnlx != NULL)
1003 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1004 int dnlen = strlen(fsm->path);
1007 dc = dnlIndex(dnli);
1008 if (fsm->dnlx[dc] < 1 || fsm->dnlx[dc] >= dnlen)
1011 /* Copy to avoid const on fsm->path. */
1012 te = stpcpy(dn, fsm->path) - 1;
1015 /* Remove generated directories. */
1019 rc = fsmStage(fsm, FSM_RMDIR);
1023 /*@innerbreak@*/ break;
1025 } while ((te - dn) > fsm->dnlx[dc]);
1027 dnli = dnlFreeIterator(dnli);
1028 /*@=observertrans =dependenttrans@*/
1036 * Create (if necessary) directories not explicitly included in package.
1037 * @param fsm file state machine data
1038 * @return 0 on success
1040 static int fsmMkdirs(/*@special@*/ FSM_t fsm)
1041 /*@uses fsm->path, fsm->sb, fsm->osb, fsm->rdbuf, fsm->iter,
1042 fsm->ldn, fsm->ldnlen, fsm->ldnalloc @*/
1043 /*@defines fsm->dnlx, fsm->ldn @*/
1044 /*@modifies fsm, fileSystem @*/
1046 struct stat * st = &fsm->sb;
1047 struct stat * ost = &fsm->osb;
1048 const char * path = fsm->path;
1049 mode_t st_mode = st->st_mode;
1050 void * dnli = dnlInitIterator(fsm, 0);
1051 char * dn = fsm->rdbuf;
1052 int dc = dnlCount(dnli);
1059 fsm->dnlx = (dc ? xcalloc(dc, sizeof(*fsm->dnlx)) : NULL);
1060 /*@-observertrans -dependenttrans@*/
1061 if (fsm->dnlx != NULL)
1062 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1063 int dnlen = strlen(fsm->path);
1066 dc = dnlIndex(dnli);
1067 if (dc < 0) continue;
1068 fsm->dnlx[dc] = dnlen;
1072 /*@-compdef -nullpass@*/ /* FIX: fsm->ldn not defined ??? */
1073 if (dnlen <= fsm->ldnlen && !strcmp(fsm->path, fsm->ldn))
1075 /*@=compdef =nullpass@*/
1077 /* Copy to avoid const on fsm->path. */
1078 (void) stpcpy(dn, fsm->path);
1081 /* Assume '/' directory exists, otherwise "mkdir -p" if non-existent. */
1082 for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
1083 if (*te != '/') continue;
1087 /* Already validated? */
1088 /*@-usedef -compdef -nullpass -nullderef@*/
1089 if (i < fsm->ldnlen &&
1090 (fsm->ldn[i] == '/' || fsm->ldn[i] == '\0') &&
1091 !strncmp(fsm->path, fsm->ldn, i))
1094 /* Move pre-existing path marker forward. */
1095 fsm->dnlx[dc] = (te - dn);
1098 /*@=usedef =compdef =nullpass =nullderef@*/
1100 /* Validate next component of path. */
1101 rc = fsmStage(fsm, FSM_LSTAT);
1104 /* Directory already exists? */
1105 if (rc == 0 && S_ISDIR(ost->st_mode)) {
1106 /* Move pre-existing path marker forward. */
1107 fsm->dnlx[dc] = (te - dn);
1108 } else if (rc == CPIOERR_LSTAT_FAILED) {
1109 TFI_t fi = fsmGetFi(fsm);
1111 st->st_mode = S_IFDIR | (fi->dperms & 07777);
1112 rc = fsmStage(fsm, FSM_MKDIR);
1114 rpmMessage(RPMMESS_WARNING,
1115 _("%s directory created with perms %04o.\n"),
1116 fsm->path, (unsigned)(st->st_mode & 07777));
1120 /*@innerbreak@*/ break;
1124 /* Save last validated path. */
1125 if (fsm->ldnalloc < (dnlen + 1)) {
1126 fsm->ldnalloc = dnlen + 100;
1127 fsm->ldn = xrealloc(fsm->ldn, fsm->ldnalloc);
1129 if (fsm->ldn != NULL) { /* XXX can't happen */
1130 strcpy(fsm->ldn, fsm->path);
1131 fsm->ldnlen = dnlen;
1134 dnli = dnlFreeIterator(dnli);
1135 /*@=observertrans =dependenttrans@*/
1138 st->st_mode = st_mode; /* XXX restore st->st_mode */
1144 int fsmStage(FSM_t fsm, fileStage stage)
1147 fileStage prevStage = fsm->stage;
1148 const char * const prev = fileStageString(prevStage);
1150 static int modulo = 4;
1151 const char * const cur = fileStageString(stage);
1152 struct stat * st = &fsm->sb;
1153 struct stat * ost = &fsm->osb;
1154 int saveerrno = errno;
1159 #define _fafilter(_a) \
1160 (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
1161 ? fileActionString(_a) : "")
1163 if (stage & FSM_DEAD) {
1165 } else if (stage & FSM_INTERNAL) {
1166 if (_fsm_debug && !(stage & FSM_SYSCALL))
1167 rpmMessage(RPMMESS_DEBUG, " %8s %06o%3d (%4d,%4d)%10d %s %s\n",
1169 (unsigned)st->st_mode, (int)st->st_nlink,
1170 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1171 (fsm->path ? fsm->path : ""),
1172 _fafilter(fsm->action));
1175 if (_fsm_debug || !(stage & FSM_VERBOSE))
1176 rpmMessage(RPMMESS_DEBUG, "%-8s %06o%3d (%4d,%4d)%10d %s %s\n",
1178 (unsigned)st->st_mode, (int)st->st_nlink,
1179 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1180 (fsm->path ? fsm->path + fsm->astriplen : ""),
1181 _fafilter(fsm->action));
1188 case FSM_PKGINSTALL:
1190 /* Clean fsm, free'ing memory. Read next archive header. */
1191 rc = fsmStage(fsm, FSM_INIT);
1193 /* Exit on end-of-payload. */
1194 if (rc == CPIOERR_HDR_TRAILER) {
1196 /*@loopbreak@*/ break;
1199 /* Exit on error. */
1202 (void) fsmStage(fsm, FSM_UNDO);
1203 /*@loopbreak@*/ break;
1206 /* Extract file from archive. */
1207 rc = fsmStage(fsm, FSM_PROCESS);
1209 (void) fsmStage(fsm, FSM_UNDO);
1210 /*@loopbreak@*/ break;
1213 /* Notify on success. */
1214 (void) fsmStage(fsm, FSM_NOTIFY);
1216 if (fsmStage(fsm, FSM_FINI))
1217 /*@loopbreak@*/ break;
1223 /* Clean fsm, free'ing memory. */
1224 rc = fsmStage(fsm, FSM_INIT);
1226 /* Exit on end-of-payload. */
1227 if (rc == CPIOERR_HDR_TRAILER) {
1229 /*@loopbreak@*/ break;
1232 /* Rename/erase next item. */
1233 if (fsmStage(fsm, FSM_FINI))
1234 /*@loopbreak@*/ break;
1240 rc = fsmStage(fsm, FSM_INIT);
1242 /* Exit on end-of-payload. */
1243 if (rc == CPIOERR_HDR_TRAILER) {
1245 /*@loopbreak@*/ break;
1248 /* Exit on error. */
1251 (void) fsmStage(fsm, FSM_UNDO);
1252 /*@loopbreak@*/ break;
1255 /* Copy file into archive. */
1256 rc = fsmStage(fsm, FSM_PROCESS);
1258 (void) fsmStage(fsm, FSM_UNDO);
1259 /*@loopbreak@*/ break;
1262 if (fsmStage(fsm, FSM_FINI))
1263 /*@loopbreak@*/ break;
1267 rc = fsmStage(fsm, FSM_TRAILER);
1270 { rpmTransactionSet ts = fsmGetTs(fsm);
1271 #define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
1272 fsm->commit = ((ts && (ts->transFlags & _tsmask) &&
1273 fsm->goal != FSM_PKGCOMMIT) ? 0 : 1);
1276 fsm->path = _free(fsm->path);
1277 fsm->opath = _free(fsm->opath);
1278 fsm->dnlx = _free(fsm->dnlx);
1280 fsm->ldn = _free(fsm->ldn);
1281 fsm->ldnalloc = fsm->ldnlen = 0;
1283 fsm->rdsize = fsm->wrsize = 0;
1284 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1285 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1286 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1287 fsm->rdsize = 8 * BUFSIZ;
1288 fsm->rdbuf = fsm->rdb = xmalloc(fsm->rdsize);
1289 fsm->wrsize = 8 * BUFSIZ;
1290 fsm->wrbuf = fsm->wrb = xmalloc(fsm->wrsize);
1293 fsm->mkdirsdone = 0;
1297 errno = 0; /* XXX get rid of EBADF */
1299 /* Detect and create directories not explicitly in package. */
1300 if (fsm->goal == FSM_PKGINSTALL) {
1301 rc = fsmStage(fsm, FSM_MKDIRS);
1302 if (!rc) fsm->mkdirsdone = 1;
1307 fsm->path = _free(fsm->path);
1309 fsm->diskchecked = fsm->exists = 0;
1311 fsm->suffix = (fsm->sufbuf[0] != '\0' ? fsm->sufbuf : NULL);
1312 fsm->action = FA_UNKNOWN;
1313 fsm->osuffix = NULL;
1314 fsm->nsuffix = NULL;
1316 if (fsm->goal == FSM_PKGINSTALL) {
1317 /* Read next header from payload, checking for end-of-payload. */
1318 rc = fsmStage(fsm, FSM_NEXT);
1322 /* Identify mapping index. */
1323 fsm->ix = ((fsm->goal == FSM_PKGINSTALL)
1324 ? mapFind(fsm->iter, fsm->path) : mapNextIterator(fsm->iter));
1326 /* On non-install, detect end-of-loop. */
1327 if (fsm->goal != FSM_PKGINSTALL && fsm->ix < 0) {
1328 rc = CPIOERR_HDR_TRAILER;
1332 /* On non-install, mode must be known so that dirs don't get suffix. */
1333 if (fsm->goal != FSM_PKGINSTALL) {
1334 TFI_t fi = fsmGetFi(fsm);
1335 st->st_mode = fi->fmodes[fsm->ix];
1338 /* Generate file path. */
1339 rc = fsmStage(fsm, FSM_MAP);
1342 /* Perform lstat/stat for disk file. */
1343 if (fsm->path != NULL) {
1344 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1345 ? FSM_LSTAT : FSM_STAT));
1346 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1350 } else if (rc == 0) {
1354 /* Skip %ghost files on build. */
1357 fsm->diskchecked = 1;
1360 /* On non-install, the disk file stat is what's remapped. */
1361 if (fsm->goal != FSM_PKGINSTALL)
1362 *st = *ost; /* structure assignment */
1364 /* Remap file perms, owner, and group. */
1365 rc = fsmMapAttrs(fsm);
1368 fsm->postpone = XFA_SKIPPING(fsm->action);
1369 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1371 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1372 fsm->postpone = saveHardLink(fsm);
1379 rc = fsmMapPath(fsm);
1382 rc = fsmMkdirs(fsm);
1386 rc = fsmRmdirs(fsm);
1389 if (fsm->postpone) {
1390 if (fsm->goal == FSM_PKGINSTALL)
1391 rc = fsmStage(fsm, FSM_EAT);
1395 if (fsm->goal == FSM_PKGBUILD) {
1396 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1397 struct hardLink * li, * prev;
1398 rc = writeLinkedFile(fsm);
1399 if (rc) break; /* W2DO? */
1401 for (li = fsm->links, prev = NULL; li; prev = li, li = li->next)
1403 /*@loopbreak@*/ break;
1406 fsm->links = fsm->li->next;
1408 prev->next = fsm->li->next;
1409 fsm->li->next = NULL;
1410 fsm->li = freeHardLink(fsm->li);
1412 rc = writeFile(fsm, 1);
1417 if (fsm->goal != FSM_PKGINSTALL)
1420 if (S_ISREG(st->st_mode)) {
1421 const char * path = fsm->path;
1423 fsm->path = fsmFsPath(fsm, st, NULL, NULL);
1424 rc = fsmStage(fsm, FSM_VERIFY);
1426 if (rc == 0 && fsm->osuffix) {
1427 const char * opath = fsm->opath;
1428 fsm->opath = fsm->path;
1429 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1430 rc = fsmStage(fsm, FSM_RENAME);
1432 rpmMessage(RPMMESS_WARNING,
1433 _("%s saved as %s\n"), fsm->opath, fsm->path);
1434 fsm->path = _free(fsm->path);
1438 /*@-dependenttrans@*/
1440 /*@=dependenttrans@*/
1441 if (rc != CPIOERR_LSTAT_FAILED) return rc;
1442 rc = expandRegular(fsm);
1443 } else if (S_ISDIR(st->st_mode)) {
1444 mode_t st_mode = st->st_mode;
1445 rc = fsmStage(fsm, FSM_VERIFY);
1446 if (rc == CPIOERR_LSTAT_FAILED) {
1447 st->st_mode &= ~07777; /* XXX abuse st->st_mode */
1448 st->st_mode |= 00700;
1449 rc = fsmStage(fsm, FSM_MKDIR);
1450 st->st_mode = st_mode; /* XXX restore st->st_mode */
1452 } else if (S_ISLNK(st->st_mode)) {
1453 const char * opath = fsm->opath;
1455 if ((st->st_size + 1) > fsm->rdsize) {
1456 rc = CPIOERR_HDR_SIZE;
1460 fsm->wrlen = st->st_size;
1461 rc = fsmStage(fsm, FSM_DREAD);
1462 if (!rc && fsm->rdnb != fsm->wrlen)
1463 rc = CPIOERR_READ_FAILED;
1466 fsm->wrbuf[st->st_size] = '\0';
1467 /* XXX symlink(fsm->opath, fsm->path) */
1468 /*@-dependenttrans@*/
1469 fsm->opath = fsm->wrbuf;
1470 /*@=dependenttrans@*/
1471 rc = fsmStage(fsm, FSM_VERIFY);
1472 if (rc == CPIOERR_LSTAT_FAILED)
1473 rc = fsmStage(fsm, FSM_SYMLINK);
1474 fsm->opath = opath; /* XXX restore fsm->path */
1475 } else if (S_ISFIFO(st->st_mode)) {
1476 mode_t st_mode = st->st_mode;
1477 /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
1478 rc = fsmStage(fsm, FSM_VERIFY);
1479 if (rc == CPIOERR_LSTAT_FAILED) {
1480 st->st_mode = 0000; /* XXX abuse st->st_mode */
1481 rc = fsmStage(fsm, FSM_MKFIFO);
1482 st->st_mode = st_mode; /* XXX restore st->st_mode */
1484 } else if (S_ISCHR(st->st_mode) ||
1485 S_ISBLK(st->st_mode) ||
1486 S_ISSOCK(st->st_mode))
1488 rc = fsmStage(fsm, FSM_VERIFY);
1489 if (rc == CPIOERR_LSTAT_FAILED)
1490 rc = fsmStage(fsm, FSM_MKNOD);
1492 rc = CPIOERR_UNKNOWN_FILETYPE;
1494 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1495 fsm->li->createdPath = fsm->li->linkIndex;
1496 rc = fsmMakeLinks(fsm);
1502 rc = fsmMakeLinks(fsm);
1504 case FSM_NOTIFY: /* XXX move from fsm to psm -> tsm */
1505 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1506 rpmTransactionSet ts = fsmGetTs(fsm);
1507 TFI_t fi = fsmGetFi(fsm);
1508 if (ts && ts->notify && fi)
1509 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS,
1510 fdGetCpioPos(fsm->cfd), fi->archiveSize,
1511 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
1517 if (fsm->goal == FSM_PKGINSTALL) {
1518 (void) fsmStage(fsm,
1519 (S_ISDIR(st->st_mode) ? FSM_RMDIR : FSM_UNLINK));
1521 #ifdef NOTYET /* XXX remove only dirs just created, not all. */
1523 (void) fsmStage(fsm, FSM_RMDIRS);
1527 if (fsm->failedFile && *fsm->failedFile == NULL)
1528 *fsm->failedFile = xstrdup(fsm->path);
1531 if (!fsm->postpone && fsm->commit) {
1532 if (fsm->goal == FSM_PKGINSTALL)
1533 rc = ((!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1534 ? fsmCommitLinks(fsm) : fsmStage(fsm, FSM_COMMIT));
1535 if (fsm->goal == FSM_PKGCOMMIT)
1536 rc = fsmStage(fsm, FSM_COMMIT);
1537 if (fsm->goal == FSM_PKGERASE)
1538 rc = fsmStage(fsm, FSM_COMMIT);
1540 fsm->path = _free(fsm->path);
1541 fsm->opath = _free(fsm->opath);
1542 memset(st, 0, sizeof(*st));
1543 memset(ost, 0, sizeof(*ost));
1546 /* Rename pre-existing modified or unmanaged file. */
1547 if (fsm->diskchecked && fsm->exists && fsm->osuffix) {
1548 const char * opath = fsm->opath;
1549 const char * path = fsm->path;
1550 /*@-nullstate@*/ /* FIX: fsm->opath null annotation? */
1551 fsm->opath = fsmFsPath(fsm, st, NULL, NULL);
1553 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
1554 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1556 rc = fsmStage(fsm, FSM_RENAME);
1558 rpmMessage(RPMMESS_WARNING, _("%s saved as %s\n"),
1559 fsm->opath, fsm->path);
1561 fsm->path = _free(fsm->path);
1563 fsm->opath = _free(fsm->opath);
1567 /* Remove erased files. */
1568 if (fsm->goal == FSM_PKGERASE) {
1569 if (fsm->action == FA_ERASE) {
1570 TFI_t fi = fsmGetFi(fsm);
1571 if (S_ISDIR(st->st_mode)) {
1572 rc = fsmStage(fsm, FSM_RMDIR);
1575 case ENOENT: /* XXX rmdir("/") linux 2.2.x kernel hack */
1577 /* XXX make sure that build side permits %missingok on directories. */
1578 if (fsm->fflags & RPMFILE_MISSINGOK)
1581 /* XXX common error message. */
1583 (strict_erasures ? RPMERR_RMDIR : RPMWARN_RMDIR),
1584 _("%s rmdir of %s failed: Directory not empty\n"),
1585 fiTypeString(fi), fsm->path);
1589 (strict_erasures ? RPMERR_RMDIR : RPMWARN_RMDIR),
1590 _("%s rmdir of %s failed: %s\n"),
1591 fiTypeString(fi), fsm->path, strerror(errno));
1595 rc = fsmStage(fsm, FSM_UNLINK);
1597 if (!(errno == ENOENT && (fsm->fflags & RPMFILE_MISSINGOK)))
1599 (strict_erasures ? RPMERR_UNLINK : RPMWARN_UNLINK),
1600 _("%s unlink of %s failed: %s\n"),
1601 fiTypeString(fi), fsm->path, strerror(errno));
1604 /* XXX Failure to remove is not (yet) cause for failure. */
1605 if (!strict_erasures) rc = 0;
1609 if (!S_ISSOCK(st->st_mode)) { /* XXX /dev/log et al are skipped */
1610 /* Rename temporary to final file name. */
1611 if (!S_ISDIR(st->st_mode) &&
1612 (fsm->subdir || fsm->suffix || fsm->nsuffix))
1614 fsm->opath = fsm->path;
1615 fsm->path = fsmFsPath(fsm, st, NULL, fsm->nsuffix);
1616 rc = fsmStage(fsm, FSM_RENAME);
1617 if (!rc && fsm->nsuffix) {
1618 const char * opath = fsmFsPath(fsm, st, NULL, NULL);
1619 rpmMessage(RPMMESS_WARNING, _("%s created as %s\n"),
1620 (opath ? opath : ""), fsm->path);
1621 opath = _free(opath);
1623 fsm->opath = _free(fsm->opath);
1625 if (S_ISLNK(st->st_mode)) {
1626 if (!rc && !getuid())
1627 rc = fsmStage(fsm, FSM_LCHOWN);
1629 if (!rc && !getuid())
1630 rc = fsmStage(fsm, FSM_CHOWN);
1632 rc = fsmStage(fsm, FSM_CHMOD);
1634 time_t st_mtime = st->st_mtime;
1635 TFI_t fi = fsmGetFi(fsm);
1637 st->st_mtime = fi->fmtimes[fsm->ix];
1638 rc = fsmStage(fsm, FSM_UTIME);
1639 st->st_mtime = st_mtime;
1644 /* Notify on success. */
1645 if (!rc) rc = fsmStage(fsm, FSM_NOTIFY);
1648 fsm->path = _free(fsm->path);
1650 /* Create any remaining links (if no error), and clean up. */
1651 while ((fsm->li = fsm->links) != NULL) {
1652 fsm->links = fsm->li->next;
1653 fsm->li->next = NULL;
1654 if (fsm->goal == FSM_PKGINSTALL && fsm->commit && fsm->li->linksLeft)
1656 for (i = 0 ; i < fsm->li->linksLeft; i++) {
1657 if (fsm->li->filex[i] < 0) continue;
1658 rc = CPIOERR_MISSING_HARDLINK;
1659 if (fsm->failedFile && *fsm->failedFile == NULL) {
1660 fsm->ix = fsm->li->filex[i];
1661 if (!fsmStage(fsm, FSM_MAP)) {
1662 *fsm->failedFile = fsm->path;
1666 /*@loopbreak@*/ break;
1669 if (fsm->goal == FSM_PKGBUILD) {
1670 rc = CPIOERR_MISSING_HARDLINK;
1672 fsm->li = freeHardLink(fsm->li);
1674 fsm->ldn = _free(fsm->ldn);
1675 fsm->ldnalloc = fsm->ldnlen = 0;
1676 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1677 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1680 if (fsm->diskchecked && !fsm->exists) {
1681 rc = CPIOERR_LSTAT_FAILED;
1684 if (S_ISREG(st->st_mode)) {
1685 char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
1686 (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
1688 * XXX HP-UX (and other os'es) don't permit unlink on busy
1691 fsm->opath = fsm->path;
1693 rc = fsmStage(fsm, FSM_RENAME);
1695 (void) fsmStage(fsm, FSM_UNLINK);
1697 rc = CPIOERR_UNLINK_FAILED;
1698 fsm->path = fsm->opath;
1700 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1701 /*@notreached@*/ break;
1702 } else if (S_ISDIR(st->st_mode)) {
1703 if (S_ISDIR(ost->st_mode)) return 0;
1704 if (S_ISLNK(ost->st_mode)) {
1705 rc = fsmStage(fsm, FSM_STAT);
1706 if (rc == CPIOERR_STAT_FAILED && errno == ENOENT) rc = 0;
1709 if (S_ISDIR(ost->st_mode)) return 0;
1711 } else if (S_ISLNK(st->st_mode)) {
1712 if (S_ISLNK(ost->st_mode)) {
1713 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1714 rc = fsmStage(fsm, FSM_READLINK);
1717 if (!strcmp(fsm->opath, fsm->rdbuf)) return 0;
1719 } else if (S_ISFIFO(st->st_mode)) {
1720 if (S_ISFIFO(ost->st_mode)) return 0;
1721 } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
1722 if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
1723 (ost->st_rdev == st->st_rdev)) return 0;
1724 } else if (S_ISSOCK(st->st_mode)) {
1725 if (S_ISSOCK(ost->st_mode)) return 0;
1727 /* XXX shouldn't do this with commit/undo. */
1729 if (fsm->stage == FSM_PROCESS) rc = fsmStage(fsm, FSM_UNLINK);
1730 if (rc == 0) rc = CPIOERR_LSTAT_FAILED;
1731 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1732 /*@notreached@*/ break;
1735 rc = Unlink(fsm->path);
1736 if (_fsm_debug && (stage & FSM_SYSCALL))
1737 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1738 fsm->path, (rc < 0 ? strerror(errno) : ""));
1739 if (rc < 0) rc = CPIOERR_UNLINK_FAILED;
1742 rc = Rename(fsm->opath, fsm->path);
1743 if (_fsm_debug && (stage & FSM_SYSCALL))
1744 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1745 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1746 if (rc < 0) rc = CPIOERR_RENAME_FAILED;
1749 rc = Mkdir(fsm->path, (st->st_mode & 07777));
1750 if (_fsm_debug && (stage & FSM_SYSCALL))
1751 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1752 fsm->path, (unsigned)(st->st_mode & 07777),
1753 (rc < 0 ? strerror(errno) : ""));
1754 if (rc < 0) rc = CPIOERR_MKDIR_FAILED;
1757 rc = Rmdir(fsm->path);
1758 if (_fsm_debug && (stage & FSM_SYSCALL))
1759 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1760 fsm->path, (rc < 0 ? strerror(errno) : ""));
1761 if (rc < 0) rc = CPIOERR_RMDIR_FAILED;
1764 rc = chown(fsm->path, st->st_uid, st->st_gid);
1765 if (_fsm_debug && (stage & FSM_SYSCALL))
1766 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1767 fsm->path, (int)st->st_uid, (int)st->st_gid,
1768 (rc < 0 ? strerror(errno) : ""));
1769 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1772 #if ! CHOWN_FOLLOWS_SYMLINK
1773 rc = lchown(fsm->path, st->st_uid, st->st_gid);
1774 if (_fsm_debug && (stage & FSM_SYSCALL))
1775 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1776 fsm->path, (int)st->st_uid, (int)st->st_gid,
1777 (rc < 0 ? strerror(errno) : ""));
1778 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1782 rc = chmod(fsm->path, (st->st_mode & 07777));
1783 if (_fsm_debug && (stage & FSM_SYSCALL))
1784 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1785 fsm->path, (unsigned)(st->st_mode & 07777),
1786 (rc < 0 ? strerror(errno) : ""));
1787 if (rc < 0) rc = CPIOERR_CHMOD_FAILED;
1790 { struct utimbuf stamp;
1791 stamp.actime = st->st_mtime;
1792 stamp.modtime = st->st_mtime;
1793 rc = utime(fsm->path, &stamp);
1794 if (_fsm_debug && (stage & FSM_SYSCALL))
1795 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0x%x) %s\n", cur,
1796 fsm->path, (unsigned)st->st_mtime,
1797 (rc < 0 ? strerror(errno) : ""));
1798 if (rc < 0) rc = CPIOERR_UTIME_FAILED;
1802 rc = symlink(fsm->opath, fsm->path);
1803 if (_fsm_debug && (stage & FSM_SYSCALL))
1804 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1805 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1806 if (rc < 0) rc = CPIOERR_SYMLINK_FAILED;
1809 rc = Link(fsm->opath, fsm->path);
1810 if (_fsm_debug && (stage & FSM_SYSCALL))
1811 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1812 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1813 if (rc < 0) rc = CPIOERR_LINK_FAILED;
1816 rc = mkfifo(fsm->path, (st->st_mode & 07777));
1817 if (_fsm_debug && (stage & FSM_SYSCALL))
1818 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1819 fsm->path, (unsigned)(st->st_mode & 07777),
1820 (rc < 0 ? strerror(errno) : ""));
1821 if (rc < 0) rc = CPIOERR_MKFIFO_FAILED;
1825 rc = mknod(fsm->path, (st->st_mode & ~07777), st->st_rdev);
1827 if (_fsm_debug && (stage & FSM_SYSCALL))
1828 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n", cur,
1829 fsm->path, (unsigned)(st->st_mode & ~07777),
1830 (unsigned)st->st_rdev,
1831 (rc < 0 ? strerror(errno) : ""));
1832 if (rc < 0) rc = CPIOERR_MKNOD_FAILED;
1835 rc = Lstat(fsm->path, ost);
1836 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1837 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1838 fsm->path, (rc < 0 ? strerror(errno) : ""));
1839 if (rc < 0) rc = CPIOERR_LSTAT_FAILED;
1842 rc = Stat(fsm->path, ost);
1843 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1844 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1845 fsm->path, (rc < 0 ? strerror(errno) : ""));
1846 if (rc < 0) rc = CPIOERR_STAT_FAILED;
1849 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1850 rc = Readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1);
1851 if (_fsm_debug && (stage & FSM_SYSCALL))
1852 rpmMessage(RPMMESS_DEBUG, " %8s (%s, rdbuf, %d) %s\n", cur,
1853 fsm->path, (int)fsm->rdlen, (rc < 0 ? strerror(errno) : ""));
1854 if (rc < 0) rc = CPIOERR_READLINK_FAILED;
1857 fsm->rdbuf[fsm->rdnb] = '\0';
1865 rc = fsmStage(fsm, FSM_HREAD);
1867 if (!strcmp(fsm->path, CPIO_TRAILER)) { /* Detect end-of-payload. */
1868 fsm->path = _free(fsm->path);
1869 rc = CPIOERR_HDR_TRAILER;
1872 rc = fsmStage(fsm, FSM_POS);
1875 for (left = st->st_size; left > 0; left -= fsm->rdnb) {
1876 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
1877 rc = fsmStage(fsm, FSM_DREAD);
1879 /*@loopbreak@*/ break;
1883 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
1886 (void) fsmStage(fsm, FSM_DREAD);
1890 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
1892 memset(fsm->rdbuf, 0, left);
1893 /* XXX DWRITE uses rdnb for I/O length. */
1895 (void) fsmStage(fsm, FSM_DWRITE);
1899 rc = cpioTrailerWrite(fsm);
1902 rc = fsmStage(fsm, FSM_POS);
1904 rc = cpioHeaderRead(fsm, st); /* Read next payload header. */
1907 rc = cpioHeaderWrite(fsm, st); /* Write next payload header. */
1910 fsm->rdnb = Fread(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->wrlen, fsm->cfd);
1911 if (_fsm_debug && (stage & FSM_SYSCALL))
1912 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\trdnb %d\n",
1913 cur, (fsm->wrbuf == fsm->wrb ? "wrbuf" : "mmap"),
1914 (int)fsm->wrlen, (int)fsm->rdnb);
1915 if (fsm->rdnb != fsm->wrlen) fprintf(stderr, "*** short read, had %d, got %d\n", (int)fsm->rdnb, (int)fsm->wrlen);
1916 if (fsm->rdnb != fsm->wrlen || Ferror(fsm->cfd))
1917 rc = CPIOERR_READ_FAILED;
1919 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->rdnb);
1922 fsm->wrnb = Fwrite(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdnb, fsm->cfd);
1923 if (_fsm_debug && (stage & FSM_SYSCALL))
1924 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\twrnb %d\n",
1925 cur, (fsm->rdbuf == fsm->rdb ? "rdbuf" : "mmap"),
1926 (int)fsm->rdnb, (int)fsm->wrnb);
1927 if (fsm->rdnb != fsm->wrnb) fprintf(stderr, "*** short write, had %d, got %d\n", (int)fsm->rdnb, (int)fsm->wrnb);
1928 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->cfd))
1929 rc = CPIOERR_WRITE_FAILED;
1931 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->wrnb);
1935 fsm->rfd = Fopen(fsm->path, "r.ufdio");
1936 if (fsm->rfd == NULL || Ferror(fsm->rfd)) {
1937 if (fsm->rfd) (void) fsmStage(fsm, FSM_RCLOSE);
1939 rc = CPIOERR_OPEN_FAILED;
1942 if (_fsm_debug && (stage & FSM_SYSCALL))
1943 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"r\") rfd %p rdbuf %p\n", cur,
1944 fsm->path, fsm->rfd, fsm->rdbuf);
1947 fsm->rdnb = Fread(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdlen, fsm->rfd);
1948 if (_fsm_debug && (stage & FSM_SYSCALL))
1949 rpmMessage(RPMMESS_DEBUG, " %8s (rdbuf, %d, rfd)\trdnb %d\n",
1950 cur, (int)fsm->rdlen, (int)fsm->rdnb);
1951 if (fsm->rdnb != fsm->rdlen) fprintf(stderr, "*** short read, had %d, got %d\n", (int)fsm->rdnb, (int)fsm->rdlen);
1952 if (fsm->rdnb != fsm->rdlen || Ferror(fsm->rfd))
1953 rc = CPIOERR_READ_FAILED;
1957 if (_fsm_debug && (stage & FSM_SYSCALL))
1958 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->rfd);
1959 (void) Fclose(fsm->rfd);
1965 fsm->wfd = Fopen(fsm->path, "w.ufdio");
1966 if (fsm->wfd == NULL || Ferror(fsm->wfd)) {
1967 if (fsm->wfd) (void) fsmStage(fsm, FSM_WCLOSE);
1969 rc = CPIOERR_OPEN_FAILED;
1971 if (_fsm_debug && (stage & FSM_SYSCALL))
1972 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"w\") wfd %p wrbuf %p\n", cur,
1973 fsm->path, fsm->wfd, fsm->wrbuf);
1976 fsm->wrnb = Fwrite(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->rdnb, fsm->wfd);
1977 if (_fsm_debug && (stage & FSM_SYSCALL))
1978 rpmMessage(RPMMESS_DEBUG, " %8s (wrbuf, %d, wfd)\twrnb %d\n",
1979 cur, (int)fsm->rdnb, (int)fsm->wrnb);
1980 if (fsm->rdnb != fsm->wrnb) fprintf(stderr, "*** short write: had %d, got %d\n", (int)fsm->rdnb, (int)fsm->wrnb);
1981 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->wfd))
1982 rc = CPIOERR_WRITE_FAILED;
1986 if (_fsm_debug && (stage & FSM_SYSCALL))
1987 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->wfd);
1988 (void) Fclose(fsm->wfd);
1998 if (!(stage & FSM_INTERNAL)) {
1999 fsm->rc = (rc == CPIOERR_HDR_TRAILER ? 0 : rc);
2005 /*@obserever@*/ const char *const fileActionString(fileAction a)
2008 case FA_UNKNOWN: return "unknown";
2009 case FA_CREATE: return "create";
2010 case FA_COPYOUT: return "copyout";
2011 case FA_COPYIN: return "copyin";
2012 case FA_BACKUP: return "backup";
2013 case FA_SAVE: return "save";
2014 case FA_SKIP: return "skip";
2015 case FA_ALTNAME: return "altname";
2016 case FA_ERASE: return "erase";
2017 case FA_SKIPNSTATE: return "skipnstate";
2018 case FA_SKIPNETSHARED: return "skipnetshared";
2019 case FA_SKIPMULTILIB: return "skipmultilib";
2020 default: return "???";
2025 /*@observer@*/ const char *const fileStageString(fileStage a) {
2027 case FSM_UNKNOWN: return "unknown";
2029 case FSM_PKGINSTALL:return "pkginstall";
2030 case FSM_PKGERASE: return "pkgerase";
2031 case FSM_PKGBUILD: return "pkgbuild";
2032 case FSM_PKGCOMMIT: return "pkgcommit";
2033 case FSM_PKGUNDO: return "pkgundo";
2035 case FSM_CREATE: return "create";
2036 case FSM_INIT: return "init";
2037 case FSM_MAP: return "map";
2038 case FSM_MKDIRS: return "mkdirs";
2039 case FSM_RMDIRS: return "rmdirs";
2040 case FSM_PRE: return "pre";
2041 case FSM_PROCESS: return "process";
2042 case FSM_POST: return "post";
2043 case FSM_MKLINKS: return "mklinks";
2044 case FSM_NOTIFY: return "notify";
2045 case FSM_UNDO: return "undo";
2046 case FSM_FINI: return "fini";
2047 case FSM_COMMIT: return "commit";
2048 case FSM_DESTROY: return "destroy";
2049 case FSM_VERIFY: return "verify";
2051 case FSM_UNLINK: return "Unlink";
2052 case FSM_RENAME: return "Rename";
2053 case FSM_MKDIR: return "Mkdir";
2054 case FSM_RMDIR: return "rmdir";
2055 case FSM_CHOWN: return "chown";
2056 case FSM_LCHOWN: return "lchown";
2057 case FSM_CHMOD: return "chmod";
2058 case FSM_UTIME: return "utime";
2059 case FSM_SYMLINK: return "symlink";
2060 case FSM_LINK: return "Link";
2061 case FSM_MKFIFO: return "mkfifo";
2062 case FSM_MKNOD: return "mknod";
2063 case FSM_LSTAT: return "Lstat";
2064 case FSM_STAT: return "Stat";
2065 case FSM_READLINK: return "Readlink";
2066 case FSM_CHROOT: return "chroot";
2068 case FSM_NEXT: return "next";
2069 case FSM_EAT: return "eat";
2070 case FSM_POS: return "pos";
2071 case FSM_PAD: return "pad";
2072 case FSM_TRAILER: return "trailer";
2073 case FSM_HREAD: return "hread";
2074 case FSM_HWRITE: return "hwrite";
2075 case FSM_DREAD: return "Fread";
2076 case FSM_DWRITE: return "Fwrite";
2078 case FSM_ROPEN: return "Fopen";
2079 case FSM_READ: return "Fread";
2080 case FSM_RCLOSE: return "Fclose";
2081 case FSM_WOPEN: return "Fopen";
2082 case FSM_WRITE: return "Fwrite";
2083 case FSM_WCLOSE: return "Fclose";
2085 default: return "???";