home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / MISC / SRC / UPGRADE / MKUGDB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-28  |  2.4 KB  |  122 lines

  1. #include <alloca.h>
  2. #include <ctype.h>
  3. #include <dirent.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <rpmlib.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/stat.h>
  10. #include <sys/types.h>
  11. #include <unistd.h>
  12.  
  13. #define FILENAME_TAG 1000000
  14.  
  15. int tags[] =  { RPMTAG_NAME, RPMTAG_VERSION, RPMTAG_RELEASE, RPMTAG_SERIAL,
  16.         RPMTAG_FILENAMES, RPMTAG_PROVIDES, RPMTAG_REQUIREFLAGS,
  17.         RPMTAG_REQUIRENAME, RPMTAG_REQUIREVERSION };
  18.  
  19. int numTags = sizeof(tags) / sizeof(int);
  20.  
  21. int main(int argc, char ** argv) {
  22.     char buf[300];
  23.     DIR * dir;
  24.     int outfd;
  25.     struct dirent * ent;
  26.     int fd, rc, isSource;
  27.     Header h, newh;
  28.     int count, type;
  29.     int i;
  30.     void * ptr;
  31.  
  32.     if (argc != 2) {
  33.     fprintf(stderr, "usage: mkugdb <dir>\n");
  34.     exit(1);
  35.     }
  36.  
  37.     strcpy(buf, argv[1]);
  38.     strcat(buf, "/RedHat/RPMS");
  39.  
  40.     dir = opendir(buf);
  41.     if (!dir) {
  42.     fprintf(stderr,"error opening directory %s: %s\n", buf,
  43.         strerror(errno));
  44.     return 1;
  45.     }
  46.     chdir(buf);
  47.  
  48.     strcpy(buf, argv[1]);
  49.     strcat(buf, "/RedHat/upgrade/ugdb");
  50.     
  51.     outfd = open(buf, O_WRONLY | O_CREAT, 0644);
  52.     if (outfd < 0) {
  53.     fprintf(stderr,"error creating file %s: %s\n", buf, strerror(errno));
  54.     return 1;
  55.     }
  56.  
  57.     errno = 0;
  58.     ent = readdir(dir);
  59.     if (errno) {
  60.     perror("readdir");
  61.     return 1;
  62.     }
  63.  
  64.     while (ent) {
  65.     if (!(ent->d_name[0] == '.' && (ent->d_name[1] == '\0' || 
  66.         ((ent->d_name[1] == '.') && (ent->d_name[2] == '\0'))))) {
  67.         fd = open(ent->d_name, O_RDONLY);
  68.  
  69.         if (fd < 0) {
  70.         perror("open");
  71.         exit(1);
  72.         }
  73.  
  74.         rc = pkgReadHeader(fd, &h, &isSource);
  75.  
  76.         close(fd);
  77.         if (rc) {
  78.         fprintf(stderr, "failed to pkgReadHeader %s\n", ent->d_name);
  79.         exit(1);
  80.         }
  81.  
  82.         newh = newHeader();
  83.         for (i = 0; i < numTags; i++) {
  84.         if (!getEntry(h, tags[i], &type, &ptr, &count)) {
  85.             switch (tags[i]) {
  86.               case RPMTAG_SERIAL:
  87.               case RPMTAG_PROVIDES:
  88.               case RPMTAG_REQUIREFLAGS:
  89.               case RPMTAG_REQUIRENAME:
  90.               case RPMTAG_REQUIREVERSION:
  91.             break;
  92.               default:
  93.             fprintf(stderr, "tag %d not found in file %s\n", 
  94.                 tags[i], ent->d_name);
  95.             exit(1);
  96.             }
  97.         } else 
  98.             addEntry(newh, tags[i], type, ptr, count);
  99.         }
  100.  
  101.         addEntry(newh, FILENAME_TAG, STRING_TYPE, ent->d_name, 1);
  102.  
  103.         writeHeader(outfd, newh, 1);
  104.  
  105.         freeHeader(newh);
  106.         freeHeader(h);
  107.     }
  108.  
  109.     errno = 0;
  110.     ent = readdir(dir);
  111.     if (errno) {
  112.         perror("readdir");
  113.         return 1;
  114.     }
  115.     } 
  116.  
  117.     closedir(dir);
  118.     close(outfd);
  119.  
  120.     return 0;
  121. }
  122.