Doxygen annotations for config files.
[tools/librpm-tizen.git] / lib / manifest.c
1 /** \ingroup rpmcli
2  * \file lib/manifest.c
3  */
4
5 #include "system.h"
6
7 #include <rpmio_internal.h>
8 #include "stringbuf.h"
9 #include "manifest.h"
10 #include "misc.h"
11 #include "debug.h"
12
13 /*@access StringBuf @*/
14
15 /**
16  * Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
17  * @param this          memory to free
18  * @retval              NULL always
19  */
20 static /*@null@*/ void * _free(/*@only@*/ /*@null@*/ const void * this) {
21     if (this)   free((void *)this);
22     return NULL;
23 }
24
25 char * rpmPermsString(int mode)
26 {
27     char *perms = xstrdup("----------");
28    
29     if (S_ISDIR(mode)) 
30         perms[0] = 'd';
31     else if (S_ISLNK(mode))
32         perms[0] = 'l';
33     else if (S_ISFIFO(mode)) 
34         perms[0] = 'p';
35     else if (S_ISSOCK(mode)) 
36         perms[0] = 's';
37     else if (S_ISCHR(mode))
38         perms[0] = 'c';
39     else if (S_ISBLK(mode))
40         perms[0] = 'b';
41
42     /*@-unrecog@*/
43     if (mode & S_IRUSR) perms[1] = 'r';
44     if (mode & S_IWUSR) perms[2] = 'w';
45     if (mode & S_IXUSR) perms[3] = 'x';
46  
47     if (mode & S_IRGRP) perms[4] = 'r';
48     if (mode & S_IWGRP) perms[5] = 'w';
49     if (mode & S_IXGRP) perms[6] = 'x';
50
51     if (mode & S_IROTH) perms[7] = 'r';
52     if (mode & S_IWOTH) perms[8] = 'w';
53     if (mode & S_IXOTH) perms[9] = 'x';
54
55     if (mode & S_ISUID)
56         perms[3] = ((mode & S_IXUSR) ? 's' : 'S'); 
57
58     if (mode & S_ISGID)
59         perms[6] = ((mode & S_IXGRP) ? 's' : 'S'); 
60
61     if (mode & S_ISVTX)
62         perms[9] = ((mode & S_IXOTH) ? 't' : 'T');
63     /*@=unrecog@*/
64
65     return perms;
66 }
67
68 /**@todo Infinite loops through manifest files exist, operator error for now. */
69 int rpmReadPackageManifest(FD_t fd, int * argcPtr, const char *** argvPtr)
70 {
71     StringBuf sb = newStringBuf();
72     char * s, *se;
73     int ac = 0;
74     const char ** av = NULL;
75     int argc = (argcPtr ? *argcPtr : 0);
76     const char ** argv = (argvPtr ? *argvPtr : NULL);
77     int rc = 0;
78     int i;
79
80     while (1) {
81         char line[BUFSIZ];
82
83         /* Read next line. */
84         s = fgets(line, sizeof(line) - 1, fdGetFp(fd));
85         if (s == NULL) {
86             /* XXX Ferror check needed */
87             break;
88         }
89
90         /* Skip comments. */
91         if ((se = strchr(s, '#')) != NULL) *se = '\0';
92
93         /* Trim white space. */
94         se = s + strlen(s);
95         while (se > s && (se[-1] == '\n' || se[-1] == '\r'))
96             *(--se) = '\0';
97         while (*s && strchr(" \f\n\r\t\v", *s) != NULL)
98             s++;
99         if (*s == '\0') continue;
100
101         /* Insure that file contains only ASCII */
102         if (*s < 32) {
103             rc = 1;
104             goto exit;
105         }
106
107         /* Concatenate next line in buffer. */
108         *se++ = ' ';
109         *se = '\0';
110         appendStringBuf(sb, s);
111     }
112
113     if (s == NULL)              /* XXX always true */
114         s = getStringBuf(sb);
115
116     if (!(s && *s)) {
117         rc = 1;
118         goto exit;
119     }
120
121     /* Glob manifest items. */
122     rc = rpmGlob(s, &ac, &av);
123     if (rc) goto exit;
124
125     /* Find 1st existing unprocessed arg. */
126     for (i = 0; i < argc; i++)
127         if (argv && argv[i]) break;
128
129     /* Concatenate existing unprocessed args after manifest contents. */
130     if (argv && i < argc) {
131         int nac = ac + (argc - i);
132         const char ** nav = xcalloc((nac + 1), sizeof(*nav));
133
134         if (ac)
135             memcpy(nav, av, ac * sizeof(*nav));
136         if ((argc - i) > 0)
137             memcpy(nav + ac, argv + i, (argc - i) * sizeof(*nav));
138         nav[nac] = NULL;
139
140         *argvPtr = argv = _free(argv);
141         av = _free(av);
142         av = nav;
143         ac = nac;
144     }
145
146     /* Save new argc/argv list. */
147     if (argvPtr) {
148         *argvPtr = _free(*argvPtr);
149         *argvPtr = av;
150     }
151     if (argcPtr)
152         *argcPtr = ac;
153
154 exit:
155     if (argvPtr == NULL || (rc != 0 && av)) {
156         for (i = 0; i < ac; i++)
157             av[i] = _free(av[i]);
158         av = _free(av);
159     }
160     freeStringBuf(sb);
161     return rc;
162 }