From: Liu Yuan <tailai.ly at taobao.com> Signed-off-by: Liu Yuan <tailai.ly at taobao.com> --- sheep/Makefile.am | 3 +- sheep/sheepfs/shadow_file.c | 130 +++++++++++++++++++++++++++++++++++++++++++ sheep/sheepfs/sheepfs.h | 12 ++++ 3 files changed, 144 insertions(+), 1 deletions(-) create mode 100644 sheep/sheepfs/shadow_file.c diff --git a/sheep/Makefile.am b/sheep/Makefile.am index 9ad9492..09cb091 100644 --- a/sheep/Makefile.am +++ b/sheep/Makefile.am @@ -47,7 +47,8 @@ sheep_SOURCES += trace/trace.c trace/mcount.S trace/stabs.c trace/graph.c endif if BUILD_SHEEPFS -sheep_SOURCES += sheepfs/core.c sheepfs/cluster.c sheepfs/VDI.c +sheep_SOURCES += sheepfs/core.c sheepfs/cluster.c sheepfs/VDI.c \ + sheepfs/shadow_file.c endif sheep_LDADD = ../lib/libsheepdog.a -lpthread \ diff --git a/sheep/sheepfs/shadow_file.c b/sheep/sheepfs/shadow_file.c new file mode 100644 index 0000000..6f7d141 --- /dev/null +++ b/sheep/sheepfs/shadow_file.c @@ -0,0 +1,130 @@ +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <attr/xattr.h> + +#include "util.h" +#include "logger.h" +#include "sheepfs.h" + +int shadow_file_read(const char *path, char *buf, size_t size, off_t offset) +{ + char p[PATH_MAX]; + int fd, len; + + sprintf(p, "%s%s", sheepfs_shadow, path); + fd = open(p, O_RDONLY); + if (fd < 0) { + eprintf("%m\n"); + return -errno; + } + len = xpread(fd, buf, size, offset); + close(fd); + return len; +} + +size_t shadow_file_fill(const char *path, char *buf, size_t size) +{ + char p[PATH_MAX]; + int fd; + size_t len = 0; + + sprintf(p, "%s%s", sheepfs_shadow, path); + fd = open(p, O_WRONLY | O_TRUNC); + if (fd < 0) { + eprintf("%m\n"); + return 0; + } + len = xwrite(fd, buf, size); + if (len != size) { + eprintf("failed to write\n"); + len = 0; + } + close(fd); + return len; +} + +int shadow_file_create(const char *path) +{ + char p[PATH_MAX]; + int fd; + sprintf(p, "%s%s", sheepfs_shadow, path); + fd = creat(p, 0644); + if (fd < 0) { + if (errno != EEXIST) { + eprintf("%m\n"); + return -1; + } + } + close(fd); + return 0; +} + +int shadow_dir_create(const char *path) +{ + char p[PATH_MAX]; + + sprintf(p, "%s%s", sheepfs_shadow, path); + if (mkdir(p, 0755) < 0) { + if (errno != EEXIST) { + eprintf("%m\n"); + return -1; + } + } + return 0; +} + +int shadow_file_setxattr(const char *path, const char *name, + const void *value, size_t size) +{ + char p[PATH_MAX]; + + sprintf(p, "%s%s", sheepfs_shadow, path); + if (setxattr(p, name, value, size, 0) < 0) { + eprintf("%m\n"); + return -1; + } + return 0; +} + +int shadow_file_getxattr(const char *path, const char *name, + void *value, size_t size) +{ + char p[PATH_MAX]; + + sprintf(p, "%s%s", sheepfs_shadow, path); + if (getxattr(p, name, value, size) < 0) { + eprintf("%m\n"); + return -1; + } + return 0; +} + +int shadow_file_delete(const char *path) +{ + char p[PATH_MAX]; + + sprintf(p, "%s%s", sheepfs_shadow, path); + if (unlink(p) < 0) { + if (errno != ENOENT) { + eprintf("%m\n"); + return -1; + } + } + return 0; +} + +int shadow_file_exsit(const char *path) +{ + char p[PATH_MAX]; + int fd; + sprintf(p, "%s%s", sheepfs_shadow, path); + fd = open(p, O_RDWR); + if (fd < 0) + return 0; + close(fd); + return 1; +} diff --git a/sheep/sheepfs/sheepfs.h b/sheep/sheepfs/sheepfs.h index b06b91b..85c95e3 100644 --- a/sheep/sheepfs/sheepfs.h +++ b/sheep/sheepfs/sheepfs.h @@ -14,6 +14,18 @@ extern struct strbuf *sheepfs_run_cmd(const char *command); extern int sheepfs_init(const char *dir); extern int sheepfs_set_op(const char *path, unsigned opcode); +/* shadow_file.c */ +extern size_t shadow_file_fill(const char *path, char *buf, size_t size); +extern int shadow_file_read(const char *, char *buf, size_t size, off_t); +extern int shadow_dir_create(const char *path); +extern int shadow_file_create(const char *path); +extern int shadow_file_setxattr(const char *path, const char *name, + const void *value, size_t size); +extern int shadow_file_getxattr(const char *path, const char *name, + void *value, size_t size); +extern int shadow_file_delete(const char *path); +extern int shadow_file_exsit(const char *path); + /* cluster.c */ extern int cluster_info_read(const char *path, char *buf, size_t size, off_t); extern size_t cluster_info_get_size(const char *path); -- 1.7.8.2 |