rpm 5.2.1

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

- File List
- Globals

rpmio/rpmmg.c
Go to the documentation of this file.

    00001
    00005 #include "system.h"
    00006
    00007 #if defined(HAVE_MAGIC_H)
    00008 #include "magic.h"
    00009 #endif
    00010
    00011 #include <rpmiotypes.h>
    00012 #include <rpmio.h>      /* for *Pool methods */
    00013 #include <rpmlog.h>
    00014 #define _RPMMG_INTERNAL
    00015 #include <rpmmg.h>
    00016
    00017 #include "debug.h"
    00018
    00019 /*@unchecked@*/
    00020 int _rpmmg_debug = 0;
    00021
    00022 /*@-mustmod@*/  /* XXX splint on crack */
    00023 static void rpmmgFini(void * _mg)
    00024         /*@globals fileSystem @*/
    00025         /*@modifies *_mg, fileSystem @*/
    00026 {
    00027     rpmmg mg = _mg;
    00028
    00029 #if defined(HAVE_MAGIC_H)
    00030     if (mg->ms) {
    00031         magic_close(mg->ms);
    00032         mg->ms = NULL;
    00033     }
    00034 #endif
    00035     mg->fn = _free(mg->fn);
    00036 }
    00037 /*@=mustmod@*/
    00038
    00039 /*@unchecked@*/ /*@only@*/ /*@null@*/
    00040 rpmioPool _rpmmgPool = NULL;
    00041
    00042 static rpmmg rpmmgGetPool(/*@null@*/ rpmioPool pool)
    00043         /*@globals _rpmmgPool, fileSystem @*/
    00044         /*@modifies pool, _rpmmgPool, fileSystem @*/
    00045 {
    00046     rpmmg mg;
    00047
    00048     if (_rpmmgPool == NULL) {
    00049         _rpmmgPool = rpmioNewPool("mg", sizeof(*mg), -1, _rpmmg_debug,
    00050                         NULL, NULL, rpmmgFini);
    00051         pool = _rpmmgPool;
    00052     }
    00053     return (rpmmg) rpmioGetPool(pool, sizeof(*mg));
    00054 }
    00055
    00056 rpmmg rpmmgNew(const char * fn, int flags)
    00057 {
    00058     rpmmg mg = rpmmgGetPool(_rpmmgPool);
    00059     int xx;
    00060
    00061     if (fn)
    00062         mg->fn = xstrdup(fn);
    00063 #if defined(HAVE_MAGIC_H)
    00064     mg->flags = (flags ? flags : MAGIC_CHECK);/* XXX MAGIC_COMPRESS flag? */
    00065     mg->ms = magic_open(flags);
    00066     if (mg->ms == NULL) {
    00067         rpmlog(RPMLOG_ERR, _("magic_open(0x%x) failed: %s\n"),
    00068                 flags, strerror(errno));
    00069         return rpmmgFree(mg);
    00070     }
    00071     xx = magic_load(mg->ms, mg->fn);
    00072     if (xx == -1) {
    00073         rpmlog(RPMLOG_ERR, _("magic_load(ms, %s) failed: %s\n"),
    00074                 (fn ? fn : "(nil)"), magic_error(mg->ms));
    00075         return rpmmgFree(mg);
    00076     }
    00077 #endif
    00078
    00079     return rpmmgLink(mg);
    00080 }
    00081
    00082 const char * rpmmgFile(rpmmg mg, const char *fn)
    00083 {
    00084     const char * t = NULL;
    00085
    00086 #if defined(HAVE_MAGIC_H)
    00087     if (mg->ms) {
    00088         t = magic_file(mg->ms, fn);
    00089         /* XXX HACK: libmagic compiled without <pcreposix.h> spews here. */
    00090         if (t == NULL) {
    00091             const char * msg = magic_error(mg->ms);
    00092             if (strstr(msg, "regexec error 17, (match failed)") == NULL)
    00093                 rpmlog(RPMLOG_ERR, _("magic_file(ms, %s) failed: %s\n"),
    00094                         (fn ? fn : "(nil)"), msg);
    00095         }
    00096     }
    00097 #endif
    00098
    00099     if (t == NULL) t = "";
    00100     t = xstrdup(t);
    00101
    00102 if (_rpmmg_debug)
    00103 fprintf(stderr, "--> rpmmgFile(%p, %s) %s\n", mg, (fn ? fn : "(nil)"), t);
    00104     return t;
    00105 }
    00106
    00107 const char * rpmmgBuffer(rpmmg mg, const char * b, size_t nb)
    00108 {
    00109     const char * t = NULL;
    00110
    00111 #if defined(HAVE_MAGIC_H)
    00112     if (mg->ms) {
    00113         t = magic_buffer(mg->ms, b, nb);
    00114         /* XXX HACK: libmagic compiled without <pcreposix.h> spews here. */
    00115         if (t == NULL) {
    00116             const char * msg = magic_error(mg->ms);
    00117             if (strstr(msg, "regexec error 17, (match failed)") == NULL)
    00118                 rpmlog(RPMLOG_ERR, _("magic_buffer(ms, %p[%u]) failed: %s\n"),
    00119                         b, (unsigned)nb, msg);
    00120         }
    00121     }
    00122 #endif
    00123
    00124     if (t == NULL) t = "";
    00125     t = xstrdup(t);
    00126
    00127 if (_rpmmg_debug)
    00128 fprintf(stderr, "--> rpmmgBuffer(%p, %p[%d]) %s\n", mg, b, (int)nb, t);
    00129     return t;
    00130 }