/* * util_poolset_open -- (internal) open memory pool set * * On success returns 0 and a pointer to a newly allocated structure * containing the info of all the parts of the pool set and replicas. */ static int util_poolset_open(struct pool_set **setp, const char *path, size_t minsize) { LOG(3, "setp %p path %s minsize %zu", setp, path, minsize); int oerrno; int ret = 0; int fd; size_t size = 0; /* do not check minsize */ if ((fd = util_file_open(path, &size, 0, O_RDWR)) == -1) return -1; char signature[POOLSET_HDR_SIG_LEN]; ret = read(fd, signature, POOLSET_HDR_SIG_LEN); if (ret < 0) { ERR("!read %d", fd); goto err; } if (ret < POOLSET_HDR_SIG_LEN || strncmp(signature, POOLSET_HDR_SIG, POOLSET_HDR_SIG_LEN)) { LOG(4, "not a pool set header"); if (size < minsize) { ERR("size %zu smaller than %zu", size, minsize); errno = EINVAL; ret = -1; goto err; } *setp = util_poolset_single(path, size, fd, 0); if (*setp == NULL) { ret = -1; goto err; } /* do not close the file */ return 0; } ret = util_poolset_parse(path, fd, setp); if (ret != 0) goto err; ret = util_poolset_files(*setp, minsize, 0); if (ret != 0) util_poolset_close(*setp, 0); err: oerrno = errno; (void) close(fd); errno = oerrno; return ret; }
/* * util_poolset_open -- (internal) open memory pool set * * On success returns 0 and a pointer to a newly allocated structure * containing the info of all the parts of the pool set and replicas. */ static int util_poolset_open(struct pool_set **setp, const char *path, size_t minsize) { LOG(3, "setp %p path %s minsize %zu", setp, path, minsize); int oerrno; int ret = 0; int fd; size_t size = 0; /* do not check minsize */ if ((fd = util_file_open(path, &size, 0, O_RDONLY)) == -1) return -1; char signature[POOLSET_HDR_SIG_LEN]; /* * read returns ssize_t, but we know it will return value between -1 * and POOLSET_HDR_SIG_LEN (11), so we can safely cast it to int */ ret = (int)read(fd, signature, POOLSET_HDR_SIG_LEN); if (ret < 0) { ERR("!read %d", fd); goto err; } if (ret < POOLSET_HDR_SIG_LEN || strncmp(signature, POOLSET_HDR_SIG, POOLSET_HDR_SIG_LEN)) { LOG(4, "not a pool set header"); if (size < minsize) { ERR("size %zu smaller than %zu", size, minsize); errno = EINVAL; ret = -1; goto err; } /* close the file and open with O_RDWR */ (void) close(fd); size = 0; if ((fd = util_file_open(path, &size, 0, O_RDWR)) == -1) return -1; *setp = util_poolset_single(path, size, fd, 0); if (*setp == NULL) { ret = -1; goto err; } /* do not close the file */ return 0; } ret = util_poolset_parse(path, fd, setp); if (ret != 0) goto err; ret = util_poolset_files(*setp, minsize, 0); if (ret != 0) util_poolset_close(*setp, 0); err: oerrno = errno; (void) close(fd); errno = oerrno; return ret; }