# lib/fsm.c File Reference

File state machine to handle a payload from a package. More...

`#include "system.h"`
`#include "psm.h"`
`#include "rpmerr.h"`
`#include "debug.h"`

Go to the source code of this file.

## Data Structures

struct | dnli_s
| Directory name iterator. More...

## Defines

#define | alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
#define | SUFFIX_RPMORIG ".rpmorig"
#define | SUFFIX_RPMSAVE ".rpmsave"
#define | SUFFIX_RPMNEW ".rpmnew"
#define | _fafilter(_a)
#define | _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)

## Typedefs

typedef dnli_s * | DNLI_t
| Directory name iterator.

## Functions

rpmTransactionSet | fsmGetTs (const FSM_t fsm)
| Retrieve transaction set from file state machine iterator.
TFI_t | fsmGetFi (const FSM_t fsm)
| Retrieve transaction element file info from file state machine iterator.
static const char * | fsmFsPath (const FSM_t fsm, const struct stat *st, const char *subdir, const char *suffix)
| Build path to file from file info, ornamented with subdir and suffix.
static void * | mapFreeIterator (const void *p)
| Destroy file info iterator.
static void * | mapInitIterator (const void *a, const void *b)
| Create file info iterator.
static int | mapNextIterator (void *a)
| Return next index into file info.
static int | cpioStrCmp (const void *a, const void *b)
static int | mapFind (void *a, const char *fsmPath)
| Locate archive path in file info.
static void * | dnlFreeIterator (const void *a)
| Destroy directory name iterator.
static int | dnlCount (const DNLI_t dnli)
static int | dnlIndex (const DNLI_t dnli)
static void * | dnlInitIterator (const FSM_t fsm, int reverse)
| Create directory name iterator.
static const char * | dnlNextIterator (DNLI_t dnli)
| Return next directory name (from file info).
static int | saveHardLink (FSM_t fsm)
| Save hard link in chain.
static void * | freeHardLink (struct hardLink_s *li)
| Destroy set of hard links.
FSM_t | newFSM (void)
| Create file state machine instance.
FSM_t | freeFSM (FSM_t fsm)
| Destroy file state machine instance.
int | fsmSetup (FSM_t fsm, fileStage goal, const rpmTransactionSet ts, const TFI_t fi, FD_t cfd, unsigned int *archiveSize, const char **failedFile)
| Load external data into file state machine.
int | fsmTeardown (FSM_t fsm)
| Clean file state machine.
int | fsmMapPath (FSM_t fsm)
| Map next file path and action.
int | fsmMapAttrs (FSM_t fsm)
| Map file stat(2) info.
static int | expandRegular (FSM_t fsm)
| Create file from payload stream.
static int | writeFile (FSM_t fsm, int writeData)
| Write next item to payload stream.
static int | writeLinkedFile (FSM_t fsm)
| Write set of linked files to payload stream.
static int | fsmMakeLinks (FSM_t fsm)
| Create pending hard links to existing file.
static int | fsmCommitLinks (FSM_t fsm)
| Commit hard linked file set atomically.
static int | fsmRmdirs (FSM_t fsm)
| Remove (if created) directories not explicitly included in package.
static int | fsmMkdirs (FSM_t fsm)
| Create (if necessary) directories not explicitly included in package.
int | fsmStage (FSM_t fsm, fileStage stage)
| File state machine driver.
const char *const | fileActionString (fileAction a)
| Return formatted string representation of file disposition.
const char *const | fileStageString (fileStage a)
| Return formatted string representation of file stages.

## Variables

int | _fsm_debug = 0
int | strict_erasures = 0

## Detailed Description

File state machine to handle a payload from a package.

Definition in file fsm.c.

## Define Documentation

#define _fafilter | ( | _a |  | ) |

Value:

    (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
            ? fileActionString(_a) : "")

Referenced by fsmStage().

#define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)

Referenced by fsmStage().

#define alloca_strdup | ( | _s |  | ) | strcpy(alloca(strlen(_s)+1), (_s))

Definition at line 18 of file fsm.c.

Referenced by ftpNLST(), readPackageHeaders(), relocateFileList(), and writeFile().

#define SUFFIX_RPMNEW ".rpmnew"

Definition at line 44 of file fsm.c.

Referenced by fsmMapPath().

#define SUFFIX_RPMORIG ".rpmorig"

Definition at line 42 of file fsm.c.

Referenced by fsmMapPath().

#define SUFFIX_RPMSAVE ".rpmsave"

Definition at line 43 of file fsm.c.

Referenced by fsmMapPath().

## Function Documentation

const char* const fileActionString | ( | fileAction | a | ) |

Return formatted string representation of file disposition.

Parameters:

  | a | file dispostion

Returns:
  formatted string

Definition at line 2123 of file fsm.c.

References FA_ALTNAME, FA_BACKUP, FA_COPYIN, FA_COPYOUT, FA_CREATE, FA_ERASE, FA_SAVE, FA_SKIP, FA_SKIPMULTILIB, FA_SKIPNETSHARED, FA_SKIPNSTATE, and FA_UNKNOWN.

const char* const fileStageString | ( | fileStage | a | ) |

Return formatted string representation of file stages.

Parameters:

  | a | file stage

Returns:
  formatted string

Definition at line 2143 of file fsm.c.

References FSM_CHMOD, FSM_CHOWN, FSM_CHROOT, FSM_COMMIT, FSM_CREATE, FSM_DESTROY, FSM_DREAD, FSM_DWRITE, FSM_EAT, FSM_FINI, FSM_HREAD, FSM_HWRITE, FSM_INIT, FSM_LCHOWN, FSM_LINK, FSM_LSTAT, FSM_MAP, FSM_MKDIR, FSM_MKDIRS, FSM_MKFIFO, FSM_MKLINKS, FSM_MKNOD, FSM_NEXT, FSM_NOTIFY, FSM_PAD, FSM_PKGBUILD, FSM_PKGCOMMIT, FSM_PKGERASE, FSM_PKGINSTALL, FSM_PKGUNDO, FSM_POS, FSM_POST, FSM_PRE, FSM_PROCESS, FSM_RCLOSE, FSM_READ, FSM_READLINK, FSM_RENAME, FSM_RMDIR, FSM_RMDIRS, FSM_ROPEN, FSM_STAT, FSM_SYMLINK, FSM_TRAILER, FSM_UNDO, FSM_UNKNOWN, FSM_UNLINK, FSM_UTIME, FSM_VERIFY, FSM_WCLOSE, FSM_WOPEN, and FSM_WRITE.

Referenced by fsmStage().

FSM_t freeFSM | ( | FSM_t | fsm | ) |

Destroy file state machine instance.

Parameters:

  | fsm | file state machine data

Returns:
  always NULL

Definition at line 438 of file fsm.c.

References _free(), fsm_s::dnlx, freeHardLink(), fsm_s::iter, fsm_s::ldn, fsm_s::li, fsm_s::links, mapFreeIterator(), hardLink_s::next, and fsm_s::path.

Referenced by freeFi().

TFI_t fsmGetFi | ( | const FSM_t | fsm | ) |

Retrieve transaction element file info from file state machine iterator.

Parameters:

  | fsm | file state machine data

Returns:
  transaction element file info

Definition at line 34 of file fsm.c.

References fsmIterator_s::fi, and fsm_s::iter.

Referenced by dnlInitIterator(), fsmMapAttrs(), fsmMapPath(), fsmMkdirs(), fsmStage(), saveHardLink(), and writeFile().

rpmTransactionSet fsmGetTs | ( | const FSM_t | fsm | ) |

Retrieve transaction set from file state machine iterator.

Parameters:

  | fsm | file state machine data

Returns:
  transaction set

Definition at line 27 of file fsm.c.

References fsm_s::iter, and fsmIterator_s::ts.

Referenced by fsmMapAttrs(), fsmStage(), and writeFile().

int fsmMapAttrs | ( | FSM_t | fsm | ) |

Map file stat(2) info.

Parameters:

  | fsm | file state machine data

Definition at line 616 of file fsm.c.

References CPIO_MAP_GID, CPIO_MAP_MODE, CPIO_MAP_TYPE, CPIO_MAP_UID, transactionFileInfo_s::dperms, transactionFileInfo_s::fgids, transactionFileInfo_s::fmd5s, fsm_s::fmd5sum, transactionFileInfo_s::fmodes, transactionFileInfo_s::fmtimes, transactionFileInfo_s::fperms, transactionFileInfo_s::frdevs, fsmGetFi(), fsmGetTs(), transactionFileInfo_s::fuids, transactionFileInfo_s::gid, fsm_s::ix, fsm_s::mapFlags, RPMTRANS_FLAG_NOMD5, fsm_s::sb, rpmTransactionSet_s::transFlags, and transactionFileInfo_s::uid.

Referenced by fsmStage().

int fsmMapPath | ( | FSM_t | fsm | ) |

Map next file path and action.

Parameters:

  | fsm | file state machine data

Definition at line 521 of file fsm.c.

References _free(), fsm_s::action, transactionFileInfo_s::action, transactionFileInfo_s::actions, fsm_s::astriplen, transactionFileInfo_s::astriplen, fsm_s::baseName, transactionFileInfo_s::bnl, CPIO_MAP_PATH, transactionFileInfo_s::dil, fsm_s::dirName, transactionFileInfo_s::dnl, FA_ALTNAME, FA_BACKUP, FA_COPYIN, FA_COPYOUT, FA_CREATE, FA_ERASE, FA_SAVE, FA_SKIP, FA_SKIPMULTILIB, FA_SKIPNETSHARED, FA_SKIPNSTATE, FA_UNKNOWN, fsm_s::fflags, transactionFileInfo_s::fflags, transactionFileInfo_s::flags, transactionFileInfo_s::fmapflags, fsmFsPath(), fsmGetFi(), transactionFileInfo_s::fstates, fsm_s::ix, fsm_s::mapFlags, transactionFileInfo_s::mapflags, fsm_s::nsuffix, fsm_s::osuffix, fsm_s::path, RPMFILE_GHOST, RPMFILE_STATE_NETSHARED, RPMFILE_STATE_NOTINSTALLED, fsm_s::sb, fsm_s::subdir, fsm_s::suffix, SUFFIX_RPMNEW, SUFFIX_RPMORIG, SUFFIX_RPMSAVE, and transactionFileInfo_s::type.

Referenced by fsmStage().

static int fsmMkdirs | ( | FSM_t | fsm | ) | `[static]`

Create (if necessary) directories not explicitly included in package.

Parameters:

  | fsm | file state machine data

Returns:
  0 on success

Definition at line 1072 of file fsm.c.

References _, CPIOERR_LSTAT_FAILED, dnlCount(), dnlIndex(), dnlInitIterator(), dnlNextIterator(), fsm_s::dnlx, transactionFileInfo_s::dperms, FSM_LSTAT, FSM_MKDIR, fsmGetFi(), fsmStage(), fsm_s::ldn, fsm_s::ldnalloc, fsm_s::ldnlen, fsm_s::osb, fsm_s::path, fsm_s::rdbuf, RPMMESS_DEBUG, rpmMessage, fsm_s::sb, stpcpy(), xcalloc(), and xrealloc().

Referenced by fsmStage().

static int fsmRmdirs | ( | FSM_t | fsm | ) | `[static]`

Remove (if created) directories not explicitly included in package.

Parameters:

  | fsm | file state machine data

Returns:
  0 on success

Definition at line 1021 of file fsm.c.

References dnlCount(), dnlFreeIterator(), dnlIndex(), dnlInitIterator(), dnlNextIterator(), fsm_s::dnlx, FSM_RMDIR, fsmStage(), fsm_s::ldn, fsm_s::path, fsm_s::rdbuf, and stpcpy().

Referenced by fsmStage().

int fsmSetup | ( | FSM_t | fsm,
|  | fileStage | goal,
|  | const rpmTransactionSet | ts,
|  | const TFI_t | fi,
|  | FD_t | cfd,
|  | unsigned int * | archiveSize,
|  | const char ** | failedFile |
| ) |  |  |

Load external data into file state machine.

Parameters:

  | fsm | file state machine data
  | goal |
  | ts | transaction set
  | fi | transaction element file info
  | archiveSize | pointer to archive size
  | failedFile | pointer to first file name that failed.

Returns:
  0 on success

Definition at line 454 of file fsm.c.

References transactionFileInfo_s::ap, transactionFileInfo_s::archiveSize, fsm_s::archiveSize, fsm_s::cfd, fsm_s::failedFile, fdGetCpioPos(), fdLink, fdSetCpioPos(), FSM_CREATE, FSM_PKGINSTALL, fsmStage(), fsm_s::goal, transactionFileInfo_s::h, rpmTransactionSet_s::id, fsm_s::iter, availablePackage::key, mapInitIterator(), rpmTransactionSet_s::notify, rpmTransactionSet_s::notifyData, fsm_s::rc, RPMCALLBACK_INST_START, and fsm_s::sufbuf.

Referenced by cpio_doio(), and psmStage().

int fsmStage | ( | FSM_t | fsm,
|  | fileStage | stage |
| ) |  |  |

File state machine driver.

Parameters:

  | fsm | file state machine data
  | stage | next stage

Returns:
  0 on success

Definition at line 1205 of file fsm.c.

References _, _fafilter, _free(), _fsm_debug, _tsmask, fsm_s::action, transactionFileInfo_s::ap, transactionFileInfo_s::archiveSize, fsm_s::astriplen, fsm_s::cfd, fsm_s::commit, CPIO_ALL_HARDLINKS, CPIO_FOLLOW_SYMLINKS, CPIOERR_HDR_SIZE, CPIOERR_HDR_TRAILER, CPIOERR_LSTAT_FAILED, CPIOERR_MISSING_HARDLINK, CPIOERR_READ_FAILED, CPIOERR_UNKNOWN_FILETYPE, CPIOERR_UNMAPPED_FILE, hardLink_s::createdPath, fsm_s::diskchecked, fsm_s::dnlx, errno, fsm_s::exists, expandRegular(), FA_ERASE, FA_UNKNOWN, fsm_s::failedFile, fdGetCpioPos(), fsm_s::fflags, fileStageString(), hardLink_s::filex, fiTypeString(), transactionFileInfo_s::fmodes, transactionFileInfo_s::fmtimes, freeHardLink(), FSM_CHMOD, FSM_CHOWN, FSM_COMMIT, FSM_CREATE, FSM_DEAD, FSM_DESTROY, FSM_DREAD, FSM_EAT, FSM_FINI, FSM_INIT, FSM_INTERNAL, FSM_LCHOWN, FSM_LSTAT, FSM_MAP, FSM_MKDIR, FSM_MKDIRS, FSM_MKFIFO, FSM_MKLINKS, FSM_MKNOD, FSM_NEXT, FSM_NOTIFY, FSM_PKGBUILD, FSM_PKGCOMMIT, FSM_PKGERASE, FSM_PKGINSTALL, FSM_POST, FSM_PRE, FSM_PROCESS, FSM_RENAME, FSM_RMDIR, FSM_RMDIRS, FSM_STAT, FSM_SYMLINK, FSM_SYSCALL, FSM_TRAILER, FSM_UNDO, FSM_UNKNOWN, FSM_UNLINK, FSM_UTIME, FSM_VERBOSE, FSM_VERIFY, fsmCommitLinks(), fsmFsPath(), fsmGetFi(), fsmGetTs(), fsmMakeLinks(), fsmMapAttrs(), fsmMapPath(), fsmMkdirs(), fsmRmdirs(), fsmStage(), fsm_s::goal, transactionFileInfo_s::h, fsm_s::iter, fsm_s::ix, availablePackage::key, fsm_s::ldn, fsm_s::ldnalloc, fsm_s::ldnlen, fsm_s::li, hardLink_s::linkIndex, fsm_s::links, hardLink_s::linksLeft, mapFind(), fsm_s::mapFlags, mapNextIterator(), fsm_s::mkdirsdone, hardLink_s::next, hardLink_s::nlink, rpmTransactionSet_s::notify, rpmTransactionSet_s::notifyData, fsm_s::nsuffix, fsm_s::opath, fsm_s::osb, fsm_s::osuffix, fsm_s::path, fsm_s::postpone, fsm_s::rc, fsm_s::rdb, fsm_s::rdbuf, fsm_s::rdnb, fsm_s::rdsize, RPMCALLBACK_INST_PROGRESS, RPMDEBUG_RMDIR, RPMDEBUG_UNLINK, RPMERR_RMDIR, RPMERR_UNLINK, rpmError, RPMFILE_MISSINGOK, RPMMESS_DEBUG, RPMMESS_WARNING, rpmMessage, S_ISLNK, S_ISSOCK, saveHardLink(), fsm_s::sb, hardLink_s::sb, fsm_s::stage, strerror(), strict_erasures, fsm_s::subdir, fsm_s::sufbuf, fsm_s::suffix, rpmTransactionSet_s::transFlags, fsm_s::wrb, fsm_s::wrbuf, writeFile(), writeLinkedFile(), fsm_s::wrlen, fsm_s::wrsize, XFA_SKIPPING, xmalloc(), and xstrdup().

Referenced by cpioHeaderRead(), cpioHeaderWrite(), cpioTrailerWrite(), expandRegular(), fsmCommitLinks(), fsmMakeLinks(), fsmMkdirs(), fsmRmdirs(), fsmSetup(), fsmStage(), fsmTeardown(), saveHardLink(), writeFile(), and writeLinkedFile().

int fsmTeardown | ( | FSM_t | fsm | ) |

Clean file state machine.

Parameters:

  | fsm | file state machine data

Returns:
  0 on success

Definition at line 504 of file fsm.c.

References fsm_s::cfd, fsm_s::failedFile, fdFree, FSM_DESTROY, fsmStage(), fsm_s::iter, mapFreeIterator(), and fsm_s::rc.

Referenced by cpio_doio(), and psmStage().

FSM_t newFSM | ( | void |  | ) |

Create file state machine instance.

Returns:
  file state machine data

Definition at line 432 of file fsm.c.

References xcalloc().

Referenced by loadFi().

## Variable Documentation

int _fsm_debug = 0

Definition at line 20 of file fsm.c.

int strict_erasures = 0

Definition at line 24 of file fsm.c.

Referenced by fsmStage().