aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJason Woodward2004-10-02 06:33:03 +0000
committerJason Woodward2004-10-02 06:33:03 +0000
commit3881ff4437a10dd85856798aff07eaba524da291 (patch)
tree5f008bfb427a4b9fe0bf1caa8196d4c619237255 /src
parent41564a244df8ab9071f2a3caf1e663923ff81d54 (diff)
downloadslapt-get-3881ff4437a10dd85856798aff07eaba524da291.tar.gz
added purge_old_cached_pkgs() to be run at the end of each transaction (thanks Michel Hermier)
Diffstat (limited to 'src')
-rw-r--r--src/package.c110
-rw-r--r--src/package.h3
-rw-r--r--src/transaction.c2
3 files changed, 115 insertions, 0 deletions
diff --git a/src/package.c b/src/package.c
index 18fd8fa..281c054 100644
--- a/src/package.c
+++ b/src/package.c
@@ -2016,3 +2016,113 @@ static char *gen_filename_from_url(const char *url,const char *file){
return cleaned;
}
+
+void purge_old_cached_pkgs(const char *dir_name){
+ DIR *dir;
+ struct dirent *file;
+ struct stat file_stat;
+ struct pkg_list *list;
+ sg_regex cached_pkgs_regex;
+
+ list = init_pkg_list();
+ init_regex(&cached_pkgs_regex,PKG_PARSE_REGEX);
+
+ if( (dir = opendir(dir_name)) == NULL ){
+ fprintf(stderr,_("Failed to opendir %s\n"),dir_name);
+ return;
+ }
+
+ if( chdir(dir_name) == -1 ){
+ fprintf(stderr,_("Failed to chdir: %s\n"),dir_name);
+ return;
+ }
+
+ while( (file = readdir(dir)) ){
+
+ /* make sure we don't have . or .. */
+ if( (strcmp(file->d_name,"..")) == 0 || (strcmp(file->d_name,".") == 0) )
+ continue;
+
+ /* setup file_stat struct */
+ if( (stat(file->d_name,&file_stat)) == -1)
+ continue;
+
+ /* if its a directory, recurse */
+ if( S_ISDIR(file_stat.st_mode) ){
+ purge_old_cached_pkgs(file->d_name);
+ if( (chdir("..")) == -1 ){
+ fprintf(stderr,_("Failed to chdir: %s\n"),dir_name);
+ return;
+ }
+ continue;
+ }
+
+ /* if its a package */
+ if( strstr(file->d_name,".tgz") !=NULL ){
+
+ execute_regex(&cached_pkgs_regex,file->d_name);
+
+ /* if our regex matches */
+ if( cached_pkgs_regex.reg_return == 0 ){
+ char *tmp_pkg_name,*tmp_pkg_version;
+ pkg_info_t *tmp_pkg;
+
+ tmp_pkg = init_pkg();
+ tmp_pkg_name = malloc( sizeof *tmp_pkg_name * NAME_LEN + 1);
+ tmp_pkg_version = malloc( sizeof *tmp_pkg_version * VERSION_LEN + 1);
+
+ if((cached_pkgs_regex.pmatch[1].rm_eo - cached_pkgs_regex.pmatch[1].rm_so) >
+ NAME_LEN ){
+ fprintf(stderr,"cached package name too long\n");
+ exit(1);
+ }
+ if((cached_pkgs_regex.pmatch[2].rm_eo - cached_pkgs_regex.pmatch[2].rm_so) >
+ VERSION_LEN ){
+ fprintf(stderr,"cached package version too long\n");
+ exit(1);
+ }
+
+ strncpy(
+ tmp_pkg_name,
+ file->d_name + cached_pkgs_regex.pmatch[1].rm_so,
+ cached_pkgs_regex.pmatch[1].rm_eo - cached_pkgs_regex.pmatch[1].rm_so
+ );
+ tmp_pkg_name[
+ cached_pkgs_regex.pmatch[1].rm_eo - cached_pkgs_regex.pmatch[1].rm_so
+ ] = '\0';
+ strncpy(
+ tmp_pkg_version,
+ file->d_name + cached_pkgs_regex.pmatch[2].rm_so,
+ cached_pkgs_regex.pmatch[2].rm_eo - cached_pkgs_regex.pmatch[2].rm_so
+ );
+ tmp_pkg_version[
+ cached_pkgs_regex.pmatch[2].rm_eo - cached_pkgs_regex.pmatch[2].rm_so
+ ] = '\0';
+
+ tmp_pkg = get_newest_pkg(list,tmp_pkg_name);
+ if( tmp_pkg == NULL ){
+ tmp_pkg = init_pkg();
+ strcpy(tmp_pkg->name,tmp_pkg_name);
+ strcpy(tmp_pkg->version,tmp_pkg_version);
+ add_pkg_to_pkg_list(list,tmp_pkg);
+ tmp_pkg = NULL;
+ continue;
+ }
+
+ if( cmp_pkg_versions(tmp_pkg_version,tmp_pkg->version) < 0 ){
+ unlink(file->d_name);
+ }
+ tmp_pkg = NULL;
+
+ }
+
+ }
+
+ }
+ closedir(dir);
+
+ free_regex(&cached_pkgs_regex);
+ free_pkg_list(list);
+
+}
+
diff --git a/src/package.h b/src/package.h
index 926b413..d2b4ef6 100644
--- a/src/package.h
+++ b/src/package.h
@@ -144,3 +144,6 @@ struct pkg_list *get_pkg_conflicts(struct pkg_list *avail_pkgs,struct pkg_list *
/* return list of packages required by */
struct pkg_list *is_required_by(const rc_config *global_config,struct pkg_list *avail, pkg_info_t *pkg);
+/* clean out old outdated packages in the cache */
+void purge_old_cached_pkgs(const char *dir_name);
+
diff --git a/src/transaction.c b/src/transaction.c
index c2bbb47..27199cf 100644
--- a/src/transaction.c
+++ b/src/transaction.c
@@ -224,6 +224,8 @@ int handle_transaction(const rc_config *global_config, transaction *tran){
if( remove_pkg(global_config,tran->remove_pkgs->pkgs[i]) == -1 ) exit(1);
}
+ purge_old_cached_pkgs(global_config->working_dir);
+
printf(_("Done\n"));
return 0;