From c8d21a38d6941303527af15b95d887a053aa0df6 Mon Sep 17 00:00:00 2001 From: midipix Date: Thu, 8 Feb 2024 06:24:02 +0000 Subject: slbt_store_archive(): initial implementation. --- include/slibtool/slibtool.h | 4 ++ project/common.mk | 1 + src/arbits/slbt_archive_store.c | 143 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 src/arbits/slbt_archive_store.c diff --git a/include/slibtool/slibtool.h b/include/slibtool/slibtool.h index 6431a25..4ca7064 100644 --- a/include/slibtool/slibtool.h +++ b/include/slibtool/slibtool.h @@ -1,6 +1,7 @@ #ifndef SLIBTOOL_H #define SLIBTOOL_H +#include #include #include #include @@ -391,6 +392,9 @@ slbt_api void slbt_free_archive_meta (struct slbt_archive_meta *); slbt_api int slbt_merge_archives (struct slbt_archive_ctx * const [], struct slbt_archive_ctx **); +slbt_api int slbt_store_archive (struct slbt_archive_ctx *, + const char *, mode_t); + /* utility api */ slbt_api int slbt_main (char **, char **, const struct slbt_fd_ctx *); diff --git a/project/common.mk b/project/common.mk index 66f2f64..e8c60f4 100644 --- a/project/common.mk +++ b/project/common.mk @@ -2,6 +2,7 @@ API_SRCS = \ src/arbits/slbt_archive_ctx.c \ src/arbits/slbt_archive_merge.c \ src/arbits/slbt_archive_meta.c \ + src/arbits/slbt_archive_store.c \ src/arbits/slbt_armap_bsd_32.c \ src/arbits/slbt_armap_bsd_64.c \ src/arbits/slbt_armap_sysv_32.c \ diff --git a/src/arbits/slbt_archive_store.c b/src/arbits/slbt_archive_store.c new file mode 100644 index 0000000..d8281b0 --- /dev/null +++ b/src/arbits/slbt_archive_store.c @@ -0,0 +1,143 @@ +/*******************************************************************/ +/* slibtool: a skinny libtool implementation, written in C */ +/* Copyright (C) 2016--2024 SysDeer Technologies, LLC */ +/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */ +/*******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include "slibtool_ar_impl.h" +#include "slibtool_driver_impl.h" +#include "slibtool_errinfo_impl.h" + +/****************************************************/ +/* As elsewhere in slibtool, file-system operations */ +/* utilizie the _at variants of the relevant posix */ +/* interfaces. In the case of archives, that means */ +/* passing dctx->fdctx->fdcwd as the _fdat_ param, */ +/* where dctx is the driver context which was used */ +/* with slbt_get_archive_ctx(). */ +/************************************************** */ + +#define PPRIX64 "%"PRIx64 + +int slbt_store_archive( + struct slbt_archive_ctx * arctx, + const char * path, + mode_t mode) +{ + const struct slbt_driver_ctx * dctx; + struct stat st; + int fdat; + int fdtmp; + char * mark; + char * slash; + size_t buflen; + size_t nbytes; + ssize_t written; + char buf[PATH_MAX]; + + /* init dctx */ + if (!(dctx = slbt_get_archive_ictx(arctx)->dctx)) + return -1; + + /* validation */ + if (strlen(path) >= PATH_MAX) + return SLBT_CUSTOM_ERROR( + dctx, + SLBT_ERR_FLOW_ERROR); + + /**************************************************/ + /* create temporary file in the target directory */ + /* */ + /* the tmpfile name pattern involes the inode */ + /* of the target directory, a local stack address */ + /* in the calling thread, the current time, and */ + /* finally the pid of the current process. */ + /**************************************************/ + + memset(buf,0,sizeof(buf)); + strcpy(buf,path); + + fdat = slbt_driver_fdcwd(dctx); + + mark = (slash = strrchr(buf,'/')) + ? slash : buf; + + if (slash) { + *++mark = '\0'; + + if (fstatat(fdat,buf,&st,0) < 0) + return SLBT_SYSTEM_ERROR( + dctx,0); + } else { + if (fstatat(fdat,".",&st,0) < 0) + return SLBT_SYSTEM_ERROR( + dctx,0); + } + + buflen = sizeof(buf) - (mark - buf); + nbytes = snprintf( + mark, + buflen, + ".slibtool.tmpfile" + ".inode."PPRIX64 + ".time."PPRIX64 + ".salt.%p" + ".pid.%d" + ".tmp", + st.st_ino, + time(0),&buf, + getpid()); + + if (nbytes >= buflen) + return SLBT_CUSTOM_ERROR( + dctx, + SLBT_ERR_FLOW_ERROR); + + if ((fdtmp = openat(fdat,buf,O_WRONLY|O_CREAT|O_EXCL,mode)) < 0) + return SLBT_SYSTEM_ERROR(dctx,0); + + /* set archive size */ + if (ftruncate(fdtmp,arctx->map->map_size) < 0) + return SLBT_SYSTEM_ERROR(dctx,0); + + /* write archive */ + mark = arctx->map->map_addr; + nbytes = arctx->map->map_size; + + for (; nbytes; ) { + written = write(fdtmp,mark,nbytes); + + while ((written < 0) && (errno == EINTR)) + written = write(fdtmp,mark,nbytes); + + if (written < 0) { + unlinkat(fdat,buf,0); + return SLBT_SYSTEM_ERROR(dctx,0); + }; + + nbytes -= written; + mark += written; + } + + /* finalize (atomically) */ + if (renameat(fdat,buf,fdat,path) < 0) { + unlinkat(fdat,buf,0); + return SLBT_SYSTEM_ERROR(dctx,0); + } + + /* yay */ + return 0; +} -- cgit v1.2.3