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 rpmTransactionSet fsmGetTs(const FSM_t fsm) {
23 const FSMI_t iter = fsm->iter;
24 return (iter ? iter->ts : NULL);
27 TFI_t fsmGetFi(const FSM_t fsm) {
28 const FSMI_t iter = fsm->iter;
29 return (iter ? iter->fi : NULL);
32 #define SUFFIX_RPMORIG ".rpmorig"
33 #define SUFFIX_RPMSAVE ".rpmsave"
34 #define SUFFIX_RPMNEW ".rpmnew"
37 * Build path to file from file info, ornamented with subdir and suffix.
38 * @param fsm file state machine data
39 * @param st file stat info
40 * @param subdir subdir to use (NULL disables)
41 * @param suffix suffix to use (NULL disables)
42 * @retval path to file
44 static /*@only@*//*@null@*/ const char * fsmFsPath(/*@null@*/ const FSM_t fsm,
45 /*@null@*/ const struct stat * st,
46 /*@null@*/ const char * subdir,
47 /*@null@*/ const char * suffix)
49 const char * s = NULL;
54 nb = strlen(fsm->dirName) +
55 (st && subdir && !S_ISDIR(st->st_mode) ? strlen(subdir) : 0) +
56 (st && suffix && !S_ISDIR(st->st_mode) ? strlen(suffix) : 0) +
57 strlen(fsm->baseName) + 1;
59 t = stpcpy(t, fsm->dirName);
60 if (st && subdir && !S_ISDIR(st->st_mode))
61 t = stpcpy(t, subdir);
62 t = stpcpy(t, fsm->baseName);
63 if (st && suffix && !S_ISDIR(st->st_mode))
64 t = stpcpy(t, suffix);
70 * Destroy file info iterator.
71 * @param this file info iterator
74 static /*@null@*/ void * mapFreeIterator(/*@only@*//*@null@*/const void * this)
76 return _free((void *)this);
80 * Create file info iterator.
81 * @param this transaction set
82 * @param that transaction element file info
83 * @return file info iterator
86 mapInitIterator(/*@kept@*/ const void * this, /*@kept@*/ const void * that)
88 rpmTransactionSet ts = (void *)this;
89 TFI_t fi = (void *)that;
92 iter = xcalloc(1, sizeof(*iter));
95 iter->reverse = (fi->type == TR_REMOVED && fi->action != FA_COPYOUT);
96 iter->i = (iter->reverse ? (fi->fc - 1) : 0);
97 iter->isave = iter->i;
102 * Return next index into file info.
103 * @param this file info iterator
104 * @return next index, -1 on termination
106 static int mapNextIterator(void * this) {
108 const TFI_t fi = iter->fi;
112 if (iter->i >= 0) i = iter->i--;
114 if (iter->i < fi->fc) i = iter->i++;
122 static int cpioStrCmp(const void * a, const void * b)
124 const char * afn = *(const char **)a;
125 const char * bfn = *(const char **)b;
127 /* Match rpm-4.0 payloads with ./ prefixes. */
128 if (afn[0] == '.' && afn[1] == '/') afn += 2;
129 if (bfn[0] == '.' && bfn[1] == '/') bfn += 2;
131 /* If either path is absolute, make it relative. */
132 if (afn[0] == '/') afn += 1;
133 if (bfn[0] == '/') bfn += 1;
135 return strcmp(afn, bfn);
139 * Locate archive path in file info.
140 * @param this file info iterator
141 * @param fsmPath archive path
142 * @return index into file info, -1 if archive path was not found
144 static int mapFind(void * this, const char * fsmPath) {
146 const TFI_t fi = iter->fi;
149 if (fi && fi->fc > 0 && fi->apath && fsmPath && *fsmPath) {
152 p = bsearch(&fsmPath, fi->apath, fi->fc, sizeof(fsmPath), cpioStrCmp);
154 fprintf(stderr, "*** not mapped %s\n", fsmPath);
156 iter->i = p - fi->apath;
157 ix = mapNextIterator(iter);
164 * Directory name iterator.
166 typedef struct dnli_s {
167 /*@dependent@*/ TFI_t fi;
168 /*@only@*/ /*@null@*/ char * active;
175 * Destroy directory name iterator.
176 * @param this file info iterator
177 * @retval NULL always
179 static /*@null@*/ void * dnlFreeIterator(/*@only@*//*@null@*/ const void * this)
182 DNLI_t dnli = (void *)this;
183 if (dnli->active) free(dnli->active);
190 static inline int dnlCount(const DNLI_t dnli) {
191 return (dnli ? dnli->fi->dc : 0);
196 static inline int dnlIndex(const DNLI_t dnli) {
197 return (dnli ? dnli->isave : -1);
201 * Create directory name iterator.
202 * @param fsm file state machine data
203 * @param reverse traverse directory names in reverse order?
204 * @return directory name iterator
206 static /*@only@*/ void * dnlInitIterator(const FSM_t fsm, int reverse)
208 TFI_t fi = fsmGetFi(fsm);
214 dnli = xcalloc(1, sizeof(*dnli));
216 dnli->reverse = reverse;
217 dnli->i = (reverse ? fi->dc : 0);
220 dnli->active = xcalloc(fi->dc, sizeof(*dnli->active));
222 /* Identify parent directories not skipped. */
223 for (i = 0; i < fi->fc; i++)
224 if (!XFA_SKIPPING(fi->actions[i])) dnli->active[fi->dil[i]] = 1;
226 /* Exclude parent directories that are explicitly included. */
227 for (i = 0; i < fi->fc; i++) {
228 int dil, dnlen, bnlen;
230 if (!S_ISDIR(fi->fmodes[i]))
234 dnlen = strlen(fi->dnl[dil]);
235 bnlen = strlen(fi->bnl[i]);
237 for (j = 0; j < fi->dc; j++) {
241 if (!dnli->active[j] || j == dil) continue;
244 if (jlen != (dnlen+bnlen+1)) continue;
245 if (strncmp(dnl, fi->dnl[dil], dnlen)) continue;
246 if (strncmp(dnl+dnlen, fi->bnl[i], bnlen)) continue;
247 if (dnl[dnlen+bnlen] != '/' || dnl[dnlen+bnlen+1] != '\0')
249 /* This directory is included in the package. */
255 /* Print only once per package. */
258 for (i = 0; i < fi->dc; i++) {
259 if (!dnli->active[i]) continue;
262 rpmMessage(RPMMESS_DEBUG,
263 _("========= Directories not explictly included in package:\n"));
265 rpmMessage(RPMMESS_DEBUG, _("%9d %s\n"), i, fi->dnl[i]);
268 rpmMessage(RPMMESS_DEBUG, "=========\n");
275 * Return next directory name (from file info).
276 * @param dnli directory name iterator
277 * @return next directory name
279 static /*@observer@*/ const char * dnlNextIterator(/*@null@*/ DNLI_t dnli)
281 const char * dn = NULL;
283 if (dnli && dnli->active) {
288 i = (!dnli->reverse ? dnli->i++ : --dnli->i);
289 } while (i >= 0 && i < fi->dc && !dnli->active[i]);
291 if (i >= 0 && i < fi->dc)
301 * Save hard link in chain.
302 * @param fsm file state machine data
303 * @return Is chain only partially filled?
305 static int saveHardLink(FSM_t fsm)
307 struct stat * st = &fsm->sb;
312 /* Find hard link set. */
313 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
314 if (fsm->li->inode == st->st_ino && fsm->li->dev == st->st_dev)
318 /* New hard link encountered, add new link to set. */
319 if (fsm->li == NULL) {
320 fsm->li = xcalloc(1, sizeof(*fsm->li));
321 fsm->li->next = NULL;
322 fsm->li->nlink = st->st_nlink;
323 fsm->li->dev = st->st_dev;
324 fsm->li->inode = st->st_ino;
325 fsm->li->linkIndex = -1;
326 fsm->li->createdPath = -1;
328 fsm->li->filex = xcalloc(st->st_nlink, sizeof(fsm->li->filex[0]));
329 memset(fsm->li->filex, -1, (st->st_nlink * sizeof(fsm->li->filex[0])));
330 fsm->li->nsuffix = xcalloc(st->st_nlink, sizeof(*fsm->li->nsuffix));
332 if (fsm->goal == FSM_PKGBUILD)
333 fsm->li->linksLeft = st->st_nlink;
334 if (fsm->goal == FSM_PKGINSTALL)
335 fsm->li->linksLeft = 0;
337 fsm->li->next = fsm->links;
338 fsm->links = fsm->li;
341 if (fsm->goal == FSM_PKGBUILD) --fsm->li->linksLeft;
342 fsm->li->filex[fsm->li->linksLeft] = fsm->ix;
344 fsm->li->nsuffix[fsm->li->linksLeft] = fsm->nsuffix;
346 if (fsm->goal == FSM_PKGINSTALL) fsm->li->linksLeft++;
349 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]);
352 if (fsm->goal == FSM_PKGBUILD)
353 return (fsm->li->linksLeft > 0);
355 if (fsm->goal != FSM_PKGINSTALL)
358 if (!(st->st_size || fsm->li->linksLeft == st->st_nlink))
361 /* Here come the bits, time to choose a non-skipped file name. */
362 { TFI_t fi = fsmGetFi(fsm);
364 for (j = fsm->li->linksLeft - 1; j >= 0; j--) {
365 ix = fsm->li->filex[j];
366 if (ix < 0 || XFA_SKIPPING(fi->actions[ix]))
372 /* Are all links skipped or not encountered yet? */
374 return 1; /* XXX W2DO? */
376 /* Save the non-skipped file name and map index. */
377 fsm->li->linkIndex = j;
378 fsm->path = _free(fsm->path);
380 rc = fsmStage(fsm, FSM_MAP);
385 * Destroy set of hard links.
386 * @param li set of hard links
388 static /*@null@*/ void * freeHardLink(/*@only@*/ /*@null@*/ struct hardLink * li)
391 li->nsuffix = _free(li->nsuffix); /* XXX elements are shared */
392 li->filex = _free(li->filex);
398 FSM_t fsm = xcalloc(1, sizeof(*fsm));
402 FSM_t freeFSM(FSM_t fsm)
405 fsm->path = _free(fsm->path);
406 while ((fsm->li = fsm->links) != NULL) {
407 fsm->links = fsm->li->next;
408 fsm->li->next = NULL;
409 fsm->li = freeHardLink(fsm->li);
411 fsm->dnlx = _free(fsm->dnlx);
412 fsm->ldn = _free(fsm->ldn);
413 fsm->iter = mapFreeIterator(fsm->iter);
418 int fsmSetup(FSM_t fsm, fileStage goal,
419 const rpmTransactionSet ts, const TFI_t fi, FD_t cfd,
420 unsigned int * archiveSize, const char ** failedFile)
427 fsm->cfd = fdLink(cfd, "persist (fsm)");
428 pos = fdGetCpioPos(fsm->cfd);
429 fdSetCpioPos(fsm->cfd, 0);
431 fsm->iter = mapInitIterator(ts, fi);
433 if (fsm->goal == FSM_PKGINSTALL) {
434 if (ts && ts->notify) {
435 (void)ts->notify(fi->h, RPMCALLBACK_INST_START, 0, fi->archiveSize,
436 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
440 fsm->archiveSize = archiveSize;
441 if (fsm->archiveSize)
442 *fsm->archiveSize = 0;
443 fsm->failedFile = failedFile;
445 *fsm->failedFile = NULL;
447 memset(fsm->sufbuf, 0, sizeof(fsm->sufbuf));
448 if (fsm->goal == FSM_PKGINSTALL) {
450 sprintf(fsm->sufbuf, ";%08x", (unsigned)ts->id);
454 rc = fsmStage(fsm, FSM_CREATE);
456 rc = fsmStage(fsm, fsm->goal);
458 if (!rc && fsm->archiveSize)
459 *fsm->archiveSize = (fdGetCpioPos(fsm->cfd) - pos);
464 int fsmTeardown(FSM_t fsm) {
468 rc = fsmStage(fsm, FSM_DESTROY);
470 fsm->iter = mapFreeIterator(fsm->iter);
472 fsm->cfd = fdFree(fsm->cfd, "persist (fsm)");
475 fsm->failedFile = NULL;
479 int fsmMapPath(FSM_t fsm)
481 TFI_t fi = fsmGetFi(fsm); /* XXX const except for fstates */
488 fsm->action = FA_UNKNOWN;
492 if (fi && i >= 0 && i < fi->fc) {
494 fsm->astriplen = fi->astriplen;
495 fsm->action = (fi->actions ? fi->actions[i] : fi->action);
496 fsm->fflags = (fi->fflags ? fi->fflags[i] : fi->flags);
497 fsm->mapFlags = (fi->fmapflags ? fi->fmapflags[i] : fi->mapflags);
499 /* src rpms have simple base name in payload. */
500 fsm->dirName = fi->dnl[fi->dil[i]];
501 fsm->baseName = fi->bnl[i];
503 switch (fsm->action) {
506 case FA_SKIPMULTILIB: /* XXX RPMFILE_STATE_MULTILIB? */
507 fprintf(stderr, "*** %s:%s %s\n", fiTypeString(fi), fileActionString(fsm->action), (fsm->path ? fsm->path : ""));
510 fprintf(stderr, "*** %s:%s %s\n", fiTypeString(fi), fileActionString(fsm->action), (fsm->path ? fsm->path : ""));
517 assert(fi->type == TR_ADDED);
521 if (fi->type == TR_ADDED)
522 fi->fstates[i] = RPMFILE_STATE_NOTINSTALLED;
525 case FA_SKIPNETSHARED:
526 if (fi->type == TR_ADDED)
527 fi->fstates[i] = RPMFILE_STATE_NETSHARED;
533 fsm->osuffix = SUFFIX_RPMORIG;
536 fsm->osuffix = SUFFIX_RPMSAVE;
542 assert(fi->type == TR_ADDED);
543 fsm->nsuffix = SUFFIX_RPMNEW;
547 fprintf(stderr, "*** %s:%s %s\n", fiTypeString(fi), fileActionString(fsm->action), (fsm->path ? fsm->path : ""));
548 assert(fi->type == TR_ADDED);
549 fsm->osuffix = SUFFIX_RPMSAVE;
552 assert(fi->type == TR_REMOVED);
555 fprintf(stderr, "*** %s:%s %s\n", fiTypeString(fi), fileActionString(fsm->action), (fsm->path ? fsm->path : ""));
559 if ((fsm->mapFlags & CPIO_MAP_PATH) || fsm->nsuffix) {
560 const struct stat * st = &fsm->sb;
561 fsm->path = _free(fsm->path);
562 fsm->path = fsmFsPath(fsm, st, fsm->subdir,
563 (fsm->suffix ? fsm->suffix : fsm->nsuffix));
569 int fsmMapAttrs(FSM_t fsm)
571 struct stat * st = &fsm->sb;
572 TFI_t fi = fsmGetFi(fsm);
575 if (fi && i >= 0 && i < fi->fc) {
577 (S_ISDIR(st->st_mode) ? fi->dperms : fi->fperms);
579 (fi->fmodes ? fi->fmodes[i] : perms);
581 (fi->fuids ? fi->fuids[i] : fi->uid); /* XXX chmod u-s */
583 (fi->fgids ? fi->fgids[i] : fi->gid); /* XXX chmod g-s */
585 if (fsm->mapFlags & CPIO_MAP_MODE)
586 st->st_mode = (st->st_mode & S_IFMT) | finalMode;
587 if (fsm->mapFlags & CPIO_MAP_UID)
588 st->st_uid = finalUid;
589 if (fsm->mapFlags & CPIO_MAP_GID)
590 st->st_gid = finalGid;
592 fsm->fmd5sum = (fi->fmd5s ? fi->fmd5s[i] : NULL);
599 * Create file from payload stream.
600 * @todo Legacy: support brokenEndian MD5 checks?
601 * @param fsm file state machine data
602 * @return 0 on success
604 static int expandRegular(FSM_t fsm)
605 /*@modifies fileSystem, fsm @*/
607 const char * fmd5sum;
608 const struct stat * st = &fsm->sb;
609 int left = st->st_size;
612 rc = fsmStage(fsm, FSM_WOPEN);
616 /* XXX md5sum's will break on repackaging that includes modified files. */
617 fmd5sum = fsm->fmd5sum;
619 /* XXX This doesn't support brokenEndian checks. */
620 if (st->st_size > 0 && fmd5sum)
621 fdInitMD5(fsm->wfd, 0);
625 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
626 rc = fsmStage(fsm, FSM_DREAD);
630 rc = fsmStage(fsm, FSM_WRITE);
636 /* don't call this with fileSize == fileComplete */
638 (void) fsmStage(fsm, FSM_NOTIFY);
641 if (st->st_size > 0 && fmd5sum) {
642 const char * md5sum = NULL;
645 fdFiniMD5(fsm->wfd, (void **)&md5sum, NULL, 1);
647 if (md5sum == NULL) {
648 rc = CPIOERR_MD5SUM_MISMATCH;
650 if (strcmp(md5sum, fmd5sum))
651 rc = CPIOERR_MD5SUM_MISMATCH;
652 md5sum = _free(md5sum);
657 (void) fsmStage(fsm, FSM_WCLOSE);
662 * Write next item to payload stream.
663 * @param fsm file state machine data
664 * @param writeData should data be written?
665 * @return 0 on success
667 static int writeFile(FSM_t fsm, int writeData)
670 const char * path = fsm->path;
671 const char * opath = fsm->opath;
672 struct stat * st = &fsm->sb;
673 struct stat * ost = &fsm->osb;
674 size_t pos = fdGetCpioPos(fsm->cfd);
675 char * symbuf = NULL;
679 st->st_size = (writeData ? ost->st_size : 0);
680 if (S_ISDIR(st->st_mode)) {
682 } else if (S_ISLNK(st->st_mode)) {
684 * While linux puts the size of a symlink in the st_size field,
685 * I don't think that's a specified standard.
687 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
688 rc = fsmStage(fsm, FSM_READLINK);
690 st->st_size = fsm->rdnb;
691 symbuf = alloca_strdup(fsm->rdbuf); /* XXX save readlink return. */
694 if (fsm->mapFlags & CPIO_MAP_ABSOLUTE) {
695 int nb = strlen(fsm->dirName) + strlen(fsm->baseName) + sizeof(".");
696 char * t = alloca(nb);
699 if (fsm->mapFlags & CPIO_MAP_ADDDOT)
701 t = stpcpy( stpcpy(t, fsm->dirName), fsm->baseName);
702 } else if (fsm->mapFlags & CPIO_MAP_PATH) {
703 TFI_t fi = fsmGetFi(fsm);
705 (fi->apath ? fi->apath[fsm->ix] + fi->striplen : fi->bnl[fsm->ix]);
708 rc = fsmStage(fsm, FSM_HWRITE);
712 if (writeData && S_ISREG(st->st_mode)) {
715 void * mapped = (void *)-1;
719 rc = fsmStage(fsm, FSM_ROPEN);
722 /* XXX unbuffered mmap generates *lots* of fdio debugging */
725 mapped = mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, Fileno(fsm->rfd), 0);
726 if (mapped != (void *)-1) {
728 fsm->rdbuf = (char *) mapped;
729 fsm->rdlen = nmapped = st->st_size;
737 if (mapped != (void *)-1) {
742 fsm->rdlen = (left > fsm->rdsize ? fsm->rdsize : left),
743 rc = fsmStage(fsm, FSM_READ);
747 /* XXX DWRITE uses rdnb for I/O length. */
748 rc = fsmStage(fsm, FSM_DWRITE);
755 if (mapped != (void *)-1) {
756 /*@-noeffect@*/ munmap(mapped, nmapped) /*@=noeffect@*/;
761 } else if (writeData && S_ISLNK(st->st_mode)) {
762 /* XXX DWRITE uses rdnb for I/O length. */
763 strcpy(fsm->rdbuf, symbuf); /* XXX restore readlink buffer. */
764 fsm->rdnb = strlen(symbuf);
765 rc = fsmStage(fsm, FSM_DWRITE);
769 rc = fsmStage(fsm, FSM_PAD);
772 { const rpmTransactionSet ts = fsmGetTs(fsm);
773 TFI_t fi = fsmGetFi(fsm);
774 if (ts && fi && ts->notify) {
775 size_t size = (fdGetCpioPos(fsm->cfd) - pos);
776 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS, size, size,
777 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
785 (void) fsmStage(fsm, FSM_RCLOSE);
792 * Write set of linked files to payload stream.
793 * @param fsm file state machine data
794 * @return 0 on success
796 static int writeLinkedFile(FSM_t fsm)
799 const char * path = fsm->path;
800 const char * nsuffix = fsm->nsuffix;
801 int iterIndex = fsm->ix;
810 for (i = fsm->li->nlink - 1; i >= 0; i--) {
811 if (fsm->li->filex[i] < 0) continue;
813 fsm->ix = fsm->li->filex[i];
814 rc = fsmStage(fsm, FSM_MAP);
816 /* Write data after last link. */
817 rc = writeFile(fsm, (i == 0));
818 if (rc && fsm->failedFile && *fsm->failedFile == NULL) {
820 *fsm->failedFile = xstrdup(fsm->path);
823 fsm->path = _free(fsm->path);
824 fsm->li->filex[i] = -1;
828 fsm->nsuffix = nsuffix;
834 * Create pending hard links to existing file.
835 * @param fsm file state machine data
836 * @return 0 on success
838 static int fsmMakeLinks(FSM_t fsm)
840 const char * path = fsm->path;
841 const char * opath = fsm->opath;
842 const char * nsuffix = fsm->nsuffix;
843 int iterIndex = fsm->ix;
853 fsm->ix = fsm->li->filex[fsm->li->createdPath];
854 rc = fsmStage(fsm, FSM_MAP);
855 fsm->opath = fsm->path;
857 for (i = 0; i < fsm->li->nlink; i++) {
858 if (fsm->li->filex[i] < 0) continue;
859 if (i == fsm->li->createdPath) continue;
861 fsm->ix = fsm->li->filex[i];
862 rc = fsmStage(fsm, FSM_MAP);
863 rc = fsmStage(fsm, FSM_VERIFY);
865 if (rc != CPIOERR_LSTAT_FAILED) break;
867 /* XXX link(fsm->opath, fsm->path) */
868 rc = fsmStage(fsm, FSM_LINK);
869 if (rc && fsm->failedFile && *fsm->failedFile == NULL) {
871 *fsm->failedFile = xstrdup(fsm->path);
874 fsm->li->linksLeft--;
876 fsm->opath = _free(fsm->opath);
879 fsm->nsuffix = nsuffix;
886 * Commit hard linked file set atomically.
887 * @param fsm file state machine data
888 * @return 0 on success
890 static int fsmCommitLinks(FSM_t fsm)
892 const char * path = fsm->path;
893 const char * nsuffix = fsm->nsuffix;
894 int iterIndex = fsm->ix;
895 struct stat * st = &fsm->sb;
903 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
904 if (fsm->li->inode == st->st_ino && fsm->li->dev == st->st_dev)
908 for (i = 0; i < fsm->li->nlink; i++) {
909 if (fsm->li->filex[i] < 0) continue;
910 fsm->ix = fsm->li->filex[i];
911 rc = fsmStage(fsm, FSM_MAP);
912 rc = fsmStage(fsm, FSM_COMMIT);
913 fsm->path = _free(fsm->path);
914 fsm->li->filex[i] = -1;
918 fsm->nsuffix = nsuffix;
924 * Remove (if created) directories not explicitly included in package.
925 * @param fsm file state machine data
926 * @return 0 on success
928 static int fsmRmdirs(FSM_t fsm)
930 const char * path = fsm->path;
931 void * dnli = dnlInitIterator(fsm, 1);
932 char * dn = fsm->rdbuf;
933 int dc = dnlCount(dnli);
938 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
939 int dnlen = strlen(fsm->path);
943 if (fsm->dnlx[dc] < 1 || fsm->dnlx[dc] >= dnlen)
946 /* Copy to avoid const on fsm->path. */
947 te = stpcpy(dn, fsm->path) - 1;
950 /* Remove generated directories. */
954 rc = fsmStage(fsm, FSM_RMDIR);
959 } while ((te - dn) > fsm->dnlx[dc]);
961 dnli = dnlFreeIterator(dnli);
967 * Create (if necessary) directories not explicitly included in package.
968 * @param fsm file state machine data
969 * @return 0 on success
971 static int fsmMkdirs(FSM_t fsm)
973 struct stat * st = &fsm->sb;
974 struct stat * ost = &fsm->osb;
975 const char * path = fsm->path;
976 mode_t st_mode = st->st_mode;
977 void * dnli = dnlInitIterator(fsm, 0);
978 char * dn = fsm->rdbuf;
979 int dc = dnlCount(dnli);
986 fsm->dnlx = (dc ? xcalloc(dc, sizeof(*fsm->dnlx)) : NULL);
987 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
988 int dnlen = strlen(fsm->path);
992 if (dc < 0) continue;
993 fsm->dnlx[dc] = dnlen;
996 if (dnlen <= fsm->ldnlen && !strcmp(fsm->path, fsm->ldn))
999 /* Copy to avoid const on fsm->path. */
1000 (void) stpcpy(dn, fsm->path);
1003 /* Assume '/' directory exists, otherwise "mkdir -p" if non-existent. */
1004 for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
1005 if (*te != '/') continue;
1009 /* Already validated? */
1010 if (i < fsm->ldnlen &&
1011 (fsm->ldn[i] == '/' || fsm->ldn[i] == '\0') &&
1012 !strncmp(fsm->path, fsm->ldn, i))
1015 /* Move pre-existing path marker forward. */
1016 fsm->dnlx[dc] = (te - dn);
1020 /* Validate next component of path. */
1021 rc = fsmStage(fsm, FSM_LSTAT);
1024 /* Directory already exists? */
1025 if (rc == 0 && S_ISDIR(ost->st_mode)) {
1026 /* Move pre-existing path marker forward. */
1027 fsm->dnlx[dc] = (te - dn);
1028 } else if (rc == CPIOERR_LSTAT_FAILED) {
1029 TFI_t fi = fsmGetFi(fsm);
1031 st->st_mode = S_IFDIR | (fi->dperms & 07777);
1032 rc = fsmStage(fsm, FSM_MKDIR);
1034 rpmMessage(RPMMESS_WARNING,
1035 _("%s directory created with perms %04o.\n"),
1036 fsm->path, (unsigned)(st->st_mode & 07777));
1043 /* Save last validated path. */
1044 if (fsm->ldnalloc < (dnlen + 1)) {
1045 fsm->ldnalloc = dnlen + 100;
1046 fsm->ldn = xrealloc(fsm->ldn, fsm->ldnalloc);
1048 strcpy(fsm->ldn, fsm->path);
1049 fsm->ldnlen = dnlen;
1051 dnli = dnlFreeIterator(dnli);
1053 st->st_mode = st_mode; /* XXX restore st->st_mode */
1058 int fsmStage(FSM_t fsm, fileStage stage)
1061 fileStage prevStage = fsm->stage;
1062 const char * const prev = fileStageString(prevStage);
1064 static int modulo = 4;
1065 const char * const cur = fileStageString(stage);
1066 struct stat * st = &fsm->sb;
1067 struct stat * ost = &fsm->osb;
1068 int saveerrno = errno;
1073 #define _fafilter(_a) \
1074 (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
1075 ? fileActionString(_a) : "")
1077 if (stage & FSM_DEAD) {
1079 } else if (stage & FSM_INTERNAL) {
1080 if (_fsm_debug && !(stage & FSM_SYSCALL))
1081 rpmMessage(RPMMESS_DEBUG, " %8s %06o%3d (%4d,%4d)%10d %s %s\n",
1083 (unsigned)st->st_mode, (int)st->st_nlink,
1084 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1085 (fsm->path ? fsm->path : ""),
1086 _fafilter(fsm->action));
1089 if (_fsm_debug || !(stage & FSM_VERBOSE))
1090 rpmMessage(RPMMESS_DEBUG, "%-8s %06o%3d (%4d,%4d)%10d %s %s\n",
1092 (unsigned)st->st_mode, (int)st->st_nlink,
1093 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1094 (fsm->path ? fsm->path + fsm->astriplen : ""),
1095 _fafilter(fsm->action));
1102 case FSM_PKGINSTALL:
1104 /* Clean fsm, free'ing memory. Read next archive header. */
1105 rc = fsmStage(fsm, FSM_INIT);
1107 /* Exit on end-of-payload. */
1108 if (rc == CPIOERR_HDR_TRAILER) {
1113 /* Exit on error. */
1116 (void) fsmStage(fsm, FSM_UNDO);
1120 /* Extract file from archive. */
1121 rc = fsmStage(fsm, FSM_PROCESS);
1123 (void) fsmStage(fsm, FSM_UNDO);
1127 /* Notify on success. */
1128 (void) fsmStage(fsm, FSM_NOTIFY);
1130 if (fsmStage(fsm, FSM_FINI))
1137 /* Clean fsm, free'ing memory. */
1138 rc = fsmStage(fsm, FSM_INIT);
1140 /* Exit on end-of-payload. */
1141 if (rc == CPIOERR_HDR_TRAILER) {
1146 /* Rename/erase next item. */
1147 if (fsmStage(fsm, FSM_FINI))
1154 rc = fsmStage(fsm, FSM_INIT);
1156 /* Exit on end-of-payload. */
1157 if (rc == CPIOERR_HDR_TRAILER) {
1162 /* Exit on error. */
1165 (void) fsmStage(fsm, FSM_UNDO);
1169 /* Copy file into archive. */
1170 rc = fsmStage(fsm, FSM_PROCESS);
1172 (void) fsmStage(fsm, FSM_UNDO);
1176 if (fsmStage(fsm, FSM_FINI))
1181 rc = fsmStage(fsm, FSM_TRAILER);
1184 { rpmTransactionSet ts = fsmGetTs(fsm);
1185 #define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
1186 fsm->commit = ((ts && (ts->transFlags & _tsmask) &&
1187 fsm->goal != FSM_PKGCOMMIT) ? 0 : 1);
1190 fsm->path = _free(fsm->path);
1191 fsm->opath = _free(fsm->opath);
1192 fsm->dnlx = _free(fsm->dnlx);
1194 fsm->ldn = _free(fsm->ldn);
1195 fsm->ldnalloc = fsm->ldnlen = 0;
1197 fsm->rdsize = fsm->wrsize = 0;
1198 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1199 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1200 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1201 fsm->rdsize = 8 * BUFSIZ;
1202 fsm->rdbuf = fsm->rdb = xmalloc(fsm->rdsize);
1203 fsm->wrsize = 8 * BUFSIZ;
1204 fsm->wrbuf = fsm->wrb = xmalloc(fsm->wrsize);
1207 fsm->mkdirsdone = 0;
1211 errno = 0; /* XXX get rid of EBADF */
1213 /* Detect and create directories not explicitly in package. */
1214 if (fsm->goal == FSM_PKGINSTALL) {
1215 rc = fsmStage(fsm, FSM_MKDIRS);
1216 if (!rc) fsm->mkdirsdone = 1;
1221 fsm->path = _free(fsm->path);
1223 fsm->diskchecked = fsm->exists = 0;
1225 fsm->suffix = (fsm->sufbuf[0] != '\0' ? fsm->sufbuf : NULL);
1226 fsm->action = FA_UNKNOWN;
1227 fsm->osuffix = NULL;
1228 fsm->nsuffix = NULL;
1230 if (fsm->goal == FSM_PKGINSTALL) {
1231 /* Read next header from payload, checking for end-of-payload. */
1232 rc = fsmStage(fsm, FSM_NEXT);
1236 /* Identify mapping index. */
1237 fsm->ix = ((fsm->goal == FSM_PKGINSTALL)
1238 ? mapFind(fsm->iter, fsm->path) : mapNextIterator(fsm->iter));
1240 /* On non-install, detect end-of-loop. */
1241 if (fsm->goal != FSM_PKGINSTALL && fsm->ix < 0) {
1242 rc = CPIOERR_HDR_TRAILER;
1246 /* On non-install, mode must be known so that dirs don't get suffix. */
1247 if (fsm->goal != FSM_PKGINSTALL) {
1248 TFI_t fi = fsmGetFi(fsm);
1249 st->st_mode = fi->fmodes[fsm->ix];
1252 /* Generate file path. */
1253 rc = fsmStage(fsm, FSM_MAP);
1256 /* Perform lstat/stat for disk file. */
1257 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1258 ? FSM_LSTAT : FSM_STAT));
1259 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1263 } else if (rc == 0) {
1266 fsm->diskchecked = 1;
1269 /* On non-install, the disk file stat is what's remapped. */
1270 if (fsm->goal != FSM_PKGINSTALL)
1271 *st = *ost; /* structure assignment */
1273 /* Remap file perms, owner, and group. */
1274 rc = fsmMapAttrs(fsm);
1277 fsm->postpone = XFA_SKIPPING(fsm->action);
1278 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1279 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1280 fsm->postpone = saveHardLink(fsm);
1286 rc = fsmMapPath(fsm);
1289 rc = fsmMkdirs(fsm);
1293 rc = fsmRmdirs(fsm);
1296 if (fsm->postpone) {
1297 if (fsm->goal == FSM_PKGINSTALL)
1298 rc = fsmStage(fsm, FSM_EAT);
1302 if (fsm->goal == FSM_PKGBUILD) {
1303 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1304 struct hardLink * li, * prev;
1305 rc = writeLinkedFile(fsm);
1306 if (rc) break; /* W2DO? */
1308 for (li = fsm->links, prev = NULL; li; prev = li, li = li->next)
1309 if (li == fsm->li) break;
1312 fsm->links = fsm->li->next;
1314 prev->next = fsm->li->next;
1315 fsm->li->next = NULL;
1316 fsm->li = freeHardLink(fsm->li);
1318 rc = writeFile(fsm, 1);
1323 if (fsm->goal != FSM_PKGINSTALL)
1326 if (S_ISREG(st->st_mode)) {
1327 const char * path = fsm->path;
1329 fsm->path = fsmFsPath(fsm, st, NULL, NULL);
1330 rc = fsmStage(fsm, FSM_VERIFY);
1332 if (rc == 0 && fsm->osuffix) {
1333 const char * opath = fsm->opath;
1334 fsm->opath = fsm->path;
1335 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1336 rc = fsmStage(fsm, FSM_RENAME);
1338 rpmMessage(RPMMESS_WARNING,
1339 _("%s saved as %s\n"), fsm->opath, fsm->path);
1340 fsm->path = _free(fsm->path);
1345 if (rc != CPIOERR_LSTAT_FAILED) return rc;
1346 rc = expandRegular(fsm);
1347 } else if (S_ISDIR(st->st_mode)) {
1348 mode_t st_mode = st->st_mode;
1349 rc = fsmStage(fsm, FSM_VERIFY);
1350 if (rc == CPIOERR_LSTAT_FAILED) {
1351 st->st_mode &= ~07777; /* XXX abuse st->st_mode */
1352 st->st_mode |= 00700;
1353 rc = fsmStage(fsm, FSM_MKDIR);
1354 st->st_mode = st_mode; /* XXX restore st->st_mode */
1356 } else if (S_ISLNK(st->st_mode)) {
1357 const char * opath = fsm->opath;
1359 if ((st->st_size + 1) > fsm->rdsize) {
1360 rc = CPIOERR_HDR_SIZE;
1364 fsm->wrlen = st->st_size;
1365 rc = fsmStage(fsm, FSM_DREAD);
1366 if (!rc && fsm->rdnb != fsm->wrlen)
1367 rc = CPIOERR_READ_FAILED;
1370 fsm->wrbuf[st->st_size] = '\0';
1371 /* XXX symlink(fsm->opath, fsm->path) */
1372 fsm->opath = fsm->wrbuf; /* XXX abuse fsm->path */
1373 rc = fsmStage(fsm, FSM_VERIFY);
1374 if (rc == CPIOERR_LSTAT_FAILED)
1375 rc = fsmStage(fsm, FSM_SYMLINK);
1376 fsm->opath = opath; /* XXX restore fsm->path */
1377 } else if (S_ISFIFO(st->st_mode)) {
1378 mode_t st_mode = st->st_mode;
1379 /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
1380 rc = fsmStage(fsm, FSM_VERIFY);
1381 if (rc == CPIOERR_LSTAT_FAILED) {
1382 st->st_mode = 0000; /* XXX abuse st->st_mode */
1383 rc = fsmStage(fsm, FSM_MKFIFO);
1384 st->st_mode = st_mode; /* XXX restore st->st_mode */
1386 } else if (S_ISCHR(st->st_mode) ||
1387 S_ISBLK(st->st_mode) ||
1388 S_ISSOCK(st->st_mode))
1390 rc = fsmStage(fsm, FSM_VERIFY);
1391 if (rc == CPIOERR_LSTAT_FAILED)
1392 rc = fsmStage(fsm, FSM_MKNOD);
1394 rc = CPIOERR_UNKNOWN_FILETYPE;
1396 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1397 fsm->li->createdPath = fsm->li->linkIndex;
1398 rc = fsmMakeLinks(fsm);
1404 rc = fsmMakeLinks(fsm);
1406 case FSM_NOTIFY: /* XXX move from fsm to psm -> tsm */
1407 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1408 rpmTransactionSet ts = fsmGetTs(fsm);
1409 TFI_t fi = fsmGetFi(fsm);
1410 if (ts && ts->notify && fi)
1411 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS,
1412 fdGetCpioPos(fsm->cfd), fi->archiveSize,
1413 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
1419 if (fsm->goal == FSM_PKGINSTALL) {
1420 (void) fsmStage(fsm,
1421 (S_ISDIR(st->st_mode) ? FSM_RMDIR : FSM_UNLINK));
1423 #ifdef NOTYET /* XXX remove only dirs just created, not all. */
1425 (void) fsmStage(fsm, FSM_RMDIRS);
1429 if (fsm->failedFile && *fsm->failedFile == NULL)
1430 *fsm->failedFile = xstrdup(fsm->path);
1433 if (!fsm->postpone && fsm->commit) {
1434 if (fsm->goal == FSM_PKGINSTALL)
1435 rc = ((!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1436 ? fsmCommitLinks(fsm) : fsmStage(fsm, FSM_COMMIT));
1437 if (fsm->goal == FSM_PKGCOMMIT)
1438 rc = fsmStage(fsm, FSM_COMMIT);
1439 if (fsm->goal == FSM_PKGERASE)
1440 rc = fsmStage(fsm, FSM_COMMIT);
1442 fsm->path = _free(fsm->path);
1443 fsm->opath = _free(fsm->opath);
1444 memset(st, 0, sizeof(*st));
1445 memset(ost, 0, sizeof(*ost));
1448 /* Rename pre-existing modified or unmanaged file. */
1449 if (fsm->diskchecked && fsm->exists && fsm->osuffix) {
1450 const char * opath = fsm->opath;
1451 const char * path = fsm->path;
1452 fsm->opath = fsmFsPath(fsm, st, NULL, NULL);
1453 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1454 rc = fsmStage(fsm, FSM_RENAME);
1456 rpmMessage(RPMMESS_WARNING, _("%s saved as %s\n"),
1457 fsm->opath, fsm->path);
1459 fsm->path = _free(fsm->path);
1461 fsm->opath = _free(fsm->opath);
1465 /* Remove erased files. */
1466 if (fsm->goal == FSM_PKGERASE) {
1467 if (fsm->action == FA_ERASE) {
1468 TFI_t fi = fsmGetFi(fsm);
1469 if (S_ISDIR(st->st_mode)) {
1470 rc = fsmStage(fsm, FSM_RMDIR);
1473 case ENOENT: /* XXX rmdir("/") linux 2.2.x kernel hack */
1475 /* XXX make sure that build side permits %missingok on directories. */
1476 if (fsm->fflags & RPMFILE_MISSINGOK)
1479 /* XXX common error message. */
1480 rpmError(RPMERR_RMDIR,
1481 _("%s rmdir of %s failed: Directory not empty\n"),
1482 fiTypeString(fi), fsm->path);
1485 rpmError(RPMERR_RMDIR,
1486 _("%s rmdir of %s failed: %s\n"),
1487 fiTypeString(fi), fsm->path, strerror(errno));
1491 rc = fsmStage(fsm, FSM_UNLINK);
1493 if (!(errno == ENOENT && (fsm->fflags & RPMFILE_MISSINGOK)))
1494 rpmError(RPMERR_UNLINK,
1495 _("%s unlink of %s failed: %s\n"),
1496 fiTypeString(fi), fsm->path, strerror(errno));
1502 if (!S_ISSOCK(st->st_mode)) { /* XXX /dev/log et al are skipped */
1503 /* Rename temporary to final file name. */
1504 if (!S_ISDIR(st->st_mode) &&
1505 (fsm->subdir || fsm->suffix || fsm->nsuffix))
1507 fsm->opath = fsm->path;
1508 fsm->path = fsmFsPath(fsm, st, NULL, fsm->nsuffix);
1509 rc = fsmStage(fsm, FSM_RENAME);
1510 if (!rc && fsm->nsuffix) {
1511 const char * opath = fsmFsPath(fsm, st, NULL, NULL);
1512 rpmMessage(RPMMESS_WARNING, _("%s created as %s\n"),
1514 opath = _free(opath);
1516 fsm->opath = _free(fsm->opath);
1518 if (S_ISLNK(st->st_mode)) {
1519 if (!rc && !getuid())
1520 rc = fsmStage(fsm, FSM_LCHOWN);
1522 if (!rc && !getuid())
1523 rc = fsmStage(fsm, FSM_CHOWN);
1525 rc = fsmStage(fsm, FSM_CHMOD);
1527 time_t st_mtime = st->st_mtime;
1528 TFI_t fi = fsmGetFi(fsm);
1530 st->st_mtime = fi->fmtimes[fsm->ix];
1531 rc = fsmStage(fsm, FSM_UTIME);
1532 st->st_mtime = st_mtime;
1537 /* Notify on success. */
1538 if (!rc) rc = fsmStage(fsm, FSM_NOTIFY);
1541 fsm->path = _free(fsm->path);
1543 /* Create any remaining links (if no error), and clean up. */
1544 while ((fsm->li = fsm->links) != NULL) {
1545 fsm->links = fsm->li->next;
1546 fsm->li->next = NULL;
1547 if (fsm->goal == FSM_PKGINSTALL && fsm->commit && fsm->li->linksLeft)
1549 for (i = 0 ; i < fsm->li->linksLeft; i++) {
1550 if (fsm->li->filex[i] < 0) continue;
1551 rc = CPIOERR_MISSING_HARDLINK;
1552 if (fsm->failedFile && *fsm->failedFile == NULL) {
1553 fsm->ix = fsm->li->filex[i];
1554 if (!fsmStage(fsm, FSM_MAP)) {
1555 *fsm->failedFile = fsm->path;
1562 if (fsm->goal == FSM_PKGBUILD) {
1563 rc = CPIOERR_MISSING_HARDLINK;
1565 fsm->li = freeHardLink(fsm->li);
1567 fsm->ldn = _free(fsm->ldn);
1568 fsm->ldnalloc = fsm->ldnlen = 0;
1569 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1570 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1573 if (fsm->diskchecked && !fsm->exists) {
1574 rc = CPIOERR_LSTAT_FAILED;
1577 if (S_ISREG(st->st_mode)) {
1578 char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
1579 (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
1581 * XXX HP-UX (and other os'es) don't permit unlink on busy
1584 fsm->opath = fsm->path;
1586 rc = fsmStage(fsm, FSM_RENAME);
1588 (void) fsmStage(fsm, FSM_UNLINK);
1590 rc = CPIOERR_UNLINK_FAILED;
1591 fsm->path = fsm->opath;
1593 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1594 /*@notreached@*/ break;
1595 } else if (S_ISDIR(st->st_mode)) {
1596 if (S_ISDIR(ost->st_mode)) return 0;
1597 if (S_ISLNK(ost->st_mode)) {
1598 rc = fsmStage(fsm, FSM_STAT);
1599 if (rc == CPIOERR_STAT_FAILED && errno == ENOENT) rc = 0;
1602 if (S_ISDIR(ost->st_mode)) return 0;
1604 } else if (S_ISLNK(st->st_mode)) {
1605 if (S_ISLNK(ost->st_mode)) {
1606 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1607 rc = fsmStage(fsm, FSM_READLINK);
1610 if (!strcmp(fsm->opath, fsm->rdbuf)) return 0;
1612 } else if (S_ISFIFO(st->st_mode)) {
1613 if (S_ISFIFO(ost->st_mode)) return 0;
1614 } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
1615 if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
1616 (ost->st_rdev == st->st_rdev)) return 0;
1617 } else if (S_ISSOCK(st->st_mode)) {
1618 if (S_ISSOCK(ost->st_mode)) return 0;
1620 /* XXX shouldn't do this with commit/undo. */
1622 if (fsm->stage == FSM_PROCESS) rc = fsmStage(fsm, FSM_UNLINK);
1623 if (rc == 0) rc = CPIOERR_LSTAT_FAILED;
1624 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1625 /*@notreached@*/ break;
1628 rc = Unlink(fsm->path);
1629 if (_fsm_debug && (stage & FSM_SYSCALL))
1630 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1631 fsm->path, (rc < 0 ? strerror(errno) : ""));
1632 if (rc < 0) rc = CPIOERR_UNLINK_FAILED;
1635 rc = Rename(fsm->opath, fsm->path);
1636 if (_fsm_debug && (stage & FSM_SYSCALL))
1637 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1638 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1639 if (rc < 0) rc = CPIOERR_RENAME_FAILED;
1642 rc = Mkdir(fsm->path, (st->st_mode & 07777));
1643 if (_fsm_debug && (stage & FSM_SYSCALL))
1644 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1645 fsm->path, (unsigned)(st->st_mode & 07777),
1646 (rc < 0 ? strerror(errno) : ""));
1647 if (rc < 0) rc = CPIOERR_MKDIR_FAILED;
1650 rc = Rmdir(fsm->path);
1651 if (_fsm_debug && (stage & FSM_SYSCALL))
1652 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1653 fsm->path, (rc < 0 ? strerror(errno) : ""));
1654 if (rc < 0) rc = CPIOERR_RMDIR_FAILED;
1657 rc = chown(fsm->path, st->st_uid, st->st_gid);
1658 if (_fsm_debug && (stage & FSM_SYSCALL))
1659 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1660 fsm->path, (int)st->st_uid, (int)st->st_gid,
1661 (rc < 0 ? strerror(errno) : ""));
1662 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1665 #if ! CHOWN_FOLLOWS_SYMLINK
1666 rc = lchown(fsm->path, st->st_uid, st->st_gid);
1667 if (_fsm_debug && (stage & FSM_SYSCALL))
1668 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1669 fsm->path, (int)st->st_uid, (int)st->st_gid,
1670 (rc < 0 ? strerror(errno) : ""));
1671 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1675 rc = chmod(fsm->path, (st->st_mode & 07777));
1676 if (_fsm_debug && (stage & FSM_SYSCALL))
1677 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1678 fsm->path, (unsigned)(st->st_mode & 07777),
1679 (rc < 0 ? strerror(errno) : ""));
1680 if (rc < 0) rc = CPIOERR_CHMOD_FAILED;
1683 { struct utimbuf stamp;
1684 stamp.actime = st->st_mtime;
1685 stamp.modtime = st->st_mtime;
1686 rc = utime(fsm->path, &stamp);
1687 if (_fsm_debug && (stage & FSM_SYSCALL))
1688 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0x%x) %s\n", cur,
1689 fsm->path, (unsigned)st->st_mtime,
1690 (rc < 0 ? strerror(errno) : ""));
1691 if (rc < 0) rc = CPIOERR_UTIME_FAILED;
1695 rc = symlink(fsm->opath, fsm->path);
1696 if (_fsm_debug && (stage & FSM_SYSCALL))
1697 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1698 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1699 if (rc < 0) rc = CPIOERR_SYMLINK_FAILED;
1702 rc = Link(fsm->opath, fsm->path);
1703 if (_fsm_debug && (stage & FSM_SYSCALL))
1704 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1705 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1706 if (rc < 0) rc = CPIOERR_LINK_FAILED;
1709 rc = mkfifo(fsm->path, (st->st_mode & 07777));
1710 if (_fsm_debug && (stage & FSM_SYSCALL))
1711 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1712 fsm->path, (unsigned)(st->st_mode & 07777),
1713 (rc < 0 ? strerror(errno) : ""));
1714 if (rc < 0) rc = CPIOERR_MKFIFO_FAILED;
1718 rc = mknod(fsm->path, (st->st_mode & ~07777), st->st_rdev);
1719 if (_fsm_debug && (stage & FSM_SYSCALL))
1720 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n", cur,
1721 fsm->path, (unsigned)(st->st_mode & ~07777),
1722 (unsigned)st->st_rdev,
1723 (rc < 0 ? strerror(errno) : ""));
1724 if (rc < 0) rc = CPIOERR_MKNOD_FAILED;
1728 rc = Lstat(fsm->path, ost);
1729 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1730 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1731 fsm->path, (rc < 0 ? strerror(errno) : ""));
1732 if (rc < 0) rc = CPIOERR_LSTAT_FAILED;
1735 rc = Stat(fsm->path, ost);
1736 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1737 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1738 fsm->path, (rc < 0 ? strerror(errno) : ""));
1739 if (rc < 0) rc = CPIOERR_STAT_FAILED;
1742 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1743 rc = Readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1);
1744 if (_fsm_debug && (stage & FSM_SYSCALL))
1745 rpmMessage(RPMMESS_DEBUG, " %8s (%s, rdbuf, %d) %s\n", cur,
1746 fsm->path, (int)fsm->rdlen, (rc < 0 ? strerror(errno) : ""));
1747 if (rc < 0) rc = CPIOERR_READLINK_FAILED;
1750 fsm->rdbuf[fsm->rdnb] = '\0';
1758 rc = fsmStage(fsm, FSM_HREAD);
1760 if (!strcmp(fsm->path, CPIO_TRAILER)) { /* Detect end-of-payload. */
1761 fsm->path = _free(fsm->path);
1762 rc = CPIOERR_HDR_TRAILER;
1765 rc = fsmStage(fsm, FSM_POS);
1768 for (left = st->st_size; left > 0; left -= fsm->rdnb) {
1769 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
1770 rc = fsmStage(fsm, FSM_DREAD);
1775 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
1778 (void) fsmStage(fsm, FSM_DREAD);
1782 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
1784 memset(fsm->rdbuf, 0, left);
1785 /* XXX DWRITE uses rdnb for I/O length. */
1787 (void) fsmStage(fsm, FSM_DWRITE);
1791 rc = cpioTrailerWrite(fsm);
1794 rc = fsmStage(fsm, FSM_POS);
1796 rc = cpioHeaderRead(fsm, st); /* Read next payload header. */
1799 rc = cpioHeaderWrite(fsm, st); /* Write next payload header. */
1802 fsm->rdnb = Fread(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->wrlen, fsm->cfd);
1803 if (_fsm_debug && (stage & FSM_SYSCALL))
1804 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\trdnb %d\n",
1805 cur, (fsm->wrbuf == fsm->wrb ? "wrbuf" : "mmap"),
1806 (int)fsm->wrlen, (int)fsm->rdnb);
1807 if (fsm->rdnb != fsm->wrlen) fprintf(stderr, "*** short read, had %d, got %d\n", (int)fsm->rdnb, (int)fsm->wrlen);
1809 if (Ferror(fsm->rfd))
1810 rc = CPIOERR_READ_FAILED;
1813 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->rdnb);
1816 fsm->wrnb = Fwrite(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdnb, fsm->cfd);
1817 if (_fsm_debug && (stage & FSM_SYSCALL))
1818 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\twrnb %d\n",
1819 cur, (fsm->rdbuf == fsm->rdb ? "rdbuf" : "mmap"),
1820 (int)fsm->rdnb, (int)fsm->wrnb);
1821 if (fsm->rdnb != fsm->wrnb) fprintf(stderr, "*** short write, had %d, got %d\n", (int)fsm->rdnb, (int)fsm->wrnb);
1823 if (Ferror(fsm->wfd))
1824 rc = CPIOERR_WRITE_FAILED;
1827 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->wrnb);
1831 fsm->rfd = Fopen(fsm->path, "r.ufdio");
1832 if (fsm->rfd == NULL || Ferror(fsm->rfd)) {
1833 if (fsm->rfd) (void) fsmStage(fsm, FSM_RCLOSE);
1835 rc = CPIOERR_OPEN_FAILED;
1838 if (_fsm_debug && (stage & FSM_SYSCALL))
1839 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"r\") rfd %p rdbuf %p\n", cur,
1840 fsm->path, fsm->rfd, fsm->rdbuf);
1843 fsm->rdnb = Fread(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdlen, fsm->rfd);
1844 if (_fsm_debug && (stage & FSM_SYSCALL))
1845 rpmMessage(RPMMESS_DEBUG, " %8s (rdbuf, %d, rfd)\trdnb %d\n",
1846 cur, (int)fsm->rdlen, (int)fsm->rdnb);
1847 if (fsm->rdnb != fsm->rdlen) fprintf(stderr, "*** short read, had %d, got %d\n", (int)fsm->rdnb, (int)fsm->rdlen);
1849 if (Ferror(fsm->rfd))
1850 rc = CPIOERR_READ_FAILED;
1855 if (_fsm_debug && (stage & FSM_SYSCALL))
1856 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->rfd);
1857 (void) Fclose(fsm->rfd);
1863 fsm->wfd = Fopen(fsm->path, "w.ufdio");
1864 if (fsm->wfd == NULL || Ferror(fsm->wfd)) {
1865 if (fsm->wfd) (void) fsmStage(fsm, FSM_WCLOSE);
1867 rc = CPIOERR_OPEN_FAILED;
1869 if (_fsm_debug && (stage & FSM_SYSCALL))
1870 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"w\") wfd %p wrbuf %p\n", cur,
1871 fsm->path, fsm->wfd, fsm->wrbuf);
1874 fsm->wrnb = Fwrite(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->rdnb, fsm->wfd);
1875 if (_fsm_debug && (stage & FSM_SYSCALL))
1876 rpmMessage(RPMMESS_DEBUG, " %8s (wrbuf, %d, wfd)\twrnb %d\n",
1877 cur, (int)fsm->rdnb, (int)fsm->wrnb);
1878 if (fsm->rdnb != fsm->wrnb) fprintf(stderr, "*** short write: had %d, got %d\n", (int)fsm->rdnb, (int)fsm->wrnb);
1880 if (Ferror(fsm->wfd))
1881 rc = CPIOERR_WRITE_FAILED;
1886 if (_fsm_debug && (stage & FSM_SYSCALL))
1887 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->wfd);
1888 (void) Fclose(fsm->wfd);
1898 if (!(stage & FSM_INTERNAL)) {
1899 fsm->rc = (rc == CPIOERR_HDR_TRAILER ? 0 : rc);
1904 /*@obserever@*/ const char *const fileActionString(fileAction a)
1907 case FA_UNKNOWN: return "unknown";
1908 case FA_CREATE: return "create";
1909 case FA_COPYOUT: return "copyout";
1910 case FA_COPYIN: return "copyin";
1911 case FA_BACKUP: return "backup";
1912 case FA_SAVE: return "save";
1913 case FA_SKIP: return "skip";
1914 case FA_ALTNAME: return "altname";
1915 case FA_ERASE: return "erase";
1916 case FA_SKIPNSTATE: return "skipnstate";
1917 case FA_SKIPNETSHARED: return "skipnetshared";
1918 case FA_SKIPMULTILIB: return "skipmultilib";
1919 default: return "???";
1924 /*@observer@*/ const char *const fileStageString(fileStage a) {
1926 case FSM_UNKNOWN: return "unknown";
1928 case FSM_PKGINSTALL:return "pkginstall";
1929 case FSM_PKGERASE: return "pkgerase";
1930 case FSM_PKGBUILD: return "pkgbuild";
1931 case FSM_PKGCOMMIT: return "pkgcommit";
1932 case FSM_PKGUNDO: return "pkgundo";
1934 case FSM_CREATE: return "create";
1935 case FSM_INIT: return "init";
1936 case FSM_MAP: return "map";
1937 case FSM_MKDIRS: return "mkdirs";
1938 case FSM_RMDIRS: return "rmdirs";
1939 case FSM_PRE: return "pre";
1940 case FSM_PROCESS: return "process";
1941 case FSM_POST: return "post";
1942 case FSM_MKLINKS: return "mklinks";
1943 case FSM_NOTIFY: return "notify";
1944 case FSM_UNDO: return "undo";
1945 case FSM_FINI: return "fini";
1946 case FSM_COMMIT: return "commit";
1947 case FSM_DESTROY: return "destroy";
1948 case FSM_VERIFY: return "verify";
1950 case FSM_UNLINK: return "Unlink";
1951 case FSM_RENAME: return "Rename";
1952 case FSM_MKDIR: return "Mkdir";
1953 case FSM_RMDIR: return "rmdir";
1954 case FSM_CHOWN: return "chown";
1955 case FSM_LCHOWN: return "lchown";
1956 case FSM_CHMOD: return "chmod";
1957 case FSM_UTIME: return "utime";
1958 case FSM_SYMLINK: return "symlink";
1959 case FSM_LINK: return "Link";
1960 case FSM_MKFIFO: return "mkfifo";
1961 case FSM_MKNOD: return "mknod";
1962 case FSM_LSTAT: return "Lstat";
1963 case FSM_STAT: return "Stat";
1964 case FSM_READLINK: return "Readlink";
1965 case FSM_CHROOT: return "chroot";
1967 case FSM_NEXT: return "next";
1968 case FSM_EAT: return "eat";
1969 case FSM_POS: return "pos";
1970 case FSM_PAD: return "pad";
1971 case FSM_TRAILER: return "trailer";
1972 case FSM_HREAD: return "hread";
1973 case FSM_HWRITE: return "hwrite";
1974 case FSM_DREAD: return "Fread";
1975 case FSM_DWRITE: return "Fwrite";
1977 case FSM_ROPEN: return "Fopen";
1978 case FSM_READ: return "Fread";
1979 case FSM_RCLOSE: return "Fclose";
1980 case FSM_WOPEN: return "Fopen";
1981 case FSM_WRITE: return "Fwrite";
1982 case FSM_WCLOSE: return "Fclose";
1984 default: return "???";