diff --git a/lib/device/dev-cache.c b/lib/device/dev-cache.c index a87042738..5a5daf112 100644 --- a/lib/device/dev-cache.c +++ b/lib/device/dev-cache.c @@ -120,28 +120,6 @@ static int _insert_dev(const char *path, dev_t d) return 1; } -/* - * return a new path for the destination of the link. - */ -static char *_follow_link(const char *path, struct stat *info) -{ - char buffer[PATH_MAX + 1]; - int n; - n = readlink(path, buffer, sizeof(buffer) - 1); - - if (n <= 0) - return NULL; - - buffer[n] = '\0'; - - if (stat(buffer, info) < 0) { - log_sys_very_verbose("stat", buffer); - return NULL; - } - - return pool_strdup(_cache.mem, buffer); -} - static char *_join(const char *dir, const char *name) { int len = strlen(dir) + strlen(name) + 2; @@ -208,7 +186,6 @@ static int _insert_dir(const char *dir) static int _insert(const char *path, int rec) { struct stat info; - char *actual_path; int r = 0; if (stat(path, &info) < 0) { @@ -216,22 +193,6 @@ static int _insert(const char *path, int rec) return 0; } - /* follow symlinks if neccessary. */ - if (S_ISLNK(info.st_mode)) { - log_debug("%s: Following symbolic link", path); - if (!(actual_path = _follow_link(path, &info))) { - stack; - return 0; - } - - if (stat(actual_path, &info) < 0) { - log_sys_very_verbose("stat", actual_path); - return 0; - } - - dbg_free(actual_path); - } - if (S_ISDIR(info.st_mode)) { /* add a directory */ if (rec) r = _insert_dir(path); diff --git a/lib/device/dev-io.c b/lib/device/dev-io.c index 5fac6d5ea..dece35c67 100644 --- a/lib/device/dev-io.c +++ b/lib/device/dev-io.c @@ -64,19 +64,30 @@ int dev_get_sectsize(struct device *dev, uint32_t *size) int dev_open(struct device *dev, int flags) { + struct stat buf; const char *name = dev_name(dev); - /* FIXME Check flags (eg is write now reqd?) */ if (dev->fd >= 0) { log_error("Device '%s' has already been opened", name); return 0; } + if ((stat(name, &buf) < 0) || (buf.st_rdev != dev->dev)) { + log_error("%s: stat failed: Has device name changed?", name); + return 0; + } + if ((dev->fd = open(name, flags)) < 0) { log_sys_error("open", name); return 0; } + if ((fstat(dev->fd, &buf) < 0) || (buf.st_rdev != dev->dev)) { + log_error("%s: fstat failed: Has device name changed?", name); + dev_close(dev->fd); + return 0; + } + return 1; }