[4.0] Use strip (instead of eu-strip) to support --strip-debug of *.so at build time
[platform/upstream/rpm.git] / lib / manifest.c
1 /** \ingroup rpmcli
2  * \file lib/manifest.c
3  */
4
5 #include "system.h"
6
7 #include <rpm/rpmlog.h>
8 #include <rpm/rpmfileutil.h>
9 #include <rpm/argv.h>
10
11 #include "lib/manifest.h"
12
13 #include "debug.h"
14
15
16 char * rpmPermsString(int mode)
17 {
18     char *perms = xstrdup("----------");
19    
20     if (S_ISREG(mode)) 
21         perms[0] = '-';
22     else if (S_ISDIR(mode)) 
23         perms[0] = 'd';
24     else if (S_ISLNK(mode))
25         perms[0] = 'l';
26     else if (S_ISFIFO(mode)) 
27         perms[0] = 'p';
28     else if (S_ISSOCK(mode)) 
29         perms[0] = 's';
30     else if (S_ISCHR(mode))
31         perms[0] = 'c';
32     else if (S_ISBLK(mode))
33         perms[0] = 'b';
34     else
35         perms[0] = '?';
36
37     if (mode & S_IRUSR) perms[1] = 'r';
38     if (mode & S_IWUSR) perms[2] = 'w';
39     if (mode & S_IXUSR) perms[3] = 'x';
40  
41     if (mode & S_IRGRP) perms[4] = 'r';
42     if (mode & S_IWGRP) perms[5] = 'w';
43     if (mode & S_IXGRP) perms[6] = 'x';
44
45     if (mode & S_IROTH) perms[7] = 'r';
46     if (mode & S_IWOTH) perms[8] = 'w';
47     if (mode & S_IXOTH) perms[9] = 'x';
48
49     if (mode & S_ISUID)
50         perms[3] = ((mode & S_IXUSR) ? 's' : 'S'); 
51
52     if (mode & S_ISGID)
53         perms[6] = ((mode & S_IXGRP) ? 's' : 'S'); 
54
55     if (mode & S_ISVTX)
56         perms[9] = ((mode & S_IXOTH) ? 't' : 'T');
57
58     return perms;
59 }
60
61 /**@todo Infinite loops through manifest files exist, operator error for now. */
62 rpmRC rpmReadPackageManifest(FD_t fd, int * argcPtr, char *** argvPtr)
63 {
64     ARGV_t sb = NULL;
65     char * s = NULL;
66     char * se;
67     int ac = 0;
68     char ** av = NULL;
69     int argc = (argcPtr ? *argcPtr : 0);
70     char ** argv = (argvPtr ? *argvPtr : NULL);
71     FILE * f = fdopen(Fileno(fd), "r");
72     rpmRC rpmrc = RPMRC_OK;
73     int i, j, next, npre;
74
75     if (f != NULL)
76     while (1) {
77         char line[BUFSIZ];
78
79         /* Read next line. */
80         s = fgets(line, sizeof(line) - 1, f);
81         if (s == NULL) {
82             /* XXX Ferror check needed */
83             break;
84         }
85
86         /* Skip comments. */
87         if ((se = strchr(s, '#')) != NULL) *se = '\0';
88
89         /* Trim white space. */
90         se = s + strlen(s);
91         while (se > s && (se[-1] == '\n' || se[-1] == '\r'))
92             *(--se) = '\0';
93         while (*s && strchr(" \f\n\r\t\v", *s) != NULL)
94             s++;
95         if (*s == '\0') continue;
96
97         /* Sanity checks: skip obviously binary lines and dash (for stdin) */
98         if (*s < 32 || rstreq(s, "-")) {
99             s = NULL;
100             rpmrc = RPMRC_NOTFOUND;
101             goto exit;
102         }
103
104         /* Concatenate next line in buffer. */
105         *se = '\0';
106         argvAdd(&sb, s);
107     }
108
109     s = argvJoin(sb, " ");
110
111     if (!(s && *s)) {
112         rpmrc = RPMRC_NOTFOUND;
113         goto exit;
114     }
115
116     /* Glob manifest items. */
117     rpmrc = (rpmGlob(s, &ac, &av) == 0 ? RPMRC_OK : RPMRC_FAIL);
118     if (rpmrc != RPMRC_OK) goto exit;
119
120     rpmlog(RPMLOG_DEBUG, "adding %d args from manifest.\n", ac);
121
122     /* Count non-NULL args, keeping track of 1st arg after last NULL. */
123     npre = 0;
124     next = 0;
125     if (argv != NULL)
126     for (i = 0; i < argc; i++) {
127         if (argv[i] != NULL)
128             npre++;
129         else if (i >= next)
130             next = i + 1;
131     }
132
133     /* Copy old arg list, inserting manifest before argv[next]. */
134     if (argv != NULL) {
135         int nac = npre + ac;
136         char ** nav = xcalloc((nac + 1), sizeof(*nav));
137
138         for (i = 0, j = 0; i < next; i++) {
139             if (argv[i] != NULL)
140                 nav[j++] = argv[i];
141         }
142
143         if (ac)
144             memcpy(nav + j, av, ac * sizeof(*nav));
145         if ((argc - next) > 0)
146             memcpy(nav + j + ac, argv + next, (argc - next) * sizeof(*nav));
147         nav[nac] = NULL;
148
149         if (argvPtr)
150             *argvPtr = argv = _free(argv);
151         av = _free(av);
152         av = nav;
153         ac = nac;
154     }
155
156     /* Save new argc/argv list. */
157     if (argvPtr) {
158         *argvPtr = _free(*argvPtr);
159         *argvPtr = av;
160     }
161     if (argcPtr)
162         *argcPtr = ac;
163
164 exit:
165     if (argvPtr == NULL || (rpmrc != RPMRC_OK && av)) {
166         if (av)
167         for (i = 0; i < ac; i++)
168            av[i] = _free(av[i]);
169         av = _free(av);
170     }
171     argvFree(sb);
172     free(s);
173     /* FIX: *argvPtr may be NULL. */
174     return rpmrc;
175 }