Fix bugs when removing files and directories
authorJ. Ali Harlow <ali@juiblex.co.uk>
Wed Jul 08 22:14:16 2009 +0100 (2009-07-08)
changeset 3775549419824b4
parent 376 d15a16347c77
child 378 ee438b1e94c0
Fix bugs when removing files and directories
librazor/iterator.c
librazor/razor-internal.h
librazor/razor.c
librazor/razor.h
librazor/types/list.c
librazor/types/types.h
librazor/util.c
src/main.c
     1.1 --- a/librazor/iterator.c	Tue Jul 07 22:50:22 2009 +0100
     1.2 +++ b/librazor/iterator.c	Wed Jul 08 22:14:16 2009 +0100
     1.3 @@ -231,7 +231,8 @@
     1.4  }
     1.5  
     1.6  RAZOR_EXPORT struct razor_file_iterator *
     1.7 -razor_file_iterator_create(struct razor_set *set, struct razor_package *package)
     1.8 +razor_file_iterator_create(struct razor_set *set, struct razor_package *package,
     1.9 +			   int post_order)
    1.10  {
    1.11  	struct razor_file_iterator *fi;
    1.12  
    1.13 @@ -240,7 +241,11 @@
    1.14  
    1.15  	fi = zalloc(sizeof *fi);
    1.16  	fi->set = set;
    1.17 -	fi->index = list_first(&package->files, &set->file_pool);
    1.18 +	fi->post_order = post_order;
    1.19 +	if (post_order)
    1.20 +		fi->index = list_last(&package->files, &set->file_pool);
    1.21 +	else
    1.22 +		fi->index = list_first(&package->files, &set->file_pool);
    1.23  	array_init(&fi->path);
    1.24  
    1.25  	return fi;
    1.26 @@ -277,7 +282,10 @@
    1.27  					strcpy(fi->path.data, "/");
    1.28  				}
    1.29  				*name = fi->path.data;
    1.30 -				fi->index = list_next(fi->index);
    1.31 +				if (fi->post_order)
    1.32 +					fi->index = list_prev(fi->index);
    1.33 +				else
    1.34 +					fi->index = list_next(fi->index);
    1.35  				return 1;
    1.36  			}
    1.37  		} while (!((e++)->flags & RAZOR_ENTRY_LAST));
     2.1 --- a/librazor/razor-internal.h	Tue Jul 07 22:50:22 2009 +0100
     2.2 +++ b/librazor/razor-internal.h	Wed Jul 08 22:14:16 2009 +0100
     2.3 @@ -167,6 +167,7 @@
     2.4  	struct razor_set *set;
     2.5  	struct array path;
     2.6  	struct list *index;
     2.7 +	int post_order;
     2.8  };
     2.9  
    2.10  struct razor_entry *
    2.11 @@ -203,6 +204,7 @@
    2.12  void razor_rpm_get_details_varg(struct razor_rpm *rpm, va_list args);
    2.13  
    2.14  int razor_create_dir(const char *root, const char *path);
    2.15 +int razor_remove(const char *path);
    2.16  int razor_write(int fd, const void *data, size_t size);
    2.17  
    2.18  void *razor_file_get_contents(const char *filename, size_t *length);
     3.1 --- a/librazor/razor.c	Tue Jul 07 22:50:22 2009 +0100
     3.2 +++ b/librazor/razor.c	Wed Jul 08 22:14:16 2009 +0100
     3.3 @@ -498,25 +498,19 @@
     3.4  		environment_add_variable(&env, buffer, prefix);
     3.5  		link = list_next(link);
     3.6  	}
     3.7 +	environment_set(&env);
     3.8  
     3.9  	razor_package_get_details(set, package,
    3.10  				  RAZOR_DETAIL_PREUNPROG, &program,
    3.11  				  RAZOR_DETAIL_PREUN, &script,
    3.12  				  RAZOR_DETAIL_LAST);
    3.13  
    3.14 -	environment_set(&env);
    3.15  	retval = razor_run_script(root, RAZOR_PROPERTY_PREUN, program, script,
    3.16  				  install_count);
    3.17 -	environment_unset(&env);
    3.18  
    3.19 -	if (retval) {
    3.20 -		environment_release(&env);
    3.21 -		return -1;
    3.22 -	}
    3.23 +	fi = razor_file_iterator_create(set, package, 1);
    3.24  
    3.25 -	fi = razor_file_iterator_create(set, package);
    3.26 -
    3.27 -	while (!retval && razor_file_iterator_next(fi, &name)) {
    3.28 +	while (razor_file_iterator_next(fi, &name)) {
    3.29  		pi = razor_package_iterator_create_for_file(set, name);
    3.30  		count = 0;
    3.31  		while (razor_package_iterator_next(pi, &p, RAZOR_DETAIL_LAST))
    3.32 @@ -524,27 +518,25 @@
    3.33  		razor_package_iterator_destroy(pi);
    3.34  		if (count <= 1) {
    3.35  			snprintf(buffer, sizeof buffer, "%s%s", root, name);
    3.36 -			retval = remove(buffer);
    3.37 +			if (razor_remove(buffer) && errno != ENOENT) {
    3.38 +				perror(name);
    3.39 +				retval = -1;
    3.40 +			}
    3.41  		}
    3.42  	}
    3.43  
    3.44  	razor_file_iterator_destroy(fi);
    3.45  
    3.46 -	if (retval) {
    3.47 -		environment_release(&env);
    3.48 -		return retval;
    3.49 -	}
    3.50 -
    3.51  	razor_package_get_details(set, package,
    3.52  				  RAZOR_DETAIL_POSTUNPROG, &program,
    3.53  				  RAZOR_DETAIL_POSTUN, &script,
    3.54  				  RAZOR_DETAIL_LAST);
    3.55  
    3.56 -	environment_set(&env);
    3.57 -	retval = razor_run_script(root, RAZOR_PROPERTY_POSTUN, program, script,
    3.58 -				  install_count);
    3.59 +	if (razor_run_script(root, RAZOR_PROPERTY_POSTUN, program, script,
    3.60 +			     install_count))
    3.61 +		retval = -1;
    3.62 +
    3.63  	environment_unset(&env);
    3.64 -
    3.65  	environment_release(&env);
    3.66  
    3.67  	return retval;
    3.68 @@ -700,7 +692,7 @@
    3.69  	assert (set != NULL);
    3.70  	assert (package != NULL);
    3.71  
    3.72 -	fi = razor_file_iterator_create(set, package);
    3.73 +	fi = razor_file_iterator_create(set, package, 0);
    3.74  
    3.75  	while (razor_file_iterator_next(fi, &name))
    3.76  		printf("%s\n", name);
     4.1 --- a/librazor/razor.h	Tue Jul 07 22:50:22 2009 +0100
     4.2 +++ b/librazor/razor.h	Wed Jul 08 22:14:16 2009 +0100
     4.3 @@ -189,7 +189,7 @@
     4.4  struct razor_file_iterator;
     4.5  struct razor_file_iterator *
     4.6  razor_file_iterator_create(struct razor_set *set,
     4.7 -			   struct razor_package *package);
     4.8 +			   struct razor_package *package, int post_order);
     4.9  int razor_file_iterator_next(struct razor_file_iterator *fi,
    4.10  			     const char **name);
    4.11  void razor_file_iterator_destroy(struct razor_file_iterator *fi);
     5.1 --- a/librazor/types/list.c	Tue Jul 07 22:50:22 2009 +0100
     5.2 +++ b/librazor/types/list.c	Wed Jul 08 22:14:16 2009 +0100
     5.3 @@ -23,10 +23,10 @@
     5.4  
     5.5  #include "types.h"
     5.6  
     5.7 -/* RAZOR_IMMEDIATE and RAZOR_ENTRY_LAST must have the same value */
     5.8 -#define RAZOR_ENTRY_LAST 0x80
     5.9 -#define RAZOR_IMMEDIATE  0x80
    5.10 -#define RAZOR_EMPTY_LIST 0xff
    5.11 +#define RAZOR_ENTRY_LAST	0x80
    5.12 +#define RAZOR_ENTRY_FIRST	0x40
    5.13 +#define RAZOR_IMMEDIATE		(RAZOR_ENTRY_LAST | RAZOR_ENTRY_FIRST)
    5.14 +#define RAZOR_EMPTY_LIST	0xff
    5.15  
    5.16  void
    5.17  list_set_empty(struct list_head *head)
    5.18 @@ -61,7 +61,8 @@
    5.19  
    5.20  	p = array_add(pool, items->size);
    5.21  	memcpy(p, items->data, items->size);
    5.22 -	p[items->size / sizeof *p - 1].flags = RAZOR_ENTRY_LAST;
    5.23 +	p->flags = RAZOR_ENTRY_FIRST;
    5.24 +	p[items->size / sizeof *p - 1].flags |= RAZOR_ENTRY_LAST;
    5.25  	list_set_ptr(head, p - (struct list *) pool->data);
    5.26  }
    5.27  
    5.28 @@ -77,13 +78,38 @@
    5.29  }
    5.30  
    5.31  struct list *
    5.32 +list_last(struct list_head *head, struct array *pool)
    5.33 +{
    5.34 +	struct list *list;
    5.35 +
    5.36 +	if (head->flags == RAZOR_EMPTY_LIST)
    5.37 +		return NULL;
    5.38 +	else if (head->flags == RAZOR_IMMEDIATE)
    5.39 +		return (struct list *) head;
    5.40 +	else {
    5.41 +		list = (struct list *) pool->data + head->list_ptr;
    5.42 +		while((list->flags & RAZOR_ENTRY_LAST) == 0)
    5.43 +			list++;
    5.44 +		return list;
    5.45 +	}
    5.46 +}
    5.47 +
    5.48 +struct list *
    5.49  list_next(struct list *list)
    5.50  {
    5.51 -	if (list->flags)
    5.52 +	if (list->flags & RAZOR_ENTRY_LAST)
    5.53  		return NULL;
    5.54  	return ++list;
    5.55  }
    5.56  
    5.57 +struct list *
    5.58 +list_prev(struct list *list)
    5.59 +{
    5.60 +	if (list->flags & RAZOR_ENTRY_FIRST)
    5.61 +		return NULL;
    5.62 +	return --list;
    5.63 +}
    5.64 +
    5.65  void
    5.66  list_remap_pool(struct array *pool, uint32_t *map)
    5.67  {
     6.1 --- a/librazor/types/types.h	Tue Jul 07 22:50:22 2009 +0100
     6.2 +++ b/librazor/types/types.h	Wed Jul 08 22:14:16 2009 +0100
     6.3 @@ -53,7 +53,9 @@
     6.4  void list_set_array(struct list_head *head, struct array *pool, struct array *items, int force_indirect);
     6.5  
     6.6  struct list *list_first(struct list_head *head, struct array *pool);
     6.7 +struct list *list_last(struct list_head *head, struct array *pool);
     6.8  struct list *list_next(struct list *list);
     6.9 +struct list *list_prev(struct list *list);
    6.10  
    6.11  void list_remap_pool(struct array *pool, uint32_t *map);
    6.12  void list_remap_head(struct list_head *list, uint32_t *map);
     7.1 --- a/librazor/util.c	Tue Jul 07 22:50:22 2009 +0100
     7.2 +++ b/librazor/util.c	Wed Jul 08 22:14:16 2009 +0100
     7.3 @@ -31,6 +31,7 @@
     7.4  #include <unistd.h>
     7.5  #include <fcntl.h>
     7.6  #ifdef MSWIN_API
     7.7 +#include <windows.h>
     7.8  #include <direct.h>
     7.9  #endif
    7.10  #if HAVE_SYS_MMAN_H
    7.11 @@ -122,6 +123,39 @@
    7.12  }
    7.13  
    7.14  int
    7.15 +razor_remove(const char *path)
    7.16 +{
    7.17 +#ifdef MSWIN_API
    7.18 +	DWORD err;
    7.19 +
    7.20 +	if (DeleteFile(path))
    7.21 +		return 0;
    7.22 +
    7.23 +	err = GetLastError();
    7.24 +	if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND)
    7.25 +		return 0;
    7.26 +
    7.27 +	if (SetFileAttributes(path, FILE_ATTRIBUTE_NORMAL) && DeleteFile(path))
    7.28 +		return 0;
    7.29 +
    7.30 +	if (RemoveDirectory(path) || GetLastError() == ERROR_DIR_NOT_EMPTY)
    7.31 +		return 0;
    7.32 +
    7.33 +	/*
    7.34 +	 * It would be tempting to use:
    7.35 +	 * 	MoveFileEx(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT)
    7.36 +	 * but unless we can guarantee that the system will be rebooted
    7.37 +	 * before we (or some other application) write another file with the
    7.38 +	 * same path, this is likely to cause more problems than it solves.
    7.39 +	 */
    7.40 +
    7.41 +	/* Use remove() as a fallback so that errno is set appropriately */
    7.42 +#endif
    7.43 +
    7.44 +	return remove(path);
    7.45 +}
    7.46 +
    7.47 +int
    7.48  razor_write(int fd, const void *data, size_t size)
    7.49  {
    7.50  	size_t rest;
     8.1 --- a/src/main.c	Tue Jul 07 22:50:22 2009 +0100
     8.2 +++ b/src/main.c	Wed Jul 08 22:14:16 2009 +0100
     8.3 @@ -860,7 +860,7 @@
     8.4  						    name, flags, version);
     8.5  		razor_property_iterator_destroy(prop_iter);
     8.6  
     8.7 -		file_iter = razor_file_iterator_create(set, package);
     8.8 +		file_iter = razor_file_iterator_create(set, package, 0);
     8.9  		while (razor_file_iterator_next(file_iter, &name)) {
    8.10  			name = razor_relocations_apply(relocations, name);
    8.11  			razor_importer_add_file(importer, name);