diff options
author | midipix <writeonce@midipix.org> | 2020-04-30 01:26:47 -0400 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2020-05-23 05:59:02 +0000 |
commit | e0fd2a143d1ff76373786ced79c2af7cbcb28025 (patch) | |
tree | a1c2c238cf1d5af386cbb568ea61a54bd3183b91 | |
parent | 845037353c2db876f004f024b5b50cc18cb70a7a (diff) | |
download | tpax-e0fd2a143d1ff76373786ced79c2af7cbcb28025.tar.bz2 tpax-e0fd2a143d1ff76373786ced79c2af7cbcb28025.tar.xz |
library api: tpax_file_create_memory_snapshot(): initial implementation.
-rw-r--r-- | include/tpax/tpax.h | 5 | ||||
-rw-r--r-- | project/common.mk | 1 | ||||
-rw-r--r-- | src/logic/tpax_file_create_memory_snapshot.c | 101 | ||||
-rw-r--r-- | src/output/tpax_output_error.c | 2 |
4 files changed, 109 insertions, 0 deletions
diff --git a/include/tpax/tpax.h b/include/tpax/tpax.h index c39f721..8d6183b 100644 --- a/include/tpax/tpax.h +++ b/include/tpax/tpax.h @@ -66,6 +66,8 @@ enum tpax_custom_error { TPAX_ERR_NULL_CONTEXT, TPAX_ERR_BAD_DATA, TPAX_ERR_FORBIDDEN_PATH, + TPAX_ERR_FILE_CHANGED, + TPAX_ERR_REGION_SIZE, TPAX_ERR_CAP, }; @@ -161,6 +163,9 @@ tpax_api int tpax_output_error_vector (const struct tpax_driver_ctx *); tpax_api int tpax_init_ustar_header (const struct tpax_driver_ctx *, const char *, const struct stat *, const char *, struct tpax_ustar_header *); +tpax_api int tpax_file_create_memory_snapshot (const struct tpax_driver_ctx *, const char *, + const struct stat *, void *); + /* package info */ tpax_api const struct tpax_source_version * tpax_source_version(void); diff --git a/project/common.mk b/project/common.mk index 14d6ec8..da9fa26 100644 --- a/project/common.mk +++ b/project/common.mk @@ -5,6 +5,7 @@ API_SRCS = \ src/helper/tpax_path_copy.c \ src/helper/tpax_stat_compare.c \ src/logic/tpax_init_ustar_header.c \ + src/logic/tpax_file_create_memory_snapshot.c \ src/output/tpax_output_error.c \ src/skin/tpax_skin_default.c \ diff --git a/src/logic/tpax_file_create_memory_snapshot.c b/src/logic/tpax_file_create_memory_snapshot.c new file mode 100644 index 0000000..7db21ea --- /dev/null +++ b/src/logic/tpax_file_create_memory_snapshot.c @@ -0,0 +1,101 @@ +/******************************************************/ +/* tpax: a topological pax implementation */ +/* Copyright (C) 2020 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.TPAX. */ +/******************************************************/ + +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <errno.h> +#include <grp.h> +#include <pwd.h> +#include <sys/stat.h> + +#include <tpax/tpax.h> +#include <tpax/tpax_specs.h> +#include "tpax_driver_impl.h" +#include "tpax_errinfo_impl.h" + +#ifndef ssizeof +#define ssizeof(x) (ssize_t)(sizeof(x)) +#endif + +int tpax_file_create_memory_snapshot( + const struct tpax_driver_ctx * dctx, + const char * path, + const struct stat * srcst, + void * addr) +{ + int fd; + char * ch; + char * cap; + ssize_t nread; + struct stat dstst; + + /* record errors */ + tpax_driver_set_ectx( + dctx,0,path); + + /* memory snapshot internal limit */ + if (srcst->st_size >= 0x80000000) + return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_REGION_SIZE); + + /* open */ + fd = openat( + tpax_driver_fdcwd(dctx),path, + O_CLOEXEC|O_NOCTTY|O_NOFOLLOW); + + if (fd < 0) + return TPAX_SYSTEM_ERROR(dctx); + + /* stat compare */ + if ((fstat(fd,&dstst)) < 0) { + close(fd); + return TPAX_SYSTEM_ERROR(dctx); + + } else if (tpax_stat_compare(srcst,&dstst)) { + close(fd); + return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FILE_CHANGED); + } + + /* read loop */ + ch = addr; + cap = &ch[srcst->st_size]; + + while (ch < cap) { + nread = read(fd,ch,cap-ch); + + while ((nread < 0) && (errno == EINTR)) + nread = read(fd,ch,cap-ch); + + if (nread < 0) { + close(fd); + return TPAX_SYSTEM_ERROR(dctx); + + } else if (nread == 0) { + close(fd); + return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FLOW_ERROR); + + } else { + ch += nread; + } + } + + /* stat compare */ + if ((fstat(fd,&dstst)) < 0) { + close(fd); + return TPAX_SYSTEM_ERROR(dctx); + + } else if (tpax_stat_compare(srcst,&dstst)) { + close(fd); + return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FILE_CHANGED); + } + + /* yay */ + close(fd); + + return 0; +} diff --git a/src/output/tpax_output_error.c b/src/output/tpax_output_error.c index 69c4938..de09f21 100644 --- a/src/output/tpax_output_error.c +++ b/src/output/tpax_output_error.c @@ -26,6 +26,8 @@ static const char * const tpax_error_strings[TPAX_ERR_CAP] = { [TPAX_ERR_FLEE_ERROR] = "flees and bugs and cats and mice", [TPAX_ERR_NULL_CONTEXT] = "null driver or unit context", [TPAX_ERR_BAD_DATA] = "corrupt or wrong daata", + [TPAX_ERR_FILE_CHANGED] = "file has changed", + [TPAX_ERR_REGION_SIZE] = "file too large for a memory snapshot", }; static const char * tpax_output_error_header(const struct tpax_error_info * erri) |