RPM Community Forums

Mailing List Message of <rpm-cvs>

[CVS] RPM: rpm/rpmio/ tmagic.c

From: Jeff Johnson <jbj@rpm5.org>
Date: Tue 31 Jul 2007 - 21:11:00 CEST
Message-Id: <20070731191100.2089734844A@rpm5.org>
  RPM Package Manager, CVS Repository
  http://rpm5.org/cvs/
  ____________________________________________________________________________

  Server: rpm5.org                         Name:   Jeff Johnson
  Root:   /v/rpm/cvs                       Email:  jbj@rpm5.org
  Module: rpm                              Date:   31-Jul-2007 21:11:00
  Branch: HEAD                             Handle: 2007073120105900

  Modified files:
    rpm/rpmio               tmagic.c

  Log:
    retrieve file magic from buffer.

  Summary:
    Revision    Changes     Path
    1.2         +23 -14     rpm/rpmio/tmagic.c
  ____________________________________________________________________________

  patch -p0 <<'@@ .'
  Index: rpm/rpmio/tmagic.c
  ============================================================================
  $ cvs diff -u -r1.1 -r1.2 tmagic.c
  --- rpm/rpmio/tmagic.c	31 Jul 2007 18:25:33 -0000	1.1
  +++ rpm/rpmio/tmagic.c	31 Jul 2007 19:10:59 -0000	1.2
  @@ -11,9 +11,26 @@
   
   static int _debug = 0;
   
  -#define	FNPATH		"tmagic.test"
  +#define	FNPATH		"tmagic"
   static char * fnpath = FNPATH;
   
  +static const char * Fmagic(const char * buffer, size_t length)
  +{
  +    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);
  +	t = xstrdup((t ? t : ""));
  +	magic_close(ms);
  +    }
  +
  +    return t;
  +}
  +
   static void readFile(const char * path)
   {
       FD_t fd;
  @@ -23,11 +40,12 @@
       if (fd != NULL) {
   	char buf[BUFSIZ];
   	size_t len = Fread(buf, 1, sizeof(buf), fd);
  -	int xx;
  -        xx = Fclose(fd);
  +	(void) Fclose(fd);
   
  -	if (len > 0)
  -	    fwrite(buf, 1, len, stderr);
  +	if (len > 0) {
  +	    const char * magic = Fmagic(buf, len);
  +	    fprintf(stderr, "==> magic \"%s\"\n", magic);
  +	}
       }
   }
   
  @@ -48,11 +66,7 @@
   main(int argc, const char *argv[])
   {
       poptContext optCon = poptGetContext(argv[0], argc, argv, optionsTable, 0);
  -    int msflags = MAGIC_CHECK;
  -    magic_t ms = NULL;
  -    const char * magicfile = NULL;
       int rc;
  -    int xx;
   
       while ((rc = poptGetNextOpt(optCon)) > 0) {
   	switch (rc) {
  @@ -64,9 +78,6 @@
   	}
       }
   
  -    ms = magic_open(msflags);
  -assert(ms);
  -    xx = magic_load(ms, magicfile);
   
       if (_debug) {
   	rpmIncreaseVerbosity();
  @@ -75,8 +86,6 @@
   
       readFile(fnpath);
   
  -    if (ms)
  -	magic_close(ms);
   /*@i@*/ urlFreeCache();
   
       return 0;
  @@ .
Received on Tue Jul 31 21:11:00 2007
Driven by Jeff Johnson and the RPM project team.
Hosted by OpenPKG and Ralf S. Engelschall.
Powered by FreeBSD and OpenPKG.