Untangle lib/fsm.c and lib/cpio.c
[platform/upstream/rpm.git] / lib / fsm.h
1 #ifndef H_FSM
2 #define H_FSM
3
4 /** \ingroup payload
5  * \file lib/fsm.h
6  * File state machine to handle a payload within an rpm package.
7  */
8
9 #include <rpm/rpmfi.h>
10 #include "cpio.h"
11
12 extern int _fsm_debug;
13
14 /**
15  */
16 #define FSM_VERBOSE     0x8000
17 #define FSM_INTERNAL    0x4000
18 #define FSM_SYSCALL     0x2000
19 #define FSM_DEAD        0x1000
20
21 #define _fv(_a)         ((_a) | FSM_VERBOSE)
22 #define _fi(_a)         ((_a) | FSM_INTERNAL)
23 #define _fs(_a)         ((_a) | (FSM_INTERNAL | FSM_SYSCALL))
24 #define _fd(_a)         ((_a) | (FSM_INTERNAL | FSM_DEAD))
25
26 typedef enum fileStage_e {
27     FSM_UNKNOWN =   0,
28     FSM_INIT    =  _fd(1),
29     FSM_PRE     =  _fd(2),
30     FSM_PROCESS =  _fv(3),
31     FSM_POST    =  _fd(4),
32     FSM_UNDO    =  5,
33     FSM_FINI    =  6,
34
35     FSM_PKGINSTALL      = _fd(7),
36     FSM_PKGERASE        = _fd(8),
37     FSM_PKGBUILD        = _fd(9),
38     FSM_PKGUNDO         = _fd(11),
39
40     FSM_CREATE  =  _fd(17),
41     FSM_MAP     =  _fd(18),
42     FSM_MKDIRS  =  _fi(19),
43     FSM_RMDIRS  =  _fi(20),
44     FSM_MKLINKS =  _fi(21),
45     FSM_NOTIFY  =  _fd(22),
46     FSM_DESTROY =  _fd(23),
47     FSM_VERIFY  =  _fd(24),
48     FSM_COMMIT  =  _fd(25),
49
50     FSM_UNLINK  =  _fs(33),
51     FSM_RENAME  =  _fs(34),
52     FSM_MKDIR   =  _fs(35),
53     FSM_RMDIR   =  _fs(36),
54     FSM_LSETFCON=  _fs(39),
55     FSM_CHOWN   =  _fs(40),
56     FSM_LCHOWN  =  _fs(41),
57     FSM_CHMOD   =  _fs(42),
58     FSM_UTIME   =  _fs(43),
59     FSM_SYMLINK =  _fs(44),
60     FSM_LINK    =  _fs(45),
61     FSM_MKFIFO  =  _fs(46),
62     FSM_MKNOD   =  _fs(47),
63     FSM_LSTAT   =  _fs(48),
64     FSM_STAT    =  _fs(49),
65     FSM_READLINK=  _fs(50),
66     FSM_SETCAP  =  _fs(52),
67
68     FSM_NEXT    =  _fd(65),
69     FSM_EAT     =  _fd(66),
70     FSM_POS     =  _fd(67),
71     FSM_PAD     =  _fd(68),
72     FSM_TRAILER =  _fd(69),
73     FSM_HREAD   =  _fd(70),
74     FSM_HWRITE  =  _fd(71),
75     FSM_DREAD   =  _fs(72),
76     FSM_DWRITE  =  _fs(73),
77 } fileStage;
78 #undef  _fv
79 #undef  _fi
80 #undef  _fs
81 #undef  _fd
82
83 /** \ingroup payload
84  */
85 enum cpioMapFlags_e {
86     CPIO_MAP_PATH       = (1 << 0),
87     CPIO_MAP_MODE       = (1 << 1),
88     CPIO_MAP_UID        = (1 << 2),
89     CPIO_MAP_GID        = (1 << 3),
90     CPIO_FOLLOW_SYMLINKS= (1 << 4), /*!< only for building. */
91     CPIO_MAP_ABSOLUTE   = (1 << 5),
92     CPIO_MAP_ADDDOT     = (1 << 6),
93     CPIO_MAP_TYPE       = (1 << 8),  /*!< only for building. */
94     CPIO_SBIT_CHECK     = (1 << 9)
95 };
96 typedef rpmFlags cpioMapFlags;
97
98 typedef struct fsmIterator_s * FSMI_t;
99 typedef struct fsm_s * FSM_t;
100 typedef struct rpmpsm_s * rpmpsm;
101
102 typedef struct hardLink_s * hardLink_t;
103
104 /** \ingroup payload
105  * File name and stat information.
106  */
107 struct fsm_s {
108     char * path;                /*!< Current file name. */
109     FD_t cfd;                   /*!< Payload file handle. */
110     rpmcpio_t archive;          /*!< cpio archive */
111     char * buf;                 /*!<  read: Buffer. */
112     size_t bufsize;             /*!<  read: Buffer allocated size. */
113     FSMI_t iter;                /*!< File iterator. */
114     int ix;                     /*!< Current file iterator index. */
115     hardLink_t links;           /*!< Pending hard linked file(s). */
116     hardLink_t li;              /*!< Current hard linked file(s). */
117     rpm_loff_t * archiveSize;   /*!< Pointer to archive size. */
118     char ** failedFile;         /*!< First file name that failed. */
119     const char * osuffix;       /*!< Old, preserved, file suffix. */
120     const char * nsuffix;       /*!< New, created, file suffix. */
121     char * suffix;              /*!< Current file suffix. */
122     int postpone;               /*!< Skip remaining stages? */
123     int diskchecked;            /*!< Has stat(2) been performed? */
124     int exists;                 /*!< Does current file exist on disk? */
125     int rc;                     /*!< External file stage return code. */
126     cpioMapFlags mapFlags;      /*!< Bit(s) to control mapping. */
127     const char * dirName;       /*!< File directory name. */
128     const char * baseName;      /*!< File base name. */
129     struct selabel_handle *sehandle;    /*!< SELinux label handle (if any). */
130     
131     unsigned fflags;            /*!< File flags. */
132     rpmFileAction action;       /*!< File disposition. */
133     fileStage goal;             /*!< Package state machine goal. */
134     fileStage stage;            /*!< External file stage. */
135     fileStage nstage;           /*!< Next file stage. */
136     struct stat sb;             /*!< Current file stat(2) info. */
137     struct stat osb;            /*!< Original file stat(2) info. */
138
139     rpmpsm psm;                 /*!< "parent" package state machine */
140 };
141
142 #ifdef __cplusplus
143 extern "C" {
144 #endif
145
146 /**
147  * Execute a file state machine goal
148  * @param goal
149  * @param ts            transaction set
150  * @param fi            transaction element file info
151  * @param cfd
152  * @param psm           owner psm (or NULL)
153  * @retval archiveSize  pointer to archive size
154  * @retval failedFile   pointer to first file name that failed (malloced)
155  * @return              0 on success
156  */
157 int rpmfsmRun(fileStage goal, rpmts ts, rpmte te, rpmfi fi, FD_t cfd,
158               rpmpsm psm, rpm_loff_t * archiveSize, char ** failedFile);
159
160 /**
161  * File state machine driver.
162  * @param fsm           file state machine
163  * @param nstage                next stage
164  * @return              0 on success
165  */
166 RPM_GNUC_INTERNAL
167 int fsmNext(FSM_t fsm, fileStage nstage);
168
169 RPM_GNUC_INTERNAL
170 void rpmpsmNotify(rpmpsm psm, int what, rpm_loff_t amount);
171 #ifdef __cplusplus
172 }
173 #endif
174
175 #endif  /* H_FSM */