diff --git a/include/tpax/tpax.h b/include/tpax/tpax.h index 8d6183b..332bc67 100644 --- a/include/tpax/tpax.h +++ b/include/tpax/tpax.h @@ -166,6 +166,9 @@ tpax_api int tpax_init_ustar_header (const struct tpax_driver_ctx *, const c tpax_api int tpax_file_create_memory_snapshot (const struct tpax_driver_ctx *, const char *, const struct stat *, void *); +tpax_api int tpax_file_create_tmpfs_snapshot (const struct tpax_driver_ctx *, const char *, + const struct stat *); + /* package info */ tpax_api const struct tpax_source_version * tpax_source_version(void); diff --git a/project/common.mk b/project/common.mk index 1725163..fcca006 100644 --- a/project/common.mk +++ b/project/common.mk @@ -6,6 +6,7 @@ API_SRCS = \ src/helper/tpax_stat_compare.c \ src/logic/tpax_init_ustar_header.c \ src/logic/tpax_file_create_memory_snapshot.c \ + src/logic/tpax_file_create_tmpfs_snapshot.c \ src/output/tpax_output_error.c \ src/skin/tpax_skin_default.c \ diff --git a/src/logic/tpax_file_create_tmpfs_snapshot.c b/src/logic/tpax_file_create_tmpfs_snapshot.c new file mode 100644 index 0000000..3bb48b7 --- /dev/null +++ b/src/logic/tpax_file_create_tmpfs_snapshot.c @@ -0,0 +1,133 @@ +/******************************************************/ +/* tpax: a topological pax implementation */ +/* Copyright (C) 2020 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.TPAX. */ +/******************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include "tpax_driver_impl.h" +#include "tpax_tmpfile_impl.h" +#include "tpax_errinfo_impl.h" + +#ifndef ssizeof +#define ssizeof(x) (ssize_t)(sizeof(x)) +#endif + +int tpax_file_create_tmpfs_snapshot( + const struct tpax_driver_ctx * dctx, + const char * path, + const struct stat * srcst) +{ + int fdsrc; + int fdtmp; + char * ch; + ssize_t nread; + ssize_t nbytes; + ssize_t ret; + void * buf; + size_t buflen; + struct stat dstst; + char sbuf[2048]; + + /* record errors */ + tpax_driver_set_ectx( + dctx,0,path); + + /* tmpfile */ + if ((fdtmp = tpax_tmpfile()) < 0) + return TPAX_SYSTEM_ERROR(dctx); + + /* buffer */ + if (!(buf = tpax_get_driver_anon_map_addr(dctx,&buflen))) { + buf = sbuf; + buflen = sizeof(sbuf); + } + + /* open */ + fdsrc = openat( + tpax_driver_fdcwd(dctx),path, + O_CLOEXEC|O_NOCTTY|O_NOFOLLOW); + + if (fdsrc < 0) { + close(fdtmp); + return TPAX_SYSTEM_ERROR(dctx); + } + + /* stat compare */ + if ((fstat(fdsrc,&dstst)) < 0) { + close(fdsrc); + close(fdtmp); + return TPAX_SYSTEM_ERROR(dctx); + + } else if (tpax_stat_compare(srcst,&dstst)) { + close(fdsrc); + close(fdtmp); + return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FILE_CHANGED); + } + + /* read/write loop */ + for (nread=0; nreadst_size; ) { + nbytes = read(fdsrc,buf,buflen); + + while ((nbytes < 0) && (errno == EINTR)) + nbytes = read(fdsrc,buf,buflen); + + if (nbytes < 0) { + close(fdsrc); + close(fdtmp); + return TPAX_SYSTEM_ERROR(dctx); + + } else if (nbytes == 0) { + close(fdsrc); + close(fdtmp); + return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FLOW_ERROR); + + } else { + nread += nbytes; + } + + for (ch=buf; nbytes; ch+=ret) { + ret = write(fdtmp,ch,nbytes); + + while ((ret < 0) && (errno == EINTR)) + ret = write(fdtmp,ch,nbytes); + + if (ret < 0) { + close(fdsrc); + close(fdtmp); + return TPAX_SYSTEM_ERROR(dctx); + + } else { + nbytes -= ret; + } + } + } + + /* stat compare */ + if ((fstat(fdsrc,&dstst)) < 0) { + close(fdsrc); + close(fdtmp); + return TPAX_SYSTEM_ERROR(dctx); + + } else if (tpax_stat_compare(srcst,&dstst)) { + close(fdsrc); + close(fdtmp); + return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FILE_CHANGED); + } + + /* yay */ + close(fdsrc); + + return fdtmp; +}