From 5fa6910abe92034c1904a9c6eb0a0302fc86ca84 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 26 Aug 2008 20:45:07 +1000 Subject: [PATCH] api updates --- hacksm_ls.c | 5 +++++ hacksm_migrate.c | 7 ++++++- hacksmd.c | 5 +++++ store.h | 13 ++++++++++++- store_file.c | 47 +++++++++++++++++++++++++++++++++++++---------- 5 files changed, 65 insertions(+), 12 deletions(-) diff --git a/hacksm_ls.c b/hacksm_ls.c index bb582c1..40aaa44 100644 --- a/hacksm_ls.c +++ b/hacksm_ls.c @@ -60,6 +60,11 @@ static void hsm_init(void) printf("Unable to open HSM store - %s\n", strerror(errno)); exit(1); } + + if (hsm_store_connect(store_ctx, "/gpfs") != 0) { + printf("Failed to connect to HSM store\n"); + exit(1); + } } diff --git a/hacksm_migrate.c b/hacksm_migrate.c index 1aacfe6..61e7901 100644 --- a/hacksm_migrate.c +++ b/hacksm_migrate.c @@ -57,6 +57,11 @@ static void hsm_init(void) printf("Unable to open HSM store - %s\n", strerror(errno)); exit(1); } + + if (hsm_store_connect(store_ctx, "/gpfs") != 0) { + printf("Failed to connect to HSM store\n"); + exit(1); + } } /* @@ -168,7 +173,7 @@ static int hsm_migrate(const char *path) /* read the file data and store it away */ ofs = 0; while ((ret = dm_read_invis(dmapi.sid, hanp, hlen, dmapi.token, ofs, sizeof(buf), buf)) > 0) { - if (hsm_store_write(handle, buf, ret) != ret) { + if (hsm_store_write(handle, buf, ret) != 0) { printf("Failed to write to store for %s - %s\n", path, strerror(errno)); hsm_store_close(handle); hsm_store_remove(store_ctx, st.st_dev, st.st_ino); diff --git a/hacksmd.c b/hacksmd.c index 6c78367..d134d99 100644 --- a/hacksmd.c +++ b/hacksmd.c @@ -60,6 +60,11 @@ static void hsm_init(void) exit(1); } + if (hsm_store_connect(store_ctx, "/gpfs") != 0) { + printf("Failed to connect to HSM store\n"); + exit(1); + } + while ((ret = dm_init_service(&dmapi_version)) == -1) { if (errno != errcode) { errcode = errno; diff --git a/store.h b/store.h index 91b3010..9049305 100644 --- a/store.h +++ b/store.h @@ -13,6 +13,17 @@ struct hsm_store_context *hsm_store_init(void); struct hsm_store_handle *hsm_store_open(struct hsm_store_context *, dev_t device, ino_t inode, bool readonly); +/* + return an error message for the last failed operation + */ +const char *hsm_store_errmsg(struct hsm_store_context *ctx); + + +/* + connect to the store + */ +int hsm_store_connect(struct hsm_store_context *ctx, const char *fsname); + /* read from an open handle */ @@ -21,7 +32,7 @@ size_t hsm_store_read(struct hsm_store_handle *, uint8_t *buf, size_t n); /* write to an open handle */ -size_t hsm_store_write(struct hsm_store_handle *, uint8_t *buf, size_t n); +int hsm_store_write(struct hsm_store_handle *, uint8_t *buf, size_t n); /* close a handle diff --git a/store_file.c b/store_file.c index 3a8f162..ac2411b 100644 --- a/store_file.c +++ b/store_file.c @@ -11,6 +11,7 @@ struct hsm_store_context { const char *basepath; + const char *errmsg; }; struct hsm_store_handle { @@ -25,23 +26,41 @@ struct hsm_store_handle { struct hsm_store_context *hsm_store_init(void) { struct hsm_store_context *ctx; - struct stat st; - ctx = malloc(sizeof(struct hsm_store_context)); + ctx = calloc(1, sizeof(struct hsm_store_context)); if (ctx == NULL) { errno = ENOMEM; return NULL; } + ctx->errmsg = ""; + + return ctx; +} + +/* + return an error message for the last failed operation + */ +const char *hsm_store_errmsg(struct hsm_store_context *ctx) +{ + return ctx->errmsg; +} + +/* + connect to the store + */ +int hsm_store_connect(struct hsm_store_context *ctx, const char *fsname) +{ + struct stat st; + ctx->basepath = HSM_STORE_PATH; - if (stat(ctx->basepath, &st) != 0 || - !S_ISDIR(st.st_mode)) { - errno = EINVAL; - free(ctx); - return NULL; + + if (stat(ctx->basepath, &st) != 0 || !S_ISDIR(st.st_mode)) { + ctx->errmsg = "Invalid store path"; + return -1; } - return ctx; + return 0; } /* @@ -80,11 +99,13 @@ struct hsm_store_handle *hsm_store_open(struct hsm_store_context *ctx, fname = store_fname(ctx, device, inode); if (fname == NULL) { + ctx->errmsg = "Unable to allocate store filename"; return NULL; } h = malloc(sizeof(struct hsm_store_handle)); if (h == NULL) { + ctx->errmsg = "Unable to allocate store handle"; errno = ENOMEM; free(fname); return NULL; @@ -102,6 +123,7 @@ struct hsm_store_handle *hsm_store_open(struct hsm_store_context *ctx, free(fname); if (h->fd == -1) { + ctx->errmsg = "Unable to open store file"; free(h); return NULL; } @@ -139,9 +161,14 @@ size_t hsm_store_read(struct hsm_store_handle *h, uint8_t *buf, size_t n) /* write to a stored file */ -size_t hsm_store_write(struct hsm_store_handle *h, uint8_t *buf, size_t n) +int hsm_store_write(struct hsm_store_handle *h, uint8_t *buf, size_t n) { - return write(h->fd, buf, n); + size_t nwritten = write(h->fd, buf, n); + if (nwritten != n) { + h->ctx->errmsg = "write failed"; + return -1; + } + return 0; } /* -- 2.34.1