db1 to db3 conversion program (Michael Schroeder <mls@suse.de>)

CVS patchset: 5755
CVS date: 2002/10/06 05:55:24
This commit is contained in:
jbj 2002-10-06 05:55:24 +00:00
parent 9ff3ad0cae
commit 8e71e2cc31
4 changed files with 192 additions and 13 deletions

View File

@ -28,13 +28,15 @@ LDADD = \
@INTLLIBS@
noinst_PROGRAMS = \
dump dumpdb rpmarchive rpmheader rpmlead rpmsignature
convertdb1 dump dumpdb rpmarchive rpmheader rpmlead rpmsignature
pkgbindir = @RPMCONFIGDIR@
pkgbin_PROGRAMS = javadeps rpmcache striptofile unstripfile
bin_PROGRAMS = rpmgraph
convertdb1_SOURCES = convertdb1.c
javadeps_SOURCES = javadeps.c
rpmcache_SOURCES = rpmcache.c

177
tools/convertdb1.c Normal file
View File

@ -0,0 +1,177 @@
#include "system.h"
#include <rpmio.h>
#include <rpmlib.h>
#include <rpmdb.h>
#include <rpmmacro.h>
#include "legacy.h"
#include "debug.h"
#define FA_MAGIC 0x02050920
struct faFileHeader{
unsigned int magic;
unsigned int firstFree;
};
struct faHeader {
unsigned int size;
unsigned int freeNext; /* offset of the next free block, 0 if none */
unsigned int freePrev;
unsigned int isFree;
/* note that the u16's appear last for alignment/space reasons */
};
static int fadFileSize;
static ssize_t Pread(FD_t fd, void * buf, size_t count, off_t offset) {
if (Fseek(fd, offset, SEEK_SET) < 0)
return -1;
return Fread(buf, sizeof(char), count, fd);
}
static FD_t fadOpen(const char * path)
{
struct faFileHeader newHdr;
FD_t fd;
struct stat stb;
fd = Fopen(path, "r.fdio");
if (!fd || Ferror(fd))
return NULL;
if (fstat(Fileno(fd), &stb)) {
Fclose(fd);
return NULL;
}
fadFileSize = stb.st_size;
/* is this file brand new? */
if (fadFileSize == 0) {
Fclose(fd);
return NULL;
}
if (Pread(fd, &newHdr, sizeof(newHdr), 0) != sizeof(newHdr)) {
Fclose(fd);
return NULL;
}
if (newHdr.magic != FA_MAGIC) {
Fclose(fd);
return NULL;
}
/*@-refcounttrans@*/ return fd /*@=refcounttrans@*/ ;
}
static int fadNextOffset(FD_t fd, unsigned int lastOffset)
{
struct faHeader header;
int offset;
offset = (lastOffset)
? (lastOffset - sizeof(header))
: sizeof(struct faFileHeader);
if (offset >= fadFileSize)
return 0;
if (Pread(fd, &header, sizeof(header), offset) != sizeof(header))
return 0;
if (!lastOffset && !header.isFree)
return (offset + sizeof(header));
do {
offset += header.size;
if (Pread(fd, &header, sizeof(header), offset) != sizeof(header))
return 0;
if (!header.isFree) break;
} while (offset < fadFileSize && header.isFree);
if (offset < fadFileSize) {
/* Sanity check this to make sure we're not going in loops */
offset += sizeof(header);
if (offset <= lastOffset) return -1;
return offset;
} else
return 0;
}
static int fadFirstOffset(FD_t fd)
{
return fadNextOffset(fd, 0);
}
static rpmdb db;
int
main(argc, argv)
int argc;
char **argv;
{
FD_t fd;
int offset;
Header h;
const char *name;
const char *version;
const char *release;
if (argc != 2)
{
fprintf(stderr, "usage: rpm3import <packages.rpm>\n");
exit(1);
}
if ((fd = fadOpen(argv[1])) == 0)
{
fprintf(stderr, "could not open %s\n", argv[1]);
exit(1);
}
rpmInitMacros(NULL, "/usr/lib/rpm/macros");
if (rpmdbOpen("/", &db, O_RDWR, 0644)) {
fprintf(stderr, "could not open rpm database\n");
exit(1);
}
for (offset = fadFirstOffset(fd); offset; offset = fadNextOffset(fd, offset))
{
rpmdbMatchIterator mi;
/* have to use lseek instead of Fseek because headerRead
* uses low level IO
*/
if (lseek(Fileno(fd), (off_t)offset, SEEK_SET) == -1)
{
perror("lseek");
continue;
}
h = headerRead(fd, HEADER_MAGIC_NO);
if (!h)
continue;
compressFilelist(h);
headerNVR(h, &name, &version, &release);
mi = rpmdbInitIterator(db, RPMTAG_NAME, name, 0);
rpmdbSetIteratorRE(mi, RPMTAG_VERSION, RPMMIRE_DEFAULT, version);
rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT, release);
if (rpmdbNextIterator(mi))
{
printf("%s-%s-%s is already in database\n", name, version, release);
rpmdbFreeIterator(mi);
headerFree(h);
continue;
}
rpmdbFreeIterator(mi);
if (rpmdbAdd(db, -1, h, 0, 0))
{
fprintf(stderr, "could not add %s-%s-%s!\n", name, version, release);
}
headerFree(h);
}
Fclose(fd);
rpmdbClose(db);
return 0;
}

View File

@ -267,7 +267,7 @@ unstrip_info_to_data (UnstripInfo *info,
fprintf (stderr, "Warning. unsupported elf class\n");
}
void
static void
unstrip_info_from_data32 (UnstripInfo *info,
Elf *elf,
Elf_Data *data)
@ -289,7 +289,7 @@ unstrip_info_from_data32 (UnstripInfo *info,
}
}
void
static void
unstrip_info_from_data64 (UnstripInfo *info,
Elf *elf,
Elf_Data *data)
@ -351,7 +351,7 @@ debug_link_to_data32 (DebugLink *debuglink,
data->d_buf = calloc (1, data->d_size);
strcpy (data->d_buf, debuglink->filename);
p = data->d_buf + namelen_aligned;
p = ((char *)data->d_buf) + namelen_aligned;
*(Elf32_Word *)p = word32_to_file (debuglink->checksum, elf);
}
@ -375,7 +375,7 @@ debug_link_to_data64 (DebugLink *debuglink,
data->d_buf = calloc (1, data->d_size);
strcpy (data->d_buf, debuglink->filename);
p = data->d_buf + namelen_aligned;
p = ((char *)data->d_buf) + namelen_aligned;
*(Elf64_Word *)p = word64_to_file (debuglink->checksum, elf);
}
@ -397,7 +397,7 @@ debug_link_to_data (DebugLink *debuglink, Elf *elf, Elf_Data *data)
fprintf (stderr, "Warning. unsupported elf class\n");
}
void
static void
debug_link_from_data32 (DebugLink *debuglink,
Elf *elf,
Elf_Data *data)
@ -409,12 +409,12 @@ debug_link_from_data32 (DebugLink *debuglink,
namelen_aligned = align_up (strlen (debuglink->filename) + 1, 4);
p = data->d_buf + namelen_aligned;
p = ((char *)data->d_buf) + namelen_aligned;
debuglink->checksum = word32_from_file (*(Elf32_Word *)p, elf);
}
void
static void
debug_link_from_data64 (DebugLink *debuglink,
Elf *elf,
Elf_Data *data)
@ -426,7 +426,7 @@ debug_link_from_data64 (DebugLink *debuglink,
namelen_aligned = align_up (strlen (debuglink->filename) + 1, 4);
p = data->d_buf + namelen_aligned;
p = ((char *)data->d_buf) + namelen_aligned;
debuglink->checksum = word64_from_file (*(Elf64_Word *)p, elf);
}

View File

@ -9,7 +9,7 @@
#include "debug.h"
DebugLink *
static DebugLink *
read_debuglink (Elf *elf)
{
GElf_Ehdr ehdr;
@ -47,7 +47,7 @@ read_debuglink (Elf *elf)
return NULL;
}
Elf_Scn *
static Elf_Scn *
find_section (Elf *elf, const unsigned char *name, const unsigned char *strtab)
{
Elf_Scn *section;
@ -69,7 +69,7 @@ find_section (Elf *elf, const unsigned char *name, const unsigned char *strtab)
return section;
}
size_t
static size_t
find_in_strtab (char *name, char *strtab, size_t strtab_len)
{
int name_len, i;
@ -82,7 +82,7 @@ find_in_strtab (char *name, char *strtab, size_t strtab_len)
return 0;
}
void
static void
unstrip_file (Elf *elf, Elf *debug_elf, Elf *out_elf)
{
UnstripInfo *info;