aboutsummaryrefslogtreecommitdiffstats
path: root/rpm2header.c
blob: 1dd3ab957a591057a72a08b4df1760d9ffde6b06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <rpmlib.h>

#ifdef RPM_42
#include <rpm/rpmts.h>
#endif

#define FILENAME_TAG 1000000
#define FILESIZE_TAG 1000001

const char *basename(const char *f) {
  char *p = strrchr(f, '/');
  return p ? p + 1 : f;
}

int_32 FD_size(FD_t fd) {
  struct stat sb;
  fstat(fdFileno(fd), &sb);
  return sb.st_size;
}

int main(int argc, char **argv) {
  int i;
  FD_t fout;

  if (argc < 2) {
    fprintf(stderr, "usage: rpm2header <rpms>\n");
    exit(1);
  }

  fout = fdDup(1 /*stdout*/);
  
  for (i = 1; i < argc; i++) {
    FD_t fd;
    Header h;
    int_32 size;
    const char *name = basename(argv[i]);
#ifdef RPM_42
    rpmts ts;
    /* rpmVSFlags vsflags, ovsflags; */
#else
    int isSource;
#endif
    
    fprintf(stderr, "%s\n", argv[i]);
    
    if (!(fd = fdOpen(argv[i], O_RDONLY, 0666))) {
      perror("open");
      exit(1);
    }
    size = FD_size(fd);

#ifdef RPM_42
    ts = rpmtsCreate();
    rpmtsSetVSFlags(ts, _RPMVSF_NOSIGNATURES);
    if (rpmReadPackageFile(ts, fd, argv[1], &h) == 0) {
#else
    if (rpmReadPackageHeader(fd, &h, &isSource, NULL, NULL) == 0) {
#endif
      headerRemoveEntry(h, RPMTAG_POSTIN);
      headerRemoveEntry(h, RPMTAG_POSTUN);
      headerRemoveEntry(h, RPMTAG_PREIN);
      headerRemoveEntry(h, RPMTAG_PREUN);
      headerRemoveEntry(h, RPMTAG_FILEUSERNAME);
      headerRemoveEntry(h, RPMTAG_FILEGROUPNAME);
      headerRemoveEntry(h, RPMTAG_FILEVERIFYFLAGS);
      headerRemoveEntry(h, RPMTAG_FILERDEVS);
      headerRemoveEntry(h, RPMTAG_FILEMTIMES);
      headerRemoveEntry(h, RPMTAG_FILEDEVICES);
      headerRemoveEntry(h, RPMTAG_FILEINODES);
      headerRemoveEntry(h, RPMTAG_TRIGGERSCRIPTS);
      headerRemoveEntry(h, RPMTAG_TRIGGERVERSION);
      headerRemoveEntry(h, RPMTAG_TRIGGERFLAGS);
      headerRemoveEntry(h, RPMTAG_TRIGGERNAME);
      headerRemoveEntry(h, RPMTAG_CHANGELOGTIME);
      headerRemoveEntry(h, RPMTAG_CHANGELOGNAME);
      headerRemoveEntry(h, RPMTAG_CHANGELOGTEXT);
      headerRemoveEntry(h, RPMTAG_ICON);
      headerRemoveEntry(h, RPMTAG_GIF);
      headerRemoveEntry(h, RPMTAG_VENDOR);
      headerRemoveEntry(h, RPMTAG_EXCLUDE);
      headerRemoveEntry(h, RPMTAG_EXCLUSIVE);
      headerRemoveEntry(h, RPMTAG_DISTRIBUTION);
      headerRemoveEntry(h, RPMTAG_VERIFYSCRIPT);

      /* removing that break updates.
      headerRemoveEntry(h, RPMTAG_OLDFILENAMES);
      headerRemoveEntry(h, RPMTAG_BASENAMES);
      headerRemoveEntry(h, RPMTAG_DIRINDEXES);
      headerRemoveEntry(h, RPMTAG_DIRNAMES);
      */

      headerAddEntry(h, FILENAME_TAG, RPM_STRING_TYPE, name, 1);
      headerAddEntry(h, FILESIZE_TAG, RPM_INT32_TYPE, &size, 1);
      headerWrite(fout, h, HEADER_MAGIC_YES);
      headerFree(h);
    }
    fdClose(fd);
  }
  return 0;
}