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

# 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 <rpmio.h>
    00012 #include <rpmlog.h>
    00013 #define _RPMMG_INTERNAL
    00014 #include <rpmmg.h>
    00015
    00016 #include "debug.h"
    00017
    00018 /*@unchecked@*/
    00019 int _rpmmg_debug = 0;
    00020
    00021 rpmmg rpmmgFree(rpmmg mg)
    00022 {
    00023 if (_rpmmg_debug)
    00024 fprintf(stderr, "--> rpmmgFree(%p)\n", mg);
    00025     if (mg) {
    00026 #if defined(HAVE_MAGIC_H)
    00027         if (mg->ms) {
    00028             magic_close(mg->ms);
    00029             mg->ms = NULL;
    00030         }
    00031 #endif
    00032         mg->fn = _free(mg->fn);
    00033         mg = _free(mg);
    00034     }
    00035     return NULL;
    00036 }
    00037
    00038 rpmmg rpmmgNew(const char * fn, int flags)
    00039 {
    00040     rpmmg mg = xcalloc(1, sizeof(*mg));
    00041     int xx;
    00042
    00043     if (fn)
    00044         mg->fn = xstrdup(fn);
    00045 #if defined(HAVE_MAGIC_H)
    00046     mg->flags = (flags ? flags : MAGIC_CHECK);/* XXX MAGIC_COMPRESS flag? */
    00047     mg->ms = magic_open(flags);
    00048     if (mg->ms == NULL) {
    00049         rpmlog(RPMLOG_ERR, _("magic_open(0x%x) failed: %s\n"),
    00050                 flags, strerror(errno));
    00051         return rpmmgFree(mg);
    00052     }
    00053     xx = magic_load(mg->ms, mg->fn);
    00054     if (xx == -1) {
    00055         rpmlog(RPMLOG_ERR, _("magic_load(ms, %s) failed: %s\n"),
    00056                 (fn ? fn : "(nil)"), magic_error(mg->ms));
    00057         return rpmmgFree(mg);
    00058     }
    00059 #endif
    00060
    00061 if (_rpmmg_debug)
    00062 fprintf(stderr, "--> rpmmgNew(%s, 0x%x) mg %p\n", (fn ? fn : "(nil)"), flags, mg);
    00063     return mg;
    00064 }
    00065
    00066 const char * rpmmgFile(rpmmg mg, const char *fn)
    00067 {
    00068     const char * t = NULL;
    00069
    00070 #if defined(HAVE_MAGIC_H)
    00071     if (mg->ms) {
    00072         t = magic_file(mg->ms, fn);
    00073         if (t == NULL) {
    00074             rpmlog(RPMLOG_ERR, _("magic_file(ms, %s) failed: %s\n"),
    00075                 (fn ? fn : "(nil)"), magic_error(mg->ms));
    00076         }
    00077     }
    00078 #endif
    00079
    00080     if (t == NULL) t = "";
    00081     t = xstrdup(t);
    00082
    00083 if (_rpmmg_debug)
    00084 fprintf(stderr, "--> rpmmgFile(%p, %s) %s\n", mg, (fn ? fn : "(nil)"), t);
    00085     return t;
    00086 }
    00087
    00088 const char * rpmmgBuffer(rpmmg mg, const char * b, size_t nb)
    00089 {
    00090     const char * t = NULL;
    00091
    00092 #if defined(HAVE_MAGIC_H)
    00093     if (mg->ms) {
    00094         t = magic_buffer(mg->ms, b, nb);
    00095         if (t == NULL) {
    00096             rpmlog(RPMLOG_ERR, _("magic_buffer(ms, %p[%u]) failed: %s\n"),
    00097                 b, (unsigned)nb, magic_error(mg->ms));
    00098         }
    00099     }
    00100 #endif
    00101
    00102     if (t == NULL) t = "";
    00103     t = xstrdup(t);
    00104
    00105 if (_rpmmg_debug)
    00106 fprintf(stderr, "--> rpmmgBuffer(%p, %p[%d]) %s\n", mg, b, (int)nb, t);
    00107     return t;
    00108 }