rpm 5.2.1

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

rpmmg.c
Go to the documentation of this file.
1
5 #include "system.h"
6
7 #if defined(HAVE_MAGIC_H)
8 #include "magic.h"
9 #endif
10
11 #include <rpmiotypes.h>
12 #include <rpmio.h> /* for *Pool methods */
13 #include <rpmlog.h>
14 #define _RPMMG_INTERNAL
15 #include <rpmmg.h>
16
17 #include "debug.h"
18
19 /*@unchecked@*/
20 int _rpmmg_debug = 0;
21
22 /*@-mustmod@*/ /* XXX splint on crack */
23 static void rpmmgFini(void * _mg)
24 /*@globals fileSystem @*/
25 /*@modifies *_mg, fileSystem @*/
26 {
27 rpmmg mg = _mg;
28
29 #if defined(HAVE_MAGIC_H)
30 if (mg->ms) {
31 magic_close(mg->ms);
32 mg->ms = NULL;
33 }
34 #endif
35 mg->fn = _free(mg->fn);
36 }
37 /*@=mustmod@*/
38
39 /*@unchecked@*/ /*@only@*/ /*@null@*/
40 rpmioPool _rpmmgPool = NULL;
41
42 static rpmmg rpmmgGetPool(/*@null@*/ rpmioPool pool)
43 /*@globals _rpmmgPool, fileSystem @*/
44 /*@modifies pool, _rpmmgPool, fileSystem @*/
45 {
46 rpmmg mg;
47
48 if (_rpmmgPool == NULL) {
49 _rpmmgPool = rpmioNewPool("mg", sizeof(*mg), -1, _rpmmg_debug,
50 NULL, NULL, rpmmgFini);
51 pool = _rpmmgPool;
52 }
53 return (rpmmg) rpmioGetPool(pool, sizeof(*mg));
54 }
55
56 rpmmg rpmmgNew(const char * fn, int flags)
57 {
58 rpmmg mg = rpmmgGetPool(_rpmmgPool);
59 int xx;
60
61 if (fn)
62 mg->fn = xstrdup(fn);
63 #if defined(HAVE_MAGIC_H)
64 mg->flags = (flags ? flags : MAGIC_CHECK);/* XXX MAGIC_COMPRESS flag? */
65 mg->ms = magic_open(flags);
66 if (mg->ms == NULL) {
67 rpmlog(RPMLOG_ERR, _("magic_open(0x%x) failed: %s\n"),
68 flags, strerror(errno));
69 return rpmmgFree(mg);
70 }
71 xx = magic_load(mg->ms, mg->fn);
72 if (xx == -1) {
73 rpmlog(RPMLOG_ERR, _("magic_load(ms, %s) failed: %s\n"),
74 (fn ? fn : "(nil)"), magic_error(mg->ms));
75 return rpmmgFree(mg);
76 }
77 #endif
78
79 return rpmmgLink(mg);
80 }
81
82 const char * rpmmgFile(rpmmg mg, const char *fn)
83 {
84 const char * t = NULL;
85
86 #if defined(HAVE_MAGIC_H)
87 if (mg->ms) {
88 t = magic_file(mg->ms, fn);
89 /* XXX HACK: libmagic compiled without <pcreposix.h> spews here. */
90 if (t == NULL) {
91 const char * msg = magic_error(mg->ms);
92 if (strstr(msg, "regexec error 17, (match failed)") == NULL)
93 rpmlog(RPMLOG_ERR, _("magic_file(ms, %s) failed: %s\n"),
94 (fn ? fn : "(nil)"), msg);
95 }
96 }
97 #endif
98
99 if (t == NULL) t = "";
100 t = xstrdup(t);
101
102 if (_rpmmg_debug)
103 fprintf(stderr, "--> rpmmgFile(%p, %s) %s\n", mg, (fn ? fn : "(nil)"), t);
104 return t;
105 }
106
107 const char * rpmmgBuffer(rpmmg mg, const char * b, size_t nb)
108 {
109 const char * t = NULL;
110
111 #if defined(HAVE_MAGIC_H)
112 if (mg->ms) {
113 t = magic_buffer(mg->ms, b, nb);
114 /* XXX HACK: libmagic compiled without <pcreposix.h> spews here. */
115 if (t == NULL) {
116 const char * msg = magic_error(mg->ms);
117 if (strstr(msg, "regexec error 17, (match failed)") == NULL)
118 rpmlog(RPMLOG_ERR, _("magic_buffer(ms, %p[%u]) failed: %s\n"),
119 b, (unsigned)nb, msg);
120 }
121 }
122 #endif
123
124 if (t == NULL) t = "";
125 t = xstrdup(t);
126
127 if (_rpmmg_debug)
128 fprintf(stderr, "--> rpmmgBuffer(%p, %p[%d]) %s\n", mg, b, (int)nb, t);
129 return t;
130 }