Sanity.
[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 "cpio.h"
10
11 /*@-exportlocal@*/
12 /*@unchecked@*/
13 extern int _fsm_debug;
14 /*@=exportlocal@*/
15
16 /**
17  */
18 #define FSM_VERBOSE     0x8000
19 #define FSM_INTERNAL    0x4000
20 #define FSM_SYSCALL     0x2000
21 #define FSM_DEAD        0x1000
22
23 #define _fv(_a)         ((_a) | FSM_VERBOSE)
24 #define _fi(_a)         ((_a) | FSM_INTERNAL)
25 #define _fs(_a)         ((_a) | (FSM_INTERNAL | FSM_SYSCALL))
26 #define _fd(_a)         ((_a) | (FSM_INTERNAL | FSM_DEAD))
27
28 typedef enum fileStage_e {
29     FSM_UNKNOWN =   0,
30     FSM_INIT    =  _fd(1),
31     FSM_PRE     =  _fd(2),
32     FSM_PROCESS =  _fv(3),
33     FSM_POST    =  _fd(4),
34     FSM_UNDO    =  5,
35     FSM_FINI    =  6,
36
37     FSM_PKGINSTALL      = _fd(7),
38     FSM_PKGERASE        = _fd(8),
39     FSM_PKGBUILD        = _fd(9),
40     FSM_PKGCOMMIT       = _fd(10),
41     FSM_PKGUNDO         = _fd(11),
42
43     FSM_CREATE  =  _fd(17),
44     FSM_MAP     =  _fd(18),
45     FSM_MKDIRS  =  _fi(19),
46     FSM_RMDIRS  =  _fi(20),
47     FSM_MKLINKS =  _fi(21),
48     FSM_NOTIFY  =  _fd(22),
49     FSM_DESTROY =  _fd(23),
50     FSM_VERIFY  =  _fd(24),
51     FSM_COMMIT  =  _fd(25),
52
53     FSM_UNLINK  =  _fs(33),
54     FSM_RENAME  =  _fs(34),
55     FSM_MKDIR   =  _fs(35),
56     FSM_RMDIR   =  _fs(36),
57     FSM_CHOWN   =  _fs(37),
58     FSM_LCHOWN  =  _fs(38),
59     FSM_CHMOD   =  _fs(39),
60     FSM_UTIME   =  _fs(40),
61     FSM_SYMLINK =  _fs(41),
62     FSM_LINK    =  _fs(42),
63     FSM_MKFIFO  =  _fs(43),
64     FSM_MKNOD   =  _fs(44),
65     FSM_LSTAT   =  _fs(45),
66     FSM_STAT    =  _fs(46),
67     FSM_READLINK=  _fs(47),
68     FSM_CHROOT  =  _fs(48),
69
70     FSM_NEXT    =  _fd(65),
71     FSM_EAT     =  _fd(66),
72     FSM_POS     =  _fd(67),
73     FSM_PAD     =  _fd(68),
74     FSM_TRAILER =  _fd(69),
75     FSM_HREAD   =  _fd(70),
76     FSM_HWRITE  =  _fd(71),
77     FSM_DREAD   =  _fs(72),
78     FSM_DWRITE  =  _fs(73),
79
80     FSM_ROPEN   =  _fs(129),
81     FSM_READ    =  _fs(130),
82     FSM_RCLOSE  =  _fs(131),
83     FSM_WOPEN   =  _fs(132),
84     FSM_WRITE   =  _fs(133),
85     FSM_WCLOSE  =  _fs(134)
86 } fileStage;
87 #undef  _fv
88 #undef  _fi
89 #undef  _fs
90 #undef  _fd
91
92 /** \ingroup payload
93  * Keeps track of the set of all hard links to a file in an archive.
94  */
95 struct hardLink_s {
96 /*@owned@*/ struct hardLink_s * next;
97 /*@owned@*/ const char ** nsuffix;
98 /*@owned@*/ int * filex;
99     struct stat sb;
100     int nlink;
101     int linksLeft;
102     int linkIndex;
103     int createdPath;
104 };
105
106 /** \ingroup payload
107  * Iterator across package file info, forward on install, backward on erase.
108  */
109 struct fsmIterator_s {
110     rpmts ts;                   /*!< transaction set. */
111     rpmfi fi;                   /*!< transaction element file info. */
112     int reverse;                /*!< reversed traversal? */
113     int isave;                  /*!< last returned iterator index. */
114     int i;                      /*!< iterator index. */
115 };
116
117 /** \ingroup payload
118  * File name and stat information.
119  */
120 struct fsm_s {
121 /*@owned@*/
122     const char * path;          /*!< Current file name. */
123 /*@owned@*/
124     const char * opath;         /*!< Original file name. */
125     FD_t cfd;                   /*!< Payload file handle. */
126     FD_t rfd;                   /*!<  read: File handle. */
127 /*@dependent@*/
128     char * rdbuf;               /*!<  read: Buffer. */
129 /*@owned@*/
130     char * rdb;                 /*!<  read: Buffer allocated. */
131     size_t rdsize;              /*!<  read: Buffer allocated size. */
132     size_t rdlen;               /*!<  read: Number of bytes requested.*/
133     size_t rdnb;                /*!<  read: Number of bytes returned. */
134     FD_t wfd;                   /*!< write: File handle. */
135 /*@dependent@*/
136     char * wrbuf;               /*!< write: Buffer. */
137 /*@owned@*/
138     char * wrb;                 /*!< write: Buffer allocated. */
139     size_t wrsize;              /*!< write: Buffer allocated size. */
140     size_t wrlen;               /*!< write: Number of bytes requested.*/
141     size_t wrnb;                /*!< write: Number of bytes returned. */
142 /*@only@*/ /*@null@*/
143     FSMI_t iter;                /*!< File iterator. */
144     int ix;                     /*!< Current file iterator index. */
145 /*@only@*/
146     struct hardLink_s * links;  /*!< Pending hard linked file(s). */
147 /*@only@*/
148     struct hardLink_s * li;     /*!< Current hard linked file(s). */
149 /*@kept@*/ /*@null@*/
150     unsigned int * archiveSize; /*!< Pointer to archive size. */
151 /*@kept@*/ /*@null@*/
152     const char ** failedFile;   /*!< First file name that failed. */
153 /*@shared@*/
154     const char * subdir;        /*!< Current file sub-directory. */
155 /*@unused@*/
156     char subbuf[64];    /* XXX eliminate */
157 /*@observer@*/
158     const char * osuffix;       /*!< Old, preserved, file suffix. */
159 /*@observer@*/
160     const char * nsuffix;       /*!< New, created, file suffix. */
161 /*@shared@*/
162     const char * suffix;        /*!< Current file suffix. */
163     char sufbuf[64];    /* XXX eliminate */
164 /*@only@*/ /*@null@*/
165     short * dnlx;               /*!< Last dirpath verified indexes. */
166 /*@only@*/ /*@null@*/
167     char * ldn;                 /*!< Last dirpath verified. */
168     int ldnlen;                 /*!< Last dirpath current length. */
169     int ldnalloc;               /*!< Last dirpath allocated length. */
170     int postpone;               /*!< Skip remaining stages? */
171     int diskchecked;            /*!< Has stat(2) been performed? */
172     int exists;                 /*!< Does current file exist on disk? */
173     int mkdirsdone;             /*!< Have "orphan" dirs been created? */
174     int astriplen;              /*!< Length of buildroot prefix. */
175     int rc;                     /*!< External file stage return code. */
176     int commit;                 /*!< Commit synchronously? */
177     cpioMapFlags mapFlags;      /*!< Bit(s) to control mapping. */
178 /*@shared@*/
179     const char * dirName;       /*!< File directory name. */
180 /*@shared@*/
181     const char * baseName;      /*!< File base name. */
182 /*@shared@*/
183     const char * fmd5sum;       /*!< Hex MD5 sum (NULL disables). */
184 /*@shared@*/
185     const char * md5sum;        /*!< Binary MD5 sum (NULL disables). */
186     
187     unsigned fflags;            /*!< File flags. */
188     fileAction action;          /*!< File disposition. */
189     fileStage goal;             /*!< Package state machine goal. */
190     fileStage stage;            /*!< External file stage. */
191     struct stat sb;             /*!< Current file stat(2) info. */
192     struct stat osb;            /*!< Original file stat(2) info. */
193 };
194
195 #ifdef __cplusplus
196 extern "C" {
197 #endif
198
199 /*@-exportlocal@*/
200 /**
201  * Return formatted string representation of file stages.
202  * @param a             file stage
203  * @return              formatted string
204  */
205 /*@observer@*/ const char *const fileStageString(fileStage a)   /*@*/;
206
207 /**
208  * Return formatted string representation of file disposition.
209  * @param a             file dispostion
210  * @return              formatted string
211  */
212 /*@observer@*/ const char *const fileActionString(fileAction a) /*@*/;
213 /*@=exportlocal@*/
214
215 /**
216  * Create file state machine instance.
217  * @return              file state machine data
218  */
219 /*@only@*/ FSM_t newFSM(void)
220         /*@*/;
221
222 /**
223  * Destroy file state machine instance.
224  * @param fsm           file state machine data
225  * @return              always NULL
226  */
227 /*@null@*/ FSM_t freeFSM(/*@only@*/ /*@null@*/ FSM_t fsm)
228         /*@modifies fsm @*/;
229
230 /**
231  * Load external data into file state machine.
232  * @param fsm           file state machine data
233  * @param goal
234  * @param ts            transaction set
235  * @param fi            transaction element file info
236  * @param cfd
237  * @retval archiveSize  pointer to archive size
238  * @retval failedFile   pointer to first file name that failed.
239  * @return              0 on success
240  */
241 int fsmSetup(FSM_t fsm, fileStage goal,
242                 const rpmts ts,
243                 const rpmfi fi,
244                 FD_t cfd,
245                 /*@out@*/ unsigned int * archiveSize,
246                 /*@out@*/ const char ** failedFile)
247         /*@globals fileSystem @*/
248         /*@modifies fsm, ts, fi, *archiveSize, *failedFile, fileSystem  @*/;
249
250 /**
251  * Clean file state machine.
252  * @param fsm           file state machine data
253  * @return              0 on success
254  */
255 int fsmTeardown(FSM_t fsm)
256         /*@globals fileSystem @*/
257         /*@modifies fsm, fileSystem @*/;
258
259 /*@-exportlocal@*/
260 /**
261  * Retrieve transaction set from file state machine iterator.
262  * @param fsm           file state machine data
263  * @return              transaction set
264  */
265 rpmts fsmGetTs(const FSM_t fsm)
266         /*@*/;
267
268 /**
269  * Retrieve transaction element file info from file state machine iterator.
270  * @param fsm           file state machine data
271  * @return              transaction element file info
272  */
273 rpmfi fsmGetFi(/*@partial@*/ const FSM_t fsm)
274         /*@*/;
275
276 /**
277  * Map next file path and action.
278  * @param fsm           file state machine data
279  */
280 int fsmMapPath(FSM_t fsm)
281         /*@modifies fsm @*/;
282
283 /**
284  * Map file stat(2) info.
285  * @param fsm           file state machine data
286  */
287 int fsmMapAttrs(FSM_t fsm)
288         /*@modifies fsm @*/;
289 /*@=exportlocal@*/
290
291 /**
292  * File state machine driver.
293  * @param fsm           file state machine data
294  * @param stage         next stage
295  * @return              0 on success
296  */
297 int fsmStage(/*@partial@*/ FSM_t fsm, fileStage stage)
298         /*@globals fileSystem @*/
299         /*@modifies fsm, fileSystem @*/;
300
301 #ifdef __cplusplus
302 }
303 #endif
304
305 #endif  /* H_FSM */