/* * GEM MINE - The ROCK Linux Package Manager * Copyright (C) 2002-2005 Clifford Wolf * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include #include #include #include #include #include #include "md5sum.h" #include "memdb.h" struct memdb_t md5_memdb; int md5_memdb_filled; /* This function is defined in md5.c (which is from GNU textutils 2.0) */ extern char * md5_file(const char *filename); char * md5sum_create(char * root, char * filename) { char realfilename[1024]; struct stat statbuf; snprintf(realfilename, 1024, "%s/%s", root, filename); if (lstat(realfilename, &statbuf) != 0 || S_ISDIR(statbuf.st_mode)) return ""; else if (!S_ISREG(statbuf.st_mode)) return "X"; else return md5_file(realfilename); } /* Returns if file is unmodified, modified, duplicate, or shared. */ int md5sum_check(char * root, char * filename) { char *md5_f, *md5_d; md5_f = md5sum_create(root, filename); md5_d = memdb_get(&md5_memdb, filename); /* printf ("md5sum_check(): md5_f: %s, md5_d: %s\n", (md5_f) ? md5_f : "NULL", (md5_d) ? md5_d : "NULL"); */ if (strcmp(md5_f, "") == 0) return MD5SUM_CHECK_OK; else if (md5_d == NULL) return MD5SUM_CHECK_DUPLICATE; else if (strcmp(md5_f, md5_d) == 0 || strcmp(md5_d, "X") == 0) return MD5SUM_CHECK_OK; else if (strcmp(md5_d, "Duplicate entry") == 0) return MD5SUM_CHECK_SHARED; else return MD5SUM_CHECK_MODIFIED; } void md5sum_initdb(char * root, int verbose) { struct dirent *md5_dent; char buffer[1024]; char *md5sum, *filename; FILE *f; DIR *d; if ( md5_memdb_filled ) return; snprintf(buffer, 1024, "%s/var/adm/md5sums", root); if ( (d = opendir(buffer)) == NULL ) return; if (verbose) printf("Reading MD5 checksum database into memory ...\n"); memdb_init(&md5_memdb); while ( (md5_dent = readdir(d)) != NULL ) { snprintf(buffer, 1024, "%s/var/adm/md5sums/%s", root, md5_dent->d_name); f = fopen(buffer, "r"); if ( f != NULL ) { while (fgets(buffer, 1024, f) != NULL) { md5sum = strtok(buffer, " \t\n"); filename = strtok(NULL, "\n"); if ( md5sum && filename) { while ( *filename == ' ' ) filename++; memdb_put(&md5_memdb, filename, md5sum); } } fclose(f); } } closedir(d); md5_memdb_filled = 1; }