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 */
62 const char * s = NULL;
67 nb = strlen(fsm->dirName) +
68 (st && !S_ISDIR(st->st_mode) ? (subdir ? strlen(subdir) : 0) : 0) +
69 (st && !S_ISDIR(st->st_mode) ? (suffix ? strlen(suffix) : 0) : 0) +
70 strlen(fsm->baseName) + 1;
72 t = stpcpy(t, fsm->dirName);
73 if (st && !S_ISDIR(st->st_mode))
74 if (subdir) t = stpcpy(t, subdir);
75 t = stpcpy(t, fsm->baseName);
76 if (st && !S_ISDIR(st->st_mode))
77 if (suffix) t = stpcpy(t, suffix);
83 * Destroy file info iterator.
84 * @param p file info iterator
87 /*@-mustmod@*/ /* LCL: *p is modified */
88 static /*@null@*/ void * mapFreeIterator(/*@only@*//*@null@*/const void * p)
91 return _free((void *)p);
96 * Create file info iterator.
97 * @param a transaction set
98 * @param b transaction element file info
99 * @return file info iterator
102 mapInitIterator(/*@kept@*/ const void * a, /*@kept@*/ const void * b)
105 rpmTransactionSet ts = (void *)a;
106 TFI_t fi = (void *)b;
109 iter = xcalloc(1, sizeof(*iter));
114 iter->reverse = (fi->type == TR_REMOVED && fi->action != FA_COPYOUT);
115 iter->i = (iter->reverse ? (fi->fc - 1) : 0);
116 iter->isave = iter->i;
121 * Return next index into file info.
122 * @param a file info iterator
123 * @return next index, -1 on termination
125 /*@-mustmod@*/ /* LCL: *a is modified */
126 static int mapNextIterator(void * a)
130 const TFI_t fi = iter->fi;
134 if (iter->i >= 0) i = iter->i--;
136 if (iter->i < fi->fc) i = iter->i++;
145 static int cpioStrCmp(const void * a, const void * b)
148 const char * afn = *(const char **)a;
149 const char * bfn = *(const char **)b;
151 /* Match rpm-4.0 payloads with ./ prefixes. */
152 if (afn[0] == '.' && afn[1] == '/') afn += 2;
153 if (bfn[0] == '.' && bfn[1] == '/') bfn += 2;
155 /* If either path is absolute, make it relative. */
156 if (afn[0] == '/') afn += 1;
157 if (bfn[0] == '/') bfn += 1;
159 return strcmp(afn, bfn);
163 * Locate archive path in file info.
164 * @param a file info iterator
165 * @param fsmPath archive path
166 * @return index into file info, -1 if archive path was not found
168 static int mapFind(void * a, const char * fsmPath)
172 const TFI_t fi = iter->fi;
175 if (fi && fi->fc > 0 && fi->apath && fsmPath && *fsmPath) {
176 const char ** p = NULL;
178 if (fi->apath != NULL)
179 p = bsearch(&fsmPath, fi->apath, fi->fc, sizeof(fsmPath),
182 iter->i = p - fi->apath;
183 ix = mapNextIterator(iter);
190 * Directory name iterator.
192 typedef struct dnli_s {
193 /*@dependent@*/ TFI_t fi;
194 /*@only@*/ /*@null@*/ char * active;
201 * Destroy directory name iterator.
202 * @param a directory name iterator
203 * @retval NULL always
205 static /*@null@*/ void * dnlFreeIterator(/*@only@*//*@null@*/ const void * a)
209 DNLI_t dnli = (void *)a;
210 if (dnli->active) free(dnli->active);
217 static inline int dnlCount(const DNLI_t dnli)
220 return (dnli ? dnli->fi->dc : 0);
225 static inline int dnlIndex(const DNLI_t dnli)
228 return (dnli ? dnli->isave : -1);
232 * Create directory name iterator.
233 * @param fsm file state machine data
234 * @param reverse traverse directory names in reverse order?
235 * @return directory name iterator
237 static /*@only@*/ void * dnlInitIterator(/*@special@*/ const FSM_t fsm,
239 /*@uses fsm->iter @*/
242 TFI_t fi = fsmGetFi(fsm);
248 dnli = xcalloc(1, sizeof(*dnli));
250 dnli->reverse = reverse;
251 dnli->i = (reverse ? fi->dc : 0);
254 dnli->active = xcalloc(fi->dc, sizeof(*dnli->active));
256 /* Identify parent directories not skipped. */
257 for (i = 0; i < fi->fc; i++)
258 if (!XFA_SKIPPING(fi->actions[i])) dnli->active[fi->dil[i]] = 1;
260 /* Exclude parent directories that are explicitly included. */
261 for (i = 0; i < fi->fc; i++) {
262 int dil, dnlen, bnlen;
264 if (!S_ISDIR(fi->fmodes[i]))
268 dnlen = strlen(fi->dnl[dil]);
269 bnlen = strlen(fi->bnl[i]);
271 for (j = 0; j < fi->dc; j++) {
275 if (!dnli->active[j] || j == dil)
276 /*@innercontinue@*/ continue;
279 if (jlen != (dnlen+bnlen+1))
280 /*@innercontinue@*/ continue;
281 if (strncmp(dnl, fi->dnl[dil], dnlen))
282 /*@innercontinue@*/ continue;
283 if (strncmp(dnl+dnlen, fi->bnl[i], bnlen))
284 /*@innercontinue@*/ continue;
285 if (dnl[dnlen+bnlen] != '/' || dnl[dnlen+bnlen+1] != '\0')
286 /*@innercontinue@*/ continue;
287 /* This directory is included in the package. */
289 /*@innerbreak@*/ break;
293 /* Print only once per package. */
296 for (i = 0; i < fi->dc; i++) {
297 if (!dnli->active[i]) continue;
300 rpmMessage(RPMMESS_DEBUG,
301 _("========= Directories not explictly included in package:\n"));
303 rpmMessage(RPMMESS_DEBUG, _("%9d %s\n"), i, fi->dnl[i]);
306 rpmMessage(RPMMESS_DEBUG, "=========\n");
313 * Return next directory name (from file info).
314 * @param dnli directory name iterator
315 * @return next directory name
317 static /*@observer@*/ const char * dnlNextIterator(/*@null@*/ DNLI_t dnli)
320 const char * dn = NULL;
328 i = (!dnli->reverse ? dnli->i++ : --dnli->i);
329 } while (i >= 0 && i < fi->dc && !dnli->active[i]);
331 if (i >= 0 && i < fi->dc)
341 * Save hard link in chain.
342 * @param fsm file state machine data
343 * @return Is chain only partially filled?
345 static int saveHardLink(/*@special@*/ /*@partial@*/ FSM_t fsm)
346 /*@uses fsm->links, fsm->ix, fsm->sb, fsm->goal, fsm->nsuffix @*/
347 /*@defines fsm->li @*/
348 /*@releases fsm->path @*/
351 struct stat * st = &fsm->sb;
356 /* Find hard link set. */
357 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
358 if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
362 /* New hard link encountered, add new link to set. */
363 if (fsm->li == NULL) {
364 fsm->li = xcalloc(1, sizeof(*fsm->li));
365 fsm->li->next = NULL;
366 fsm->li->sb = *st; /* structure assignment */
367 fsm->li->nlink = st->st_nlink;
368 fsm->li->linkIndex = fsm->ix;
369 fsm->li->createdPath = -1;
371 fsm->li->filex = xcalloc(st->st_nlink, sizeof(fsm->li->filex[0]));
372 memset(fsm->li->filex, -1, (st->st_nlink * sizeof(fsm->li->filex[0])));
373 fsm->li->nsuffix = xcalloc(st->st_nlink, sizeof(*fsm->li->nsuffix));
375 if (fsm->goal == FSM_PKGBUILD)
376 fsm->li->linksLeft = st->st_nlink;
377 if (fsm->goal == FSM_PKGINSTALL)
378 fsm->li->linksLeft = 0;
381 fsm->li->next = fsm->links;
383 fsm->links = fsm->li;
386 if (fsm->goal == FSM_PKGBUILD) --fsm->li->linksLeft;
387 fsm->li->filex[fsm->li->linksLeft] = fsm->ix;
388 /*@-observertrans -dependenttrans@*/
389 fsm->li->nsuffix[fsm->li->linksLeft] = fsm->nsuffix;
390 /*@=observertrans =dependenttrans@*/
391 if (fsm->goal == FSM_PKGINSTALL) fsm->li->linksLeft++;
393 if (fsm->goal == FSM_PKGBUILD)
394 return (fsm->li->linksLeft > 0);
396 if (fsm->goal != FSM_PKGINSTALL)
399 if (!(st->st_size || fsm->li->linksLeft == st->st_nlink))
402 /* Here come the bits, time to choose a non-skipped file name. */
403 { TFI_t fi = fsmGetFi(fsm);
405 for (j = fsm->li->linksLeft - 1; j >= 0; j--) {
406 ix = fsm->li->filex[j];
407 if (ix < 0 || XFA_SKIPPING(fi->actions[ix]))
413 /* Are all links skipped or not encountered yet? */
415 return 1; /* XXX W2DO? */
417 /* Save the non-skipped file name and map index. */
418 fsm->li->linkIndex = j;
419 fsm->path = _free(fsm->path);
421 rc = fsmStage(fsm, FSM_MAP);
422 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
428 * Destroy set of hard links.
429 * @param li set of hard links
431 static /*@null@*/ void * freeHardLink(/*@only@*/ /*@null@*/ struct hardLink * li)
435 li->nsuffix = _free(li->nsuffix); /* XXX elements are shared */
436 li->filex = _free(li->filex);
443 FSM_t fsm = xcalloc(1, sizeof(*fsm));
447 FSM_t freeFSM(FSM_t fsm)
450 fsm->path = _free(fsm->path);
451 while ((fsm->li = fsm->links) != NULL) {
452 fsm->links = fsm->li->next;
453 fsm->li->next = NULL;
454 fsm->li = freeHardLink(fsm->li);
456 fsm->dnlx = _free(fsm->dnlx);
457 fsm->ldn = _free(fsm->ldn);
458 fsm->iter = mapFreeIterator(fsm->iter);
463 int fsmSetup(FSM_t fsm, fileStage goal,
464 const rpmTransactionSet ts, const TFI_t fi, FD_t cfd,
465 unsigned int * archiveSize, const char ** failedFile)
472 fsm->cfd = fdLink(cfd, "persist (fsm)");
473 pos = fdGetCpioPos(fsm->cfd);
474 fdSetCpioPos(fsm->cfd, 0);
476 fsm->iter = mapInitIterator(ts, fi);
478 if (fsm->goal == FSM_PKGINSTALL) {
479 if (ts && ts->notify) {
480 (void)ts->notify(fi->h, RPMCALLBACK_INST_START, 0, fi->archiveSize,
481 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
486 fsm->archiveSize = archiveSize;
487 if (fsm->archiveSize)
488 *fsm->archiveSize = 0;
489 fsm->failedFile = failedFile;
491 *fsm->failedFile = NULL;
494 memset(fsm->sufbuf, 0, sizeof(fsm->sufbuf));
495 if (fsm->goal == FSM_PKGINSTALL) {
496 if (ts && ts->id > 0)
497 sprintf(fsm->sufbuf, ";%08x", (unsigned)ts->id);
501 rc = fsmStage(fsm, FSM_CREATE);
502 if (rc && !ec) ec = rc;
504 rc = fsmStage(fsm, fsm->goal);
505 if (rc && !ec) ec = rc;
507 if (fsm->archiveSize && ec == 0)
508 *fsm->archiveSize = (fdGetCpioPos(fsm->cfd) - pos);
513 int fsmTeardown(FSM_t fsm) {
517 rc = fsmStage(fsm, FSM_DESTROY);
519 fsm->iter = mapFreeIterator(fsm->iter);
521 fsm->cfd = fdFree(fsm->cfd, "persist (fsm)");
524 fsm->failedFile = NULL;
525 /*@-nullstate@*/ /* FIX: fsm->iter null annotation? */
530 int fsmMapPath(FSM_t fsm)
532 TFI_t fi = fsmGetFi(fsm); /* XXX const except for fstates */
539 fsm->action = FA_UNKNOWN;
543 if (fi && i >= 0 && i < fi->fc) {
545 fsm->astriplen = fi->astriplen;
546 fsm->action = (fi->actions ? fi->actions[i] : fi->action);
547 fsm->fflags = (fi->fflags ? fi->fflags[i] : fi->flags);
548 fsm->mapFlags = (fi->fmapflags ? fi->fmapflags[i] : fi->mapflags);
550 /* src rpms have simple base name in payload. */
551 fsm->dirName = fi->dnl[fi->dil[i]];
552 fsm->baseName = fi->bnl[i];
554 switch (fsm->action) {
557 case FA_SKIPMULTILIB: /* XXX RPMFILE_STATE_MULTILIB? */
566 assert(fi->type == TR_ADDED);
570 if (fi->fstates && fi->type == TR_ADDED)
571 fi->fstates[i] = RPMFILE_STATE_NOTINSTALLED;
574 case FA_SKIPNETSHARED:
575 if (fi->fstates && fi->type == TR_ADDED)
576 fi->fstates[i] = RPMFILE_STATE_NETSHARED;
580 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
583 fsm->osuffix = SUFFIX_RPMORIG;
584 /*@innerbreak@*/ break;
586 fsm->osuffix = SUFFIX_RPMSAVE;
587 /*@innerbreak@*/ break;
592 assert(fi->type == TR_ADDED);
593 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
594 fsm->nsuffix = SUFFIX_RPMNEW;
598 assert(fi->type == TR_ADDED);
599 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
600 fsm->osuffix = SUFFIX_RPMSAVE;
603 assert(fi->type == TR_REMOVED);
605 * XXX TODO: %ghost probably shouldn't be removed, but that changes
606 * legacy rpm behavior.
613 if ((fsm->mapFlags & CPIO_MAP_PATH) || fsm->nsuffix) {
614 const struct stat * st = &fsm->sb;
615 fsm->path = _free(fsm->path);
616 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
617 fsm->path = fsmFsPath(fsm, st, fsm->subdir,
618 (fsm->suffix ? fsm->suffix : fsm->nsuffix));
625 int fsmMapAttrs(FSM_t fsm)
627 struct stat * st = &fsm->sb;
628 TFI_t fi = fsmGetFi(fsm);
631 if (fi && i >= 0 && i < fi->fc) {
633 (S_ISDIR(st->st_mode) ? fi->dperms : fi->fperms);
635 (fi->fmodes ? fi->fmodes[i] : perms);
637 (fi->fuids ? fi->fuids[i] : fi->uid); /* XXX chmod u-s */
639 (fi->fgids ? fi->fgids[i] : fi->gid); /* XXX chmod g-s */
641 (fi->frdevs ? fi->frdevs[i] : 0);
643 (fi->fmtimes ? fi->fmtimes[i] : 0);
645 if (fsm->mapFlags & CPIO_MAP_MODE)
646 st->st_mode = (st->st_mode & S_IFMT) | (finalMode & ~S_IFMT);
647 if (fsm->mapFlags & CPIO_MAP_TYPE) {
648 st->st_mode = (st->st_mode & ~S_IFMT) | (finalMode & S_IFMT);
649 if ((S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
650 && st->st_nlink == 0)
652 st->st_rdev = finalRdev;
653 st->st_mtime = finalMtime;
655 if (fsm->mapFlags & CPIO_MAP_UID)
656 st->st_uid = finalUid;
657 if (fsm->mapFlags & CPIO_MAP_GID)
658 st->st_gid = finalGid;
660 fsm->fmd5sum = (fi->fmd5s ? fi->fmd5s[i] : NULL);
667 * Create file from payload stream.
668 * @param fsm file state machine data
669 * @return 0 on success
671 static int expandRegular(/*@special@*/ FSM_t fsm)
673 /*@modifies fsm, fileSystem @*/
675 const char * fmd5sum;
676 const struct stat * st = &fsm->sb;
677 int left = st->st_size;
680 rc = fsmStage(fsm, FSM_WOPEN);
684 /* XXX md5sum's will break on repackaging that includes modified files. */
685 fmd5sum = fsm->fmd5sum;
687 if (st->st_size > 0 && fmd5sum)
688 fdInitMD5(fsm->wfd, 0);
692 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
693 rc = fsmStage(fsm, FSM_DREAD);
697 rc = fsmStage(fsm, FSM_WRITE);
703 /* don't call this with fileSize == fileComplete */
705 (void) fsmStage(fsm, FSM_NOTIFY);
708 if (st->st_size > 0 && fmd5sum) {
709 const char * md5sum = NULL;
711 (void) Fflush(fsm->wfd);
712 fdFiniMD5(fsm->wfd, (void **)&md5sum, NULL, 1);
714 if (md5sum == NULL) {
715 rc = CPIOERR_MD5SUM_MISMATCH;
717 if (strcmp(md5sum, fmd5sum))
718 rc = CPIOERR_MD5SUM_MISMATCH;
719 md5sum = _free(md5sum);
724 (void) fsmStage(fsm, FSM_WCLOSE);
729 * Write next item to payload stream.
730 * @param fsm file state machine data
731 * @param writeData should data be written?
732 * @return 0 on success
734 static int writeFile(/*@special@*/ FSM_t fsm, int writeData)
735 /*@uses fsm->path, fsm->opath, fsm->sb, fsm->osb, fsm->cfd @*/
736 /*@modifies fsm, fileSystem @*/
738 const char * path = fsm->path;
739 const char * opath = fsm->opath;
740 struct stat * st = &fsm->sb;
741 struct stat * ost = &fsm->osb;
742 size_t pos = fdGetCpioPos(fsm->cfd);
743 char * symbuf = NULL;
747 st->st_size = (writeData ? ost->st_size : 0);
749 if (S_ISDIR(st->st_mode)) {
751 } else if (S_ISLNK(st->st_mode)) {
753 * While linux puts the size of a symlink in the st_size field,
754 * I don't think that's a specified standard.
756 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
757 rc = fsmStage(fsm, FSM_READLINK);
759 st->st_size = fsm->rdnb;
760 symbuf = alloca_strdup(fsm->rdbuf); /* XXX save readlink return. */
763 if (fsm->mapFlags & CPIO_MAP_ABSOLUTE) {
764 int nb = strlen(fsm->dirName) + strlen(fsm->baseName) + sizeof(".");
765 char * t = alloca(nb);
768 if (fsm->mapFlags & CPIO_MAP_ADDDOT)
770 t = stpcpy( stpcpy(t, fsm->dirName), fsm->baseName);
771 } else if (fsm->mapFlags & CPIO_MAP_PATH) {
772 TFI_t fi = fsmGetFi(fsm);
774 (fi->apath ? fi->apath[fsm->ix] + fi->striplen : fi->bnl[fsm->ix]);
777 rc = fsmStage(fsm, FSM_HWRITE);
781 if (writeData && S_ISREG(st->st_mode)) {
784 void * mapped = (void *)-1;
788 rc = fsmStage(fsm, FSM_ROPEN);
791 /* XXX unbuffered mmap generates *lots* of fdio debugging */
794 mapped = mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, Fileno(fsm->rfd), 0);
795 if (mapped != (void *)-1) {
797 fsm->rdbuf = (char *) mapped;
798 fsm->rdlen = nmapped = st->st_size;
806 if (mapped != (void *)-1) {
811 fsm->rdlen = (left > fsm->rdsize ? fsm->rdsize : left),
812 rc = fsmStage(fsm, FSM_READ);
816 /* XXX DWRITE uses rdnb for I/O length. */
817 rc = fsmStage(fsm, FSM_DWRITE);
824 if (mapped != (void *)-1) {
825 /*@-noeffect@*/ (void) munmap(mapped, nmapped) /*@=noeffect@*/;
830 } else if (writeData && S_ISLNK(st->st_mode)) {
831 /* XXX DWRITE uses rdnb for I/O length. */
832 strcpy(fsm->rdbuf, symbuf); /* XXX restore readlink buffer. */
833 fsm->rdnb = strlen(symbuf);
834 rc = fsmStage(fsm, FSM_DWRITE);
838 rc = fsmStage(fsm, FSM_PAD);
841 { const rpmTransactionSet ts = fsmGetTs(fsm);
842 TFI_t fi = fsmGetFi(fsm);
843 if (ts && fi && ts->notify) {
844 size_t size = (fdGetCpioPos(fsm->cfd) - pos);
845 /*@-modunconnomods@*/
846 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS, size, size,
847 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
848 /*@=modunconnomods@*/
856 (void) fsmStage(fsm, FSM_RCLOSE);
857 /*@-dependenttrans@*/
860 /*@=dependenttrans@*/
865 * Write set of linked files to payload stream.
866 * @param fsm file state machine data
867 * @return 0 on success
869 static int writeLinkedFile(/*@special@*/ FSM_t fsm)
870 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->li, fsm->failedFile @*/
871 /*@modifies fsm, fileSystem @*/
873 const char * path = fsm->path;
874 const char * nsuffix = fsm->nsuffix;
875 int iterIndex = fsm->ix;
884 for (i = fsm->li->nlink - 1; i >= 0; i--) {
886 if (fsm->li->filex[i] < 0) continue;
888 fsm->ix = fsm->li->filex[i];
889 rc = fsmStage(fsm, FSM_MAP);
891 /* Write data after last link. */
892 rc = writeFile(fsm, (i == 0));
893 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
895 *fsm->failedFile = xstrdup(fsm->path);
898 fsm->path = _free(fsm->path);
899 fsm->li->filex[i] = -1;
903 fsm->nsuffix = nsuffix;
909 * Create pending hard links to existing file.
910 * @param fsm file state machine data
911 * @return 0 on success
913 static int fsmMakeLinks(/*@special@*/ FSM_t fsm)
914 /*@uses fsm->path, fsm->opath, fsm->nsuffix, fsm->ix, fsm->li @*/
915 /*@modifies fsm, fileSystem @*/
917 const char * path = fsm->path;
918 const char * opath = fsm->opath;
919 const char * nsuffix = fsm->nsuffix;
920 int iterIndex = fsm->ix;
930 fsm->ix = fsm->li->filex[fsm->li->createdPath];
931 rc = fsmStage(fsm, FSM_MAP);
932 fsm->opath = fsm->path;
934 for (i = 0; i < fsm->li->nlink; i++) {
935 if (fsm->li->filex[i] < 0) continue;
936 if (i == fsm->li->createdPath) continue;
938 fsm->ix = fsm->li->filex[i];
939 fsm->path = _free(fsm->path);
940 rc = fsmStage(fsm, FSM_MAP);
941 rc = fsmStage(fsm, FSM_VERIFY);
943 if (rc != CPIOERR_LSTAT_FAILED) break;
945 /* XXX link(fsm->opath, fsm->path) */
946 rc = fsmStage(fsm, FSM_LINK);
947 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
949 *fsm->failedFile = xstrdup(fsm->path);
952 fsm->li->linksLeft--;
954 fsm->path = _free(fsm->path);
955 fsm->opath = _free(fsm->opath);
958 fsm->nsuffix = nsuffix;
965 * Commit hard linked file set atomically.
966 * @param fsm file state machine data
967 * @return 0 on success
969 static int fsmCommitLinks(/*@special@*/ FSM_t fsm)
970 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->sb,
971 fsm->li, fsm->links @*/
972 /*@modifies fsm, fileSystem @*/
974 const char * path = fsm->path;
975 const char * nsuffix = fsm->nsuffix;
976 int iterIndex = fsm->ix;
977 struct stat * st = &fsm->sb;
985 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
986 if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
990 for (i = 0; i < fsm->li->nlink; i++) {
991 if (fsm->li->filex[i] < 0) continue;
992 fsm->ix = fsm->li->filex[i];
993 rc = fsmStage(fsm, FSM_MAP);
994 rc = fsmStage(fsm, FSM_COMMIT);
995 fsm->path = _free(fsm->path);
996 fsm->li->filex[i] = -1;
1000 fsm->nsuffix = nsuffix;
1006 * Remove (if created) directories not explicitly included in package.
1007 * @param fsm file state machine data
1008 * @return 0 on success
1010 static int fsmRmdirs(/*@special@*/ FSM_t fsm)
1011 /*@uses fsm->path, fsm->dnlx, fsm->ldn, fsm->rdbuf, fsm->iter @*/
1012 /*@modifies fsm, fileSystem @*/
1014 const char * path = fsm->path;
1015 void * dnli = dnlInitIterator(fsm, 1);
1016 char * dn = fsm->rdbuf;
1017 int dc = dnlCount(dnli);
1022 /*@-observertrans -dependenttrans@*/
1023 if (fsm->ldn != NULL && fsm->dnlx != NULL)
1024 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1025 int dnlen = strlen(fsm->path);
1028 dc = dnlIndex(dnli);
1029 if (fsm->dnlx[dc] < 1 || fsm->dnlx[dc] >= dnlen)
1032 /* Copy to avoid const on fsm->path. */
1033 te = stpcpy(dn, fsm->path) - 1;
1036 /* Remove generated directories. */
1037 /*@-usereleased@*/ /* LCL: te used after release? */
1041 rc = fsmStage(fsm, FSM_RMDIR);
1045 /*@innerbreak@*/ break;
1047 } while ((te - fsm->path) > fsm->dnlx[dc]);
1050 dnli = dnlFreeIterator(dnli);
1051 /*@=observertrans =dependenttrans@*/
1058 * Create (if necessary) directories not explicitly included in package.
1059 * @param fsm file state machine data
1060 * @return 0 on success
1062 static int fsmMkdirs(/*@special@*/ FSM_t fsm)
1063 /*@uses fsm->path, fsm->sb, fsm->osb, fsm->rdbuf, fsm->iter,
1064 fsm->ldn, fsm->ldnlen, fsm->ldnalloc @*/
1065 /*@defines fsm->dnlx, fsm->ldn @*/
1066 /*@modifies fsm, fileSystem @*/
1068 struct stat * st = &fsm->sb;
1069 struct stat * ost = &fsm->osb;
1070 const char * path = fsm->path;
1071 mode_t st_mode = st->st_mode;
1072 void * dnli = dnlInitIterator(fsm, 0);
1073 char * dn = fsm->rdbuf;
1074 int dc = dnlCount(dnli);
1081 fsm->dnlx = (dc ? xcalloc(dc, sizeof(*fsm->dnlx)) : NULL);
1082 /*@-observertrans -dependenttrans@*/
1083 if (fsm->dnlx != NULL)
1084 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1085 int dnlen = strlen(fsm->path);
1088 dc = dnlIndex(dnli);
1089 if (dc < 0) continue;
1090 fsm->dnlx[dc] = dnlen;
1094 /*@-compdef -nullpass@*/ /* FIX: fsm->ldn not defined ??? */
1095 if (dnlen <= fsm->ldnlen && !strcmp(fsm->path, fsm->ldn))
1097 /*@=compdef =nullpass@*/
1099 /* Copy to avoid const on fsm->path. */
1100 (void) stpcpy(dn, fsm->path);
1103 /* Assume '/' directory exists, "mkdir -p" for others if non-existent */
1104 for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
1106 /*@innercontinue@*/ continue;
1110 /* Already validated? */
1111 /*@-usedef -compdef -nullpass -nullderef@*/
1112 if (i < fsm->ldnlen &&
1113 (fsm->ldn[i] == '/' || fsm->ldn[i] == '\0') &&
1114 !strncmp(fsm->path, fsm->ldn, i))
1117 /* Move pre-existing path marker forward. */
1118 fsm->dnlx[dc] = (te - dn);
1119 /*@innercontinue@*/ continue;
1121 /*@=usedef =compdef =nullpass =nullderef@*/
1123 /* Validate next component of path. */
1124 rc = fsmStage(fsm, FSM_LSTAT);
1127 /* Directory already exists? */
1128 if (rc == 0 && S_ISDIR(ost->st_mode)) {
1129 /* Move pre-existing path marker forward. */
1130 fsm->dnlx[dc] = (te - dn);
1131 } else if (rc == CPIOERR_LSTAT_FAILED) {
1132 TFI_t fi = fsmGetFi(fsm);
1134 st->st_mode = S_IFDIR | (fi->dperms & 07777);
1135 rc = fsmStage(fsm, FSM_MKDIR);
1137 rpmMessage(RPMMESS_DEBUG,
1138 _("%s directory created with perms %04o.\n"),
1139 fsm->path, (unsigned)(st->st_mode & 07777));
1143 /*@innerbreak@*/ break;
1147 /* Save last validated path. */
1148 if (fsm->ldnalloc < (dnlen + 1)) {
1149 fsm->ldnalloc = dnlen + 100;
1150 fsm->ldn = xrealloc(fsm->ldn, fsm->ldnalloc);
1152 if (fsm->ldn != NULL) { /* XXX can't happen */
1153 strcpy(fsm->ldn, fsm->path);
1154 fsm->ldnlen = dnlen;
1157 dnli = dnlFreeIterator(dnli);
1158 /*@=observertrans =dependenttrans@*/
1161 st->st_mode = st_mode; /* XXX restore st->st_mode */
1167 * Check for file on disk.
1168 * @param fsm file state machine data
1169 * @return 0 on success
1171 static int fsmStat(FSM_t fsm)
1173 int saveerrno = errno;
1176 if (fsm->path != NULL) {
1177 int saveernno = errno;
1178 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1179 ? FSM_LSTAT : FSM_STAT));
1180 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1184 } else if (rc == 0) {
1188 /* Skip %ghost files on build. */
1196 int fsmStage(FSM_t fsm, fileStage stage)
1199 fileStage prevStage = fsm->stage;
1200 const char * const prev = fileStageString(prevStage);
1202 static int modulo = 4;
1203 const char * const cur = fileStageString(stage);
1204 struct stat * st = &fsm->sb;
1205 struct stat * ost = &fsm->osb;
1206 int saveerrno = errno;
1211 #define _fafilter(_a) \
1212 (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
1213 ? fileActionString(_a) : "")
1215 if (stage & FSM_DEAD) {
1217 } else if (stage & FSM_INTERNAL) {
1218 if (_fsm_debug && !(stage & FSM_SYSCALL))
1219 rpmMessage(RPMMESS_DEBUG, " %8s %06o%3d (%4d,%4d)%10d %s %s\n",
1221 (unsigned)st->st_mode, (int)st->st_nlink,
1222 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1223 (fsm->path ? fsm->path : ""),
1224 _fafilter(fsm->action));
1227 if (_fsm_debug || !(stage & FSM_VERBOSE))
1228 rpmMessage(RPMMESS_DEBUG, "%-8s %06o%3d (%4d,%4d)%10d %s %s\n",
1230 (unsigned)st->st_mode, (int)st->st_nlink,
1231 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1232 (fsm->path ? fsm->path + fsm->astriplen : ""),
1233 _fafilter(fsm->action));
1240 case FSM_PKGINSTALL:
1242 /* Clean fsm, free'ing memory. Read next archive header. */
1243 rc = fsmStage(fsm, FSM_INIT);
1245 /* Exit on end-of-payload. */
1246 if (rc == CPIOERR_HDR_TRAILER) {
1248 /*@loopbreak@*/ break;
1251 /* Exit on error. */
1254 (void) fsmStage(fsm, FSM_UNDO);
1255 /*@loopbreak@*/ break;
1258 /* Extract file from archive. */
1259 rc = fsmStage(fsm, FSM_PROCESS);
1261 (void) fsmStage(fsm, FSM_UNDO);
1262 /*@loopbreak@*/ break;
1265 /* Notify on success. */
1266 (void) fsmStage(fsm, FSM_NOTIFY);
1268 rc = fsmStage(fsm, FSM_FINI);
1270 /*@loopbreak@*/ break;
1277 /* Clean fsm, free'ing memory. */
1278 rc = fsmStage(fsm, FSM_INIT);
1280 /* Exit on end-of-payload. */
1281 if (rc == CPIOERR_HDR_TRAILER) {
1283 /*@loopbreak@*/ break;
1286 /* Rename/erase next item. */
1287 if (fsmStage(fsm, FSM_FINI))
1288 /*@loopbreak@*/ break;
1294 rc = fsmStage(fsm, FSM_INIT);
1296 /* Exit on end-of-payload. */
1297 if (rc == CPIOERR_HDR_TRAILER) {
1299 /*@loopbreak@*/ break;
1302 /* Exit on error. */
1305 (void) fsmStage(fsm, FSM_UNDO);
1306 /*@loopbreak@*/ break;
1309 /* Copy file into archive. */
1310 rc = fsmStage(fsm, FSM_PROCESS);
1312 (void) fsmStage(fsm, FSM_UNDO);
1313 /*@loopbreak@*/ break;
1316 if (fsmStage(fsm, FSM_FINI))
1317 /*@loopbreak@*/ break;
1320 /* Flush partial sets of hard linked files. */
1321 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) {
1323 while ((fsm->li = fsm->links) != NULL) {
1324 fsm->links = fsm->li->next;
1325 fsm->li->next = NULL;
1327 /* Re-calculate link count for archive header. */
1328 for (j = -1, nlink = 0, i = 0; i < fsm->li->nlink; i++) {
1329 if (fsm->li->filex[i] < 0)
1330 /*@innercontinue@*/ continue;
1334 /* XXX force the contents out as well. */
1336 fsm->li->filex[0] = fsm->li->filex[j];
1337 fsm->li->filex[j] = -1;
1339 fsm->li->sb.st_nlink = nlink;
1341 fsm->sb = fsm->li->sb; /* structure assignment */
1342 fsm->osb = fsm->sb; /* structure assignment */
1344 if (!rc) rc = writeLinkedFile(fsm);
1346 fsm->li = freeHardLink(fsm->li);
1351 rc = fsmStage(fsm, FSM_TRAILER);
1355 { rpmTransactionSet ts = fsmGetTs(fsm);
1356 #define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
1357 fsm->commit = ((ts && (ts->transFlags & _tsmask) &&
1358 fsm->goal != FSM_PKGCOMMIT) ? 0 : 1);
1361 fsm->path = _free(fsm->path);
1362 fsm->opath = _free(fsm->opath);
1363 fsm->dnlx = _free(fsm->dnlx);
1365 fsm->ldn = _free(fsm->ldn);
1366 fsm->ldnalloc = fsm->ldnlen = 0;
1368 fsm->rdsize = fsm->wrsize = 0;
1369 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1370 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1371 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1372 fsm->rdsize = 8 * BUFSIZ;
1373 fsm->rdbuf = fsm->rdb = xmalloc(fsm->rdsize);
1374 fsm->wrsize = 8 * BUFSIZ;
1375 fsm->wrbuf = fsm->wrb = xmalloc(fsm->wrsize);
1378 fsm->mkdirsdone = 0;
1382 errno = 0; /* XXX get rid of EBADF */
1384 /* Detect and create directories not explicitly in package. */
1385 if (fsm->goal == FSM_PKGINSTALL) {
1386 rc = fsmStage(fsm, FSM_MKDIRS);
1387 if (!rc) fsm->mkdirsdone = 1;
1392 fsm->path = _free(fsm->path);
1394 fsm->diskchecked = fsm->exists = 0;
1396 fsm->suffix = (fsm->sufbuf[0] != '\0' ? fsm->sufbuf : NULL);
1397 fsm->action = FA_UNKNOWN;
1398 fsm->osuffix = NULL;
1399 fsm->nsuffix = NULL;
1401 if (fsm->goal == FSM_PKGINSTALL) {
1402 /* Read next header from payload, checking for end-of-payload. */
1403 rc = fsmStage(fsm, FSM_NEXT);
1407 /* Identify mapping index. */
1408 fsm->ix = ((fsm->goal == FSM_PKGINSTALL)
1409 ? mapFind(fsm->iter, fsm->path) : mapNextIterator(fsm->iter));
1411 /* Detect end-of-loop and/or mapping error. */
1413 if (fsm->goal == FSM_PKGINSTALL) {
1415 rpmMessage(RPMMESS_WARNING,
1416 _("archive file %s was not found in header file list\n"),
1419 if (fsm->failedFile && *fsm->failedFile == NULL)
1420 *fsm->failedFile = xstrdup(fsm->path);
1421 rc = CPIOERR_UNMAPPED_FILE;
1423 rc = CPIOERR_HDR_TRAILER;
1428 /* On non-install, mode must be known so that dirs don't get suffix. */
1429 if (fsm->goal != FSM_PKGINSTALL) {
1430 TFI_t fi = fsmGetFi(fsm);
1431 st->st_mode = fi->fmodes[fsm->ix];
1434 /* Generate file path. */
1435 rc = fsmStage(fsm, FSM_MAP);
1438 /* Perform lstat/stat for disk file. */
1442 if (fsm->path != NULL) {
1443 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1444 ? FSM_LSTAT : FSM_STAT));
1445 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1449 } else if (rc == 0) {
1453 /* Skip %ghost files on build. */
1457 fsm->diskchecked = 1;
1460 /* On non-install, the disk file stat is what's remapped. */
1461 if (fsm->goal != FSM_PKGINSTALL)
1462 *st = *ost; /* structure assignment */
1464 /* Remap file perms, owner, and group. */
1465 rc = fsmMapAttrs(fsm);
1468 fsm->postpone = XFA_SKIPPING(fsm->action);
1469 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1470 /*@-evalorder@*/ /* FIX: saveHardLink can modify fsm */
1471 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1472 fsm->postpone = saveHardLink(fsm);
1479 rc = fsmMapPath(fsm);
1482 rc = fsmMkdirs(fsm);
1486 rc = fsmRmdirs(fsm);
1489 if (fsm->postpone) {
1490 if (fsm->goal == FSM_PKGINSTALL)
1491 rc = fsmStage(fsm, FSM_EAT);
1495 if (fsm->goal == FSM_PKGBUILD) {
1496 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1497 struct hardLink * li, * prev;
1499 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) break;
1500 rc = writeLinkedFile(fsm);
1501 if (rc) break; /* W2DO? */
1503 for (li = fsm->links, prev = NULL; li; prev = li, li = li->next)
1505 /*@loopbreak@*/ break;
1508 fsm->links = fsm->li->next;
1510 prev->next = fsm->li->next;
1511 fsm->li->next = NULL;
1512 fsm->li = freeHardLink(fsm->li);
1514 rc = writeFile(fsm, 1);
1519 if (fsm->goal != FSM_PKGINSTALL)
1522 if (S_ISREG(st->st_mode)) {
1523 const char * path = fsm->path;
1525 fsm->path = fsmFsPath(fsm, st, NULL, NULL);
1526 rc = fsmStage(fsm, FSM_VERIFY);
1528 if (rc == 0 && fsm->osuffix) {
1529 const char * opath = fsm->opath;
1530 fsm->opath = fsm->path;
1531 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1532 rc = fsmStage(fsm, FSM_RENAME);
1534 rpmMessage(RPMMESS_WARNING,
1535 _("%s saved as %s\n"), fsm->opath, fsm->path);
1536 fsm->path = _free(fsm->path);
1540 /*@-dependenttrans@*/
1542 /*@=dependenttrans@*/
1543 if (rc != CPIOERR_LSTAT_FAILED) return rc;
1544 rc = expandRegular(fsm);
1545 } else if (S_ISDIR(st->st_mode)) {
1546 mode_t st_mode = st->st_mode;
1547 rc = fsmStage(fsm, FSM_VERIFY);
1548 if (rc == CPIOERR_LSTAT_FAILED) {
1549 st->st_mode &= ~07777; /* XXX abuse st->st_mode */
1550 st->st_mode |= 00700;
1551 rc = fsmStage(fsm, FSM_MKDIR);
1552 st->st_mode = st_mode; /* XXX restore st->st_mode */
1554 } else if (S_ISLNK(st->st_mode)) {
1555 const char * opath = fsm->opath;
1557 if ((st->st_size + 1) > fsm->rdsize) {
1558 rc = CPIOERR_HDR_SIZE;
1562 fsm->wrlen = st->st_size;
1563 rc = fsmStage(fsm, FSM_DREAD);
1564 if (!rc && fsm->rdnb != fsm->wrlen)
1565 rc = CPIOERR_READ_FAILED;
1568 fsm->wrbuf[st->st_size] = '\0';
1569 /* XXX symlink(fsm->opath, fsm->path) */
1570 /*@-dependenttrans@*/
1571 fsm->opath = fsm->wrbuf;
1572 /*@=dependenttrans@*/
1573 rc = fsmStage(fsm, FSM_VERIFY);
1574 if (rc == CPIOERR_LSTAT_FAILED)
1575 rc = fsmStage(fsm, FSM_SYMLINK);
1576 fsm->opath = opath; /* XXX restore fsm->path */
1577 } else if (S_ISFIFO(st->st_mode)) {
1578 mode_t st_mode = st->st_mode;
1579 /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
1580 rc = fsmStage(fsm, FSM_VERIFY);
1581 if (rc == CPIOERR_LSTAT_FAILED) {
1582 st->st_mode = 0000; /* XXX abuse st->st_mode */
1583 rc = fsmStage(fsm, FSM_MKFIFO);
1584 st->st_mode = st_mode; /* XXX restore st->st_mode */
1586 } else if (S_ISCHR(st->st_mode) ||
1587 S_ISBLK(st->st_mode) ||
1588 /*@-unrecog@*/ S_ISSOCK(st->st_mode) /*@=unrecog@*/)
1590 rc = fsmStage(fsm, FSM_VERIFY);
1591 if (rc == CPIOERR_LSTAT_FAILED)
1592 rc = fsmStage(fsm, FSM_MKNOD);
1594 rc = CPIOERR_UNKNOWN_FILETYPE;
1596 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1597 fsm->li->createdPath = fsm->li->linkIndex;
1598 rc = fsmMakeLinks(fsm);
1604 rc = fsmMakeLinks(fsm);
1606 case FSM_NOTIFY: /* XXX move from fsm to psm -> tsm */
1607 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1608 rpmTransactionSet ts = fsmGetTs(fsm);
1609 TFI_t fi = fsmGetFi(fsm);
1610 if (ts && ts->notify && fi)
1611 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS,
1612 fdGetCpioPos(fsm->cfd), fi->archiveSize,
1613 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
1619 if (fsm->goal == FSM_PKGINSTALL) {
1620 (void) fsmStage(fsm,
1621 (S_ISDIR(st->st_mode) ? FSM_RMDIR : FSM_UNLINK));
1623 #ifdef NOTYET /* XXX remove only dirs just created, not all. */
1625 (void) fsmStage(fsm, FSM_RMDIRS);
1629 if (fsm->failedFile && *fsm->failedFile == NULL)
1630 *fsm->failedFile = xstrdup(fsm->path);
1633 if (!fsm->postpone && fsm->commit) {
1634 if (fsm->goal == FSM_PKGINSTALL)
1635 rc = ((!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1636 ? fsmCommitLinks(fsm) : fsmStage(fsm, FSM_COMMIT));
1637 if (fsm->goal == FSM_PKGCOMMIT)
1638 rc = fsmStage(fsm, FSM_COMMIT);
1639 if (fsm->goal == FSM_PKGERASE)
1640 rc = fsmStage(fsm, FSM_COMMIT);
1642 fsm->path = _free(fsm->path);
1643 fsm->opath = _free(fsm->opath);
1644 memset(st, 0, sizeof(*st));
1645 memset(ost, 0, sizeof(*ost));
1648 /* Rename pre-existing modified or unmanaged file. */
1649 if (fsm->diskchecked && fsm->exists && fsm->osuffix) {
1650 const char * opath = fsm->opath;
1651 const char * path = fsm->path;
1652 /*@-nullstate@*/ /* FIX: fsm->opath null annotation? */
1653 fsm->opath = fsmFsPath(fsm, st, NULL, NULL);
1655 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
1656 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1658 rc = fsmStage(fsm, FSM_RENAME);
1660 rpmMessage(RPMMESS_WARNING, _("%s saved as %s\n"),
1661 fsm->opath, fsm->path);
1663 fsm->path = _free(fsm->path);
1665 fsm->opath = _free(fsm->opath);
1669 /* Remove erased files. */
1670 if (fsm->goal == FSM_PKGERASE) {
1671 if (fsm->action == FA_ERASE) {
1672 TFI_t fi = fsmGetFi(fsm);
1673 if (S_ISDIR(st->st_mode)) {
1674 rc = fsmStage(fsm, FSM_RMDIR);
1677 case ENOENT: /* XXX rmdir("/") linux 2.2.x kernel hack */
1679 /* XXX make sure that build side permits %missingok on directories. */
1680 if (fsm->fflags & RPMFILE_MISSINGOK)
1681 /*@innerbreak@*/ break;
1683 /* XXX common error message. */
1685 (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
1686 _("%s rmdir of %s failed: Directory not empty\n"),
1687 fiTypeString(fi), fsm->path);
1688 /*@innerbreak@*/ break;
1691 (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
1692 _("%s rmdir of %s failed: %s\n"),
1693 fiTypeString(fi), fsm->path, strerror(errno));
1694 /*@innerbreak@*/ break;
1697 rc = fsmStage(fsm, FSM_UNLINK);
1699 if (!(errno == ENOENT && (fsm->fflags & RPMFILE_MISSINGOK)))
1701 (strict_erasures ? RPMERR_UNLINK : RPMDEBUG_UNLINK),
1702 _("%s unlink of %s failed: %s\n"),
1703 fiTypeString(fi), fsm->path, strerror(errno));
1706 /* XXX Failure to remove is not (yet) cause for failure. */
1707 if (!strict_erasures) rc = 0;
1711 if (!S_ISSOCK(st->st_mode)) { /* XXX /dev/log et al are skipped */
1712 /* Rename temporary to final file name. */
1713 if (!S_ISDIR(st->st_mode) &&
1714 (fsm->subdir || fsm->suffix || fsm->nsuffix))
1716 fsm->opath = fsm->path;
1717 fsm->path = fsmFsPath(fsm, st, NULL, fsm->nsuffix);
1718 rc = fsmStage(fsm, FSM_RENAME);
1719 if (!rc && fsm->nsuffix) {
1720 const char * opath = fsmFsPath(fsm, st, NULL, NULL);
1721 rpmMessage(RPMMESS_WARNING, _("%s created as %s\n"),
1722 (opath ? opath : ""), fsm->path);
1723 opath = _free(opath);
1725 fsm->opath = _free(fsm->opath);
1727 if (S_ISLNK(st->st_mode)) {
1728 if (!rc && !getuid())
1729 rc = fsmStage(fsm, FSM_LCHOWN);
1731 if (!rc && !getuid())
1732 rc = fsmStage(fsm, FSM_CHOWN);
1734 rc = fsmStage(fsm, FSM_CHMOD);
1736 time_t mtime = st->st_mtime;
1737 TFI_t fi = fsmGetFi(fsm);
1739 st->st_mtime = fi->fmtimes[fsm->ix];
1740 rc = fsmStage(fsm, FSM_UTIME);
1741 st->st_mtime = mtime;
1746 /* Notify on success. */
1747 if (!rc) rc = fsmStage(fsm, FSM_NOTIFY);
1748 else if (fsm->failedFile && *fsm->failedFile == NULL) {
1749 *fsm->failedFile = fsm->path;
1754 fsm->path = _free(fsm->path);
1756 /* Check for hard links missing from payload. */
1757 while ((fsm->li = fsm->links) != NULL) {
1758 fsm->links = fsm->li->next;
1759 fsm->li->next = NULL;
1760 if (fsm->goal == FSM_PKGINSTALL &&
1761 fsm->commit && fsm->li->linksLeft)
1763 for (i = 0 ; i < fsm->li->linksLeft; i++) {
1764 if (fsm->li->filex[i] < 0)
1765 /*@innercontinue@*/ continue;
1766 rc = CPIOERR_MISSING_HARDLINK;
1767 if (fsm->failedFile && *fsm->failedFile == NULL) {
1768 fsm->ix = fsm->li->filex[i];
1769 if (!fsmStage(fsm, FSM_MAP)) {
1770 *fsm->failedFile = fsm->path;
1774 /*@loopbreak@*/ break;
1777 if (fsm->goal == FSM_PKGBUILD &&
1778 (fsm->mapFlags & CPIO_ALL_HARDLINKS))
1780 rc = CPIOERR_MISSING_HARDLINK;
1782 fsm->li = freeHardLink(fsm->li);
1784 fsm->ldn = _free(fsm->ldn);
1785 fsm->ldnalloc = fsm->ldnlen = 0;
1786 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1787 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1790 if (fsm->diskchecked && !fsm->exists) {
1791 rc = CPIOERR_LSTAT_FAILED;
1794 if (S_ISREG(st->st_mode)) {
1795 char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
1796 (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
1798 * XXX HP-UX (and other os'es) don't permit unlink on busy
1801 fsm->opath = fsm->path;
1803 rc = fsmStage(fsm, FSM_RENAME);
1805 (void) fsmStage(fsm, FSM_UNLINK);
1807 rc = CPIOERR_UNLINK_FAILED;
1808 fsm->path = fsm->opath;
1810 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1811 /*@notreached@*/ break;
1812 } else if (S_ISDIR(st->st_mode)) {
1813 if (S_ISDIR(ost->st_mode)) return 0;
1814 if (S_ISLNK(ost->st_mode)) {
1815 rc = fsmStage(fsm, FSM_STAT);
1816 if (rc == CPIOERR_STAT_FAILED && errno == ENOENT) rc = 0;
1819 if (S_ISDIR(ost->st_mode)) return 0;
1821 } else if (S_ISLNK(st->st_mode)) {
1822 if (S_ISLNK(ost->st_mode)) {
1823 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1824 rc = fsmStage(fsm, FSM_READLINK);
1827 if (!strcmp(fsm->opath, fsm->rdbuf)) return 0;
1829 } else if (S_ISFIFO(st->st_mode)) {
1830 if (S_ISFIFO(ost->st_mode)) return 0;
1831 } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
1832 if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
1833 (ost->st_rdev == st->st_rdev)) return 0;
1834 } else if (S_ISSOCK(st->st_mode)) {
1835 if (S_ISSOCK(ost->st_mode)) return 0;
1837 /* XXX shouldn't do this with commit/undo. */
1839 if (fsm->stage == FSM_PROCESS) rc = fsmStage(fsm, FSM_UNLINK);
1840 if (rc == 0) rc = CPIOERR_LSTAT_FAILED;
1841 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1842 /*@notreached@*/ break;
1845 rc = Unlink(fsm->path);
1846 if (_fsm_debug && (stage & FSM_SYSCALL))
1847 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1848 fsm->path, (rc < 0 ? strerror(errno) : ""));
1849 if (rc < 0) rc = CPIOERR_UNLINK_FAILED;
1852 rc = Rename(fsm->opath, fsm->path);
1853 if (_fsm_debug && (stage & FSM_SYSCALL))
1854 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1855 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1856 if (rc < 0) rc = CPIOERR_RENAME_FAILED;
1859 rc = Mkdir(fsm->path, (st->st_mode & 07777));
1860 if (_fsm_debug && (stage & FSM_SYSCALL))
1861 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1862 fsm->path, (unsigned)(st->st_mode & 07777),
1863 (rc < 0 ? strerror(errno) : ""));
1864 if (rc < 0) rc = CPIOERR_MKDIR_FAILED;
1867 rc = Rmdir(fsm->path);
1868 if (_fsm_debug && (stage & FSM_SYSCALL))
1869 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1870 fsm->path, (rc < 0 ? strerror(errno) : ""));
1871 if (rc < 0) rc = CPIOERR_RMDIR_FAILED;
1874 rc = chown(fsm->path, st->st_uid, st->st_gid);
1875 if (_fsm_debug && (stage & FSM_SYSCALL))
1876 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1877 fsm->path, (int)st->st_uid, (int)st->st_gid,
1878 (rc < 0 ? strerror(errno) : ""));
1879 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1882 #if ! CHOWN_FOLLOWS_SYMLINK
1883 rc = lchown(fsm->path, st->st_uid, st->st_gid);
1884 if (_fsm_debug && (stage & FSM_SYSCALL))
1885 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1886 fsm->path, (int)st->st_uid, (int)st->st_gid,
1887 (rc < 0 ? strerror(errno) : ""));
1888 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1892 rc = chmod(fsm->path, (st->st_mode & 07777));
1893 if (_fsm_debug && (stage & FSM_SYSCALL))
1894 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1895 fsm->path, (unsigned)(st->st_mode & 07777),
1896 (rc < 0 ? strerror(errno) : ""));
1897 if (rc < 0) rc = CPIOERR_CHMOD_FAILED;
1900 { struct utimbuf stamp;
1901 stamp.actime = st->st_mtime;
1902 stamp.modtime = st->st_mtime;
1903 rc = utime(fsm->path, &stamp);
1904 if (_fsm_debug && (stage & FSM_SYSCALL))
1905 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0x%x) %s\n", cur,
1906 fsm->path, (unsigned)st->st_mtime,
1907 (rc < 0 ? strerror(errno) : ""));
1908 if (rc < 0) rc = CPIOERR_UTIME_FAILED;
1912 rc = symlink(fsm->opath, fsm->path);
1913 if (_fsm_debug && (stage & FSM_SYSCALL))
1914 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1915 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1916 if (rc < 0) rc = CPIOERR_SYMLINK_FAILED;
1919 rc = Link(fsm->opath, fsm->path);
1920 if (_fsm_debug && (stage & FSM_SYSCALL))
1921 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1922 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1923 if (rc < 0) rc = CPIOERR_LINK_FAILED;
1926 rc = mkfifo(fsm->path, (st->st_mode & 07777));
1927 if (_fsm_debug && (stage & FSM_SYSCALL))
1928 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1929 fsm->path, (unsigned)(st->st_mode & 07777),
1930 (rc < 0 ? strerror(errno) : ""));
1931 if (rc < 0) rc = CPIOERR_MKFIFO_FAILED;
1934 /*@-unrecog -portability @*/ /* FIX: check S_IFIFO or dev != 0 */
1935 rc = mknod(fsm->path, (st->st_mode & ~07777), st->st_rdev);
1936 /*@=unrecog =portability @*/
1937 if (_fsm_debug && (stage & FSM_SYSCALL))
1938 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n", cur,
1939 fsm->path, (unsigned)(st->st_mode & ~07777),
1940 (unsigned)st->st_rdev,
1941 (rc < 0 ? strerror(errno) : ""));
1942 if (rc < 0) rc = CPIOERR_MKNOD_FAILED;
1945 rc = Lstat(fsm->path, ost);
1946 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1947 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1948 fsm->path, (rc < 0 ? strerror(errno) : ""));
1949 if (rc < 0) rc = CPIOERR_LSTAT_FAILED;
1952 rc = Stat(fsm->path, ost);
1953 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1954 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1955 fsm->path, (rc < 0 ? strerror(errno) : ""));
1956 if (rc < 0) rc = CPIOERR_STAT_FAILED;
1959 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1960 rc = Readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1);
1961 if (_fsm_debug && (stage & FSM_SYSCALL))
1962 rpmMessage(RPMMESS_DEBUG, " %8s (%s, rdbuf, %d) %s\n", cur,
1963 fsm->path, (int)(fsm->rdsize -1), (rc < 0 ? strerror(errno) : ""));
1964 if (rc < 0) rc = CPIOERR_READLINK_FAILED;
1967 fsm->rdbuf[fsm->rdnb] = '\0';
1975 rc = fsmStage(fsm, FSM_HREAD);
1977 if (!strcmp(fsm->path, CPIO_TRAILER)) { /* Detect end-of-payload. */
1978 fsm->path = _free(fsm->path);
1979 rc = CPIOERR_HDR_TRAILER;
1982 rc = fsmStage(fsm, FSM_POS);
1985 for (left = st->st_size; left > 0; left -= fsm->rdnb) {
1986 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
1987 rc = fsmStage(fsm, FSM_DREAD);
1989 /*@loopbreak@*/ break;
1993 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
1996 (void) fsmStage(fsm, FSM_DREAD);
2000 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
2002 memset(fsm->rdbuf, 0, left);
2003 /* XXX DWRITE uses rdnb for I/O length. */
2005 (void) fsmStage(fsm, FSM_DWRITE);
2009 rc = cpioTrailerWrite(fsm);
2012 rc = fsmStage(fsm, FSM_POS);
2014 rc = cpioHeaderRead(fsm, st); /* Read next payload header. */
2017 rc = cpioHeaderWrite(fsm, st); /* Write next payload header. */
2020 fsm->rdnb = Fread(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->wrlen, fsm->cfd);
2021 if (_fsm_debug && (stage & FSM_SYSCALL))
2022 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\trdnb %d\n",
2023 cur, (fsm->wrbuf == fsm->wrb ? "wrbuf" : "mmap"),
2024 (int)fsm->wrlen, (int)fsm->rdnb);
2025 if (fsm->rdnb != fsm->wrlen || Ferror(fsm->cfd))
2026 rc = CPIOERR_READ_FAILED;
2028 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->rdnb);
2031 fsm->wrnb = Fwrite(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdnb, fsm->cfd);
2032 if (_fsm_debug && (stage & FSM_SYSCALL))
2033 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\twrnb %d\n",
2034 cur, (fsm->rdbuf == fsm->rdb ? "rdbuf" : "mmap"),
2035 (int)fsm->rdnb, (int)fsm->wrnb);
2036 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->cfd))
2037 rc = CPIOERR_WRITE_FAILED;
2039 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->wrnb);
2043 fsm->rfd = Fopen(fsm->path, "r.ufdio");
2044 if (fsm->rfd == NULL || Ferror(fsm->rfd)) {
2045 if (fsm->rfd) (void) fsmStage(fsm, FSM_RCLOSE);
2047 rc = CPIOERR_OPEN_FAILED;
2050 if (_fsm_debug && (stage & FSM_SYSCALL))
2051 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"r\") rfd %p rdbuf %p\n", cur,
2052 fsm->path, fsm->rfd, fsm->rdbuf);
2055 fsm->rdnb = Fread(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdlen, fsm->rfd);
2056 if (_fsm_debug && (stage & FSM_SYSCALL))
2057 rpmMessage(RPMMESS_DEBUG, " %8s (rdbuf, %d, rfd)\trdnb %d\n",
2058 cur, (int)fsm->rdlen, (int)fsm->rdnb);
2059 if (fsm->rdnb != fsm->rdlen || Ferror(fsm->rfd))
2060 rc = CPIOERR_READ_FAILED;
2064 if (_fsm_debug && (stage & FSM_SYSCALL))
2065 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->rfd);
2066 (void) Fclose(fsm->rfd);
2072 fsm->wfd = Fopen(fsm->path, "w.ufdio");
2073 if (fsm->wfd == NULL || Ferror(fsm->wfd)) {
2074 if (fsm->wfd) (void) fsmStage(fsm, FSM_WCLOSE);
2076 rc = CPIOERR_OPEN_FAILED;
2078 if (_fsm_debug && (stage & FSM_SYSCALL))
2079 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"w\") wfd %p wrbuf %p\n", cur,
2080 fsm->path, fsm->wfd, fsm->wrbuf);
2083 fsm->wrnb = Fwrite(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->rdnb, fsm->wfd);
2084 if (_fsm_debug && (stage & FSM_SYSCALL))
2085 rpmMessage(RPMMESS_DEBUG, " %8s (wrbuf, %d, wfd)\twrnb %d\n",
2086 cur, (int)fsm->rdnb, (int)fsm->wrnb);
2087 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->wfd))
2088 rc = CPIOERR_WRITE_FAILED;
2092 if (_fsm_debug && (stage & FSM_SYSCALL))
2093 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->wfd);
2094 (void) Fclose(fsm->wfd);
2104 if (!(stage & FSM_INTERNAL)) {
2105 fsm->rc = (rc == CPIOERR_HDR_TRAILER ? 0 : rc);
2111 /*@obserever@*/ const char *const fileActionString(fileAction a)
2114 case FA_UNKNOWN: return "unknown";
2115 case FA_CREATE: return "create";
2116 case FA_COPYOUT: return "copyout";
2117 case FA_COPYIN: return "copyin";
2118 case FA_BACKUP: return "backup";
2119 case FA_SAVE: return "save";
2120 case FA_SKIP: return "skip";
2121 case FA_ALTNAME: return "altname";
2122 case FA_ERASE: return "erase";
2123 case FA_SKIPNSTATE: return "skipnstate";
2124 case FA_SKIPNETSHARED: return "skipnetshared";
2125 case FA_SKIPMULTILIB: return "skipmultilib";
2126 default: return "???";
2131 /*@observer@*/ const char *const fileStageString(fileStage a) {
2133 case FSM_UNKNOWN: return "unknown";
2135 case FSM_PKGINSTALL:return "pkginstall";
2136 case FSM_PKGERASE: return "pkgerase";
2137 case FSM_PKGBUILD: return "pkgbuild";
2138 case FSM_PKGCOMMIT: return "pkgcommit";
2139 case FSM_PKGUNDO: return "pkgundo";
2141 case FSM_CREATE: return "create";
2142 case FSM_INIT: return "init";
2143 case FSM_MAP: return "map";
2144 case FSM_MKDIRS: return "mkdirs";
2145 case FSM_RMDIRS: return "rmdirs";
2146 case FSM_PRE: return "pre";
2147 case FSM_PROCESS: return "process";
2148 case FSM_POST: return "post";
2149 case FSM_MKLINKS: return "mklinks";
2150 case FSM_NOTIFY: return "notify";
2151 case FSM_UNDO: return "undo";
2152 case FSM_FINI: return "fini";
2153 case FSM_COMMIT: return "commit";
2154 case FSM_DESTROY: return "destroy";
2155 case FSM_VERIFY: return "verify";
2157 case FSM_UNLINK: return "Unlink";
2158 case FSM_RENAME: return "Rename";
2159 case FSM_MKDIR: return "Mkdir";
2160 case FSM_RMDIR: return "rmdir";
2161 case FSM_CHOWN: return "chown";
2162 case FSM_LCHOWN: return "lchown";
2163 case FSM_CHMOD: return "chmod";
2164 case FSM_UTIME: return "utime";
2165 case FSM_SYMLINK: return "symlink";
2166 case FSM_LINK: return "Link";
2167 case FSM_MKFIFO: return "mkfifo";
2168 case FSM_MKNOD: return "mknod";
2169 case FSM_LSTAT: return "Lstat";
2170 case FSM_STAT: return "Stat";
2171 case FSM_READLINK: return "Readlink";
2172 case FSM_CHROOT: return "chroot";
2174 case FSM_NEXT: return "next";
2175 case FSM_EAT: return "eat";
2176 case FSM_POS: return "pos";
2177 case FSM_PAD: return "pad";
2178 case FSM_TRAILER: return "trailer";
2179 case FSM_HREAD: return "hread";
2180 case FSM_HWRITE: return "hwrite";
2181 case FSM_DREAD: return "Fread";
2182 case FSM_DWRITE: return "Fwrite";
2184 case FSM_ROPEN: return "Fopen";
2185 case FSM_READ: return "Fread";
2186 case FSM_RCLOSE: return "Fclose";
2187 case FSM_WOPEN: return "Fopen";
2188 case FSM_WRITE: return "Fwrite";
2189 case FSM_WCLOSE: return "Fclose";
2191 default: return "???";