diff options
author | midipix <writeonce@midipix.org> | 2024-05-26 15:09:11 +0000 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2024-05-26 15:09:11 +0000 |
commit | 0ee4a827bc3b3a71234df5c44288d15ad697b724 (patch) | |
tree | 023d290cc147a4a2833b0f55af41216e7469048f /src/io | |
parent | 4cb6ae148e6bd9d832c637a5fdd01d68febdbf67 (diff) | |
download | tpax-0ee4a827bc3b3a71234df5c44288d15ad697b724.tar.bz2 tpax-0ee4a827bc3b3a71234df5c44288d15ad697b724.tar.xz |
library api's: _io_ (low level interfaces) namespace overhaul.
Diffstat (limited to 'src/io')
-rw-r--r-- | src/io/tpax_create_memory_snapshot.c | 98 | ||||
-rw-r--r-- | src/io/tpax_create_tmpfs_snapshot.c | 127 |
2 files changed, 225 insertions, 0 deletions
diff --git a/src/io/tpax_create_memory_snapshot.c b/src/io/tpax_create_memory_snapshot.c new file mode 100644 index 0000000..7c3dd7e --- /dev/null +++ b/src/io/tpax_create_memory_snapshot.c @@ -0,0 +1,98 @@ +/**************************************************************/ +/* tpax: a topological pax implementation */ +/* Copyright (C) 2020--2024 SysDeer Technologies, LLC */ +/* 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_io_create_memory_snapshot( + const struct tpax_driver_ctx * dctx, + int fdat, + 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 */ + if ((fd = openat(fdat,path,O_CLOEXEC|O_NOCTTY|O_NOFOLLOW,0)) < 0) + return TPAX_SYSTEM_ERROR(dctx); + + /* stat compare */ + if ((fstat(fd,&dstst)) < 0) { + close(fd); + return TPAX_SYSTEM_ERROR(dctx); + + } else if (tpax_util_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_util_stat_compare(srcst,&dstst)) { + close(fd); + return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FILE_CHANGED); + } + + /* yay */ + close(fd); + + return 0; +} diff --git a/src/io/tpax_create_tmpfs_snapshot.c b/src/io/tpax_create_tmpfs_snapshot.c new file mode 100644 index 0000000..9891e98 --- /dev/null +++ b/src/io/tpax_create_tmpfs_snapshot.c @@ -0,0 +1,127 @@ +/**************************************************************/ +/* tpax: a topological pax implementation */ +/* Copyright (C) 2020--2024 SysDeer Technologies, LLC */ +/* 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_tmpfile_impl.h" +#include "tpax_errinfo_impl.h" + +#ifndef ssizeof +#define ssizeof(x) (ssize_t)(sizeof(x)) +#endif + +int tpax_io_create_tmpfs_snapshot( + const struct tpax_driver_ctx * dctx, + int fdat, + 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; + + /* record errors */ + tpax_driver_set_ectx( + dctx,0,path); + + /* tmpfile */ + if ((fdtmp = tpax_tmpfile()) < 0) + return TPAX_SYSTEM_ERROR(dctx); + + /* buffer */ + buf = tpax_get_driver_anon_map_addr( + dctx,&buflen); + + /* open */ + if ((fdsrc = openat(fdat,path,O_CLOEXEC|O_NOCTTY|O_NOFOLLOW,0)) < 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_util_stat_compare(srcst,&dstst)) { + close(fdsrc); + close(fdtmp); + return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FILE_CHANGED); + } + + /* read/write loop */ + for (nread=0; nread<srcst->st_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_util_stat_compare(srcst,&dstst)) { + close(fdsrc); + close(fdtmp); + return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FILE_CHANGED); + } + + /* yay */ + close(fdsrc); + + return fdtmp; +} |