added virtual query tag ability, fsnames query tag,
[platform/upstream/rpm.git] / lib / formats.c
1 #include "config.h"
2
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/stat.h>
6
7 #include "header.h"
8 #include "rpmlib.h"
9
10 static char * permsFormat(int_32 type, const void * data, 
11                           char * formatPrefix, int padding, int element);
12 static char * depflagsFormat(int_32 type, const void * data, 
13                              char * formatPrefix, int padding, int element);
14 static char * fflagsFormat(int_32 type, const void * data, 
15                            char * formatPrefix, int padding, int element);
16 static int fsnamesTag(Header h, int_32 * type, void ** data, int_32 * count,
17                       int * freeData);
18 static char * permsString(int mode);
19
20 const struct headerSprintfExtension rpmHeaderFormats[] = {
21     { HEADER_EXT_TAG, "RPMTAG_FSNAMES", { fsnamesTag } },
22     { HEADER_EXT_FORMAT, "depflags", { depflagsFormat } },
23     { HEADER_EXT_FORMAT, "fflags", { fflagsFormat } },
24     { HEADER_EXT_FORMAT, "perms", { permsFormat } },
25     { HEADER_EXT_FORMAT, "permissions", { permsFormat } },
26     { HEADER_EXT_MORE, NULL, { (void *) headerDefaultFormats } }
27 } ;
28
29 static char * permsString(int mode) {
30     char * perms = malloc(11);
31
32     strcpy(perms, "-----------");
33    
34     if (mode & S_ISVTX) perms[10] = 't';
35
36     if (mode & S_IRUSR) perms[1] = 'r';
37     if (mode & S_IWUSR) perms[2] = 'w';
38     if (mode & S_IXUSR) perms[3] = 'x';
39  
40     if (mode & S_IRGRP) perms[4] = 'r';
41     if (mode & S_IWGRP) perms[5] = 'w';
42     if (mode & S_IXGRP) perms[6] = 'x';
43
44     if (mode & S_IROTH) perms[7] = 'r';
45     if (mode & S_IWOTH) perms[8] = 'w';
46     if (mode & S_IXOTH) perms[9] = 'x';
47
48     if (mode & S_ISUID) {
49         if (mode & S_IXUSR) 
50             perms[3] = 's'; 
51         else
52             perms[3] = 'S'; 
53     }
54
55     if (mode & S_ISGID) {
56         if (mode & S_IXGRP) 
57             perms[6] = 's'; 
58         else
59             perms[6] = 'S'; 
60     }
61
62     if (S_ISDIR(mode)) 
63         perms[0] = 'd';
64     else if (S_ISLNK(mode)) {
65         perms[0] = 'l';
66     }
67     else if (S_ISFIFO(mode)) 
68         perms[0] = 'p';
69     else if (S_ISSOCK(mode)) 
70         perms[0] = 'l';
71     else if (S_ISCHR(mode)) {
72         perms[0] = 'c';
73     } else if (S_ISBLK(mode)) {
74         perms[0] = 'b';
75     }
76
77     return perms;
78 }
79
80 static char * permsFormat(int_32 type, const void * data, 
81                          char * formatPrefix, int padding, int element) {
82     char * val;
83     char * buf;
84
85     if (type != RPM_INT32_TYPE) {
86         val = malloc(20);
87         strcpy(val, "(not a number)");
88     } else {
89         val = malloc(15 + padding);
90         strcat(formatPrefix, "s");
91         buf = permsString(*((int_32 *) data));
92         sprintf(val, formatPrefix, buf);
93         free(buf);
94     }
95
96     return val;
97 }
98
99 static char * fflagsFormat(int_32 type, const void * data, 
100                          char * formatPrefix, int padding, int element) {
101     char * val;
102     char buf[10];
103     int anint = *((int_32 *) data);
104
105     if (type != RPM_INT32_TYPE) {
106         val = malloc(20);
107         strcpy(val, "(not a number)");
108     } else {
109         buf[0] = '\0';
110         if (anint & RPMFILE_DOC)
111             strcat(buf, "d");
112         if (anint & RPMFILE_CONFIG)
113             strcat(buf, "c");
114
115         val = malloc(5 + padding);
116         strcat(formatPrefix, "s");
117         sprintf(val, formatPrefix, buf);
118     }
119
120     return val;
121 }
122
123 static char * depflagsFormat(int_32 type, const void * data, 
124                          char * formatPrefix, int padding, int element) {
125     char * val;
126     char buf[10];
127     int anint = *((int_32 *) data);
128
129     if (type != RPM_INT32_TYPE) {
130         val = malloc(20);
131         strcpy(val, "(not a number)");
132     } else {
133         *buf = '\0';
134
135         if (anint & RPMSENSE_LESS) 
136             strcat(buf, "<");
137         if (anint & RPMSENSE_GREATER)
138             strcat(buf, ">");
139         if (anint & RPMSENSE_EQUAL)
140             strcat(buf, "=");
141         if (anint & RPMSENSE_SERIAL)
142             strcat(buf, "S");
143
144         val = malloc(5 + padding);
145         strcat(formatPrefix, "s");
146         sprintf(val, formatPrefix, buf);
147     }
148
149     return val;
150 }
151
152 static int fsnamesTag(Header h, int_32 * type, void ** data, int_32 * count,
153                       int * freeData) {
154     char ** list;
155     int i;
156
157     if (rpmGetFilesystemList(&list)) {
158         return 1;
159     }
160
161     *type = RPM_STRING_ARRAY_TYPE;
162     *((char ***) data) = list;
163
164     for (i = 0; list[i]; i++) ;
165     *count = i;
166
167     *freeData = 0;
168
169     return 0; 
170 }