backup.c

Go to the documentation of this file.
00001 /*
00002  *  backup.c
00003  *
00004  *  Copyright (c) 2005 by Judd Vinet <jvinet@zeroflux.org>
00005  *  Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
00006  *  Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu>
00007  *  Copyright (c) 2006 by Miklos Vajna <vmiklos@frugalware.org>
00008  *
00009  *  This program is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  This program is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00021  */
00022 
00023 #include "config.h"
00024 
00025 #include <stdlib.h>
00026 #include <string.h>
00027 
00028 /* libalpm */
00029 #include "backup.h"
00030 #include "alpm_list.h"
00031 #include "log.h"
00032 #include "util.h"
00033 
00034 /* split a backup string "file\thash" into two strings : file and hash */
00035 int _alpm_backup_split(const char *string, char **file, char **hash)
00036 {
00037     char *str = strdup(string);
00038     char *ptr;
00039 
00040     /* tab delimiter */
00041     ptr = strchr(str, '\t');
00042     if(ptr == NULL) {
00043         if(file) {
00044             *file = str;
00045         }
00046         return(0);
00047     }
00048     *ptr = '\0';
00049     ptr++;
00050     /* now str points to the filename and ptr points to the hash */
00051     if(file) {
00052         *file = strdup(str);
00053     }
00054     if(hash) {
00055         *hash = strdup(ptr);
00056     }
00057     FREE(str);
00058     return(1);
00059 }
00060 
00061 char *_alpm_backup_file(const char *string)
00062 {
00063     char *file = NULL;
00064     _alpm_backup_split(string, &file, NULL);
00065     return(file);
00066 }
00067 
00068 char *_alpm_backup_hash(const char *string)
00069 {
00070     char *hash = NULL;
00071     _alpm_backup_split(string, NULL, &hash);
00072     return(hash);
00073 }
00074 
00075 /* Look for a filename in a pmpkg_t.backup list.  If we find it,
00076  * then we return the md5 hash (parsed from the same line)
00077  */
00078 char *_alpm_needbackup(const char *file, const alpm_list_t *backup)
00079 {
00080     const alpm_list_t *lp;
00081 
00082     ALPM_LOG_FUNC;
00083 
00084     if(file == NULL || backup == NULL) {
00085         return(NULL);
00086     }
00087 
00088     /* run through the backup list and parse out the hash for our file */
00089     for(lp = backup; lp; lp = lp->next) {
00090         char *filename = NULL;
00091         char *hash = NULL;
00092 
00093         /* no hash found */
00094         if(!_alpm_backup_split((char *)lp->data, &filename, &hash)) {
00095             FREE(filename);
00096             continue;
00097         }
00098         if(strcmp(file, filename) == 0) {
00099             FREE(filename);
00100             return(hash);
00101         }
00102         FREE(filename);
00103         FREE(hash);
00104     }
00105 
00106     return(NULL);
00107 }
00108 
00109 /* vim: set ts=2 sw=2 noet: */

Generated on Mon Jan 14 23:53:39 2008 for libalpm by  doxygen 1.5.4