RPM Package Manager, CVS Repository
  http://rpm5.org/cvs/
  ____________________________________________________________________________

  Server: rpm5.org                         Name:   Jeff Johnson
  Root:   /v/rpm/cvs                       Email:  [EMAIL PROTECTED]
  Module: rpm                              Date:   01-Aug-2007 15:24:00
  Branch: HEAD                             Handle: 2007080114240000

  Modified files:
    rpm/rpmio               tmagic.c

  Log:
    start wrapping magic handling.

  Summary:
    Revision    Changes     Path
    1.3         +51 -12     rpm/rpmio/tmagic.c
  ____________________________________________________________________________

  patch -p0 <<'@@ .'
  Index: rpm/rpmio/tmagic.c
  ============================================================================
  $ cvs diff -u -r1.2 -r1.3 tmagic.c
  --- rpm/rpmio/tmagic.c        31 Jul 2007 19:10:59 -0000      1.2
  +++ rpm/rpmio/tmagic.c        1 Aug 2007 13:24:00 -0000       1.3
  @@ -14,28 +14,63 @@
   #define      FNPATH          "tmagic"
   static char * fnpath = FNPATH;
   
  -static const char * Fmagic(const char * buffer, size_t length)
  +typedef struct rpmmp_t * rpmmp;
  +
  +struct rpmmp_t {
  +    const char * fn;
  +    int flags;
  +    magic_t ms;
  +};
  +
  +static
  +rpmmp rpmmpFree(rpmmp mp)
  +{
  +    if (mp) {
  +     if (mp->ms) {
  +         magic_close(mp->ms);
  +         mp->ms = NULL;
  +     }
  +     mp->fn = _free(mp->fn);
  +     mp = _free(mp);
  +    }
  +    return NULL;
  +}
  +
  +static
  +rpmmp rpmmpNew(const char * fn, int flags)
  +{
  +    rpmmp mp = xcalloc(1, sizeof(*mp));
  +    int xx;
  +
  +    if (fn)
  +     mp->fn = xstrdup(fn);
  +    mp->flags = (flags ? flags : MAGIC_CHECK);
  +    mp->ms = magic_open(mp->ms);
  +    if (mp->ms == NULL)
  +     return rpmmpFree(mp);
  +    xx = magic_load(mp->ms, mp->fn);
  +    return mp;
  +}
  +
  +static
  +const char * rpmmpBuffer(rpmmp mp, const char * b, size_t nb)
   {
  -    int msflags = MAGIC_CHECK;
  -    const char * magicfile = NULL;
  -    magic_t ms = magic_open(msflags);
       const char * t = NULL;
   
  -    if (ms) {
  -     (void) magic_load(ms, magicfile);
  -     t = magic_buffer(ms, buffer, length);
  +    if (mp->ms) {
  +     t = magic_buffer(mp->ms, b, nb);
        t = xstrdup((t ? t : ""));
  -     magic_close(ms);
       }
   
       return t;
   }
   
  -static void readFile(const char * path)
  +static
  +void readFile(rpmmp mp, const char * path)
   {
       FD_t fd;
   
  -fprintf(stderr, "===== %s\n", path);
  +fprintf(stderr, "===== %s(%p, %s)\n", __FUNCTION__, mp, path);
       fd = Fopen(path, "r");
       if (fd != NULL) {
        char buf[BUFSIZ];
  @@ -43,8 +78,9 @@
        (void) Fclose(fd);
   
        if (len > 0) {
  -         const char * magic = Fmagic(buf, len);
  +         const char * magic = rpmmpBuffer(mp, buf, len);
            fprintf(stderr, "==> magic \"%s\"\n", magic);
  +         magic = _free(magic);
        }
       }
   }
  @@ -66,6 +102,7 @@
   main(int argc, const char *argv[])
   {
       poptContext optCon = poptGetContext(argv[0], argc, argv, optionsTable, 
0);
  +    rpmmp mp;
       int rc;
   
       while ((rc = poptGetNextOpt(optCon)) > 0) {
  @@ -84,7 +121,9 @@
        rpmIncreaseVerbosity();
       }
   
  -    readFile(fnpath);
  +    mp = rpmmpNew(NULL, 0);
  +    readFile(mp, fnpath);
  +    mp = rpmmpFree(mp);
   
   /[EMAIL PROTECTED]@*/ urlFreeCache();
   
  @@ .
______________________________________________________________________
RPM Package Manager                                    http://rpm5.org
CVS Sources Repository                                [email protected]

Reply via email to