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