diff -r fda83d91e600 -r ca75725e6849 librazor/root.c --- a/librazor/root.c Fri Jul 03 19:06:29 2009 +0100 +++ b/librazor/root.c Wed Apr 28 11:59:02 2010 +0100 @@ -42,7 +42,14 @@ #endif static const char system_repo_filename[] = "system.rzdb"; -static const char next_repo_filename[] = "system-next.rzdb"; +/* + * system_lock_filename is chosen to be the same as the pre v0.3 + * next_repo_filename. This means that once a system has been + * updated by a v0.3+ copy of razor all pre v0.3 versions of razor + * will see the system as permenantly locked. + */ +static const char system_lock_filename[] = "system-next.rzdb"; +static const char system_tmp_filename[] = "system.tmp"; #ifdef MSWIN_API #define RAZOR_ROOT_PATH NULL #else @@ -129,6 +136,7 @@ razor_root_open(const char *root) { struct razor_root *image; + char lock_path[PATH_MAX]; assert (root != NULL); @@ -137,21 +145,28 @@ if (image == NULL) return NULL; - /* Create the new next repo file up front to ensure exclusive - * access. */ + image->system = razor_set_create_without_root(); + if (image->system == NULL) { + free(image); + return NULL; + } + + snprintf(lock_path, sizeof lock_path, + "%s%s/%s", root, razor_root_path, system_lock_filename); + + if (razor_set_aquire_lock(image->system, lock_path, 1) < 0) { + razor_set_destroy(image->system); + free(image); + return NULL; + } + snprintf(image->new_path, sizeof image->new_path, - "%s%s/%s", root, razor_root_path, next_repo_filename); + "%s%s/%s", root, razor_root_path, system_tmp_filename); image->fd = open(image->new_path, - O_CREAT | O_WRONLY | O_TRUNC | O_EXCL | O_BINARY, + O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0666); if (image->fd < 0) { - fprintf(stderr, "failed to get lock file, " - "maybe previous operation crashed?\n"); - - /* FIXME: Use fcntl advisory locking on the system - * package set file to figure out whether previous - * operation crashed or is still in progress. */ - + razor_set_destroy(image->system); free(image); return NULL; } @@ -159,10 +174,10 @@ snprintf(image->path, sizeof image->path, "%s%s/%s", root, razor_root_path, system_repo_filename); - image->system = razor_set_open(image->path); - if (image->system == NULL) { + if (razor_set_bind_sections(image->system, image->path)) { + close(image->fd); unlink(image->new_path); - close(image->fd); + razor_set_destroy(image->system); free(image); return NULL; } @@ -174,14 +189,31 @@ razor_root_open_read_only(const char *root) { char path[PATH_MAX]; + struct razor_set *set; assert (root != NULL); razor_root_init(); + set = razor_set_create_without_root(); + if (set == NULL) + return NULL; + + snprintf(path, sizeof path, + "%s%s/%s", root, razor_root_path, system_lock_filename); + if (razor_set_aquire_lock(set, path, 0) < 0) { + razor_set_destroy(set); + return NULL; + } + snprintf(path, sizeof path, "%s%s/%s", root, razor_root_path, system_repo_filename); - return razor_set_open(path); + if (razor_set_bind_sections(set, path)) { + razor_set_destroy(set); + return NULL; + } + + return set; } RAZOR_EXPORT struct razor_set *