summaryrefslogtreecommitdiffhomepage
path: root/src/internal
diff options
context:
space:
mode:
Diffstat (limited to 'src/internal')
-rw-r--r--src/internal/tpax_dprintf_impl.c62
-rw-r--r--src/internal/tpax_dprintf_impl.h10
-rw-r--r--src/internal/tpax_driver_impl.h112
-rw-r--r--src/internal/tpax_errinfo_impl.c45
-rw-r--r--src/internal/tpax_errinfo_impl.h80
-rw-r--r--src/internal/tpax_readlink_impl.h31
6 files changed, 340 insertions, 0 deletions
diff --git a/src/internal/tpax_dprintf_impl.c b/src/internal/tpax_dprintf_impl.c
new file mode 100644
index 0000000..5d2f5f8
--- /dev/null
+++ b/src/internal/tpax_dprintf_impl.c
@@ -0,0 +1,62 @@
+/******************************************************/
+/* tpax: a topological pax implementation */
+/* Copyright (C) 2020 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.TPAX. */
+/******************************************************/
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+
+int tpax_dprintf(int fd, const char * fmt, ...)
+{
+ int ret;
+ int cnt;
+ int size;
+ va_list ap;
+ char * ch;
+ char * buf;
+ char chbuf[2048];
+
+ va_start(ap,fmt);
+
+ size = sizeof(chbuf);
+ buf = ((cnt = vsnprintf(chbuf,size,fmt,ap)) < size)
+ ? chbuf : malloc(cnt + 1);
+
+ va_end(ap);
+
+ if (buf == chbuf) {
+ (void)0;
+
+ } else if (buf) {
+ va_start(ap,fmt);
+ vsprintf(buf,fmt,ap);
+ va_end(ap);
+
+ } else {
+ return -1;
+ }
+
+ ret = 0;
+ ch = buf;
+
+ for (; cnt && ret>=0; ) {
+ ret = write(fd,ch,cnt);
+
+ while ((ret < 0) && (errno == EINTR))
+ ret = write(fd,ch,cnt);
+
+ ch += ret;
+ cnt -= ret;
+ }
+
+ ret = (ret < 0) ? -1 : ch - buf;
+
+ if (buf != chbuf)
+ free(buf);
+
+ return ret;
+}
diff --git a/src/internal/tpax_dprintf_impl.h b/src/internal/tpax_dprintf_impl.h
new file mode 100644
index 0000000..d83d0d3
--- /dev/null
+++ b/src/internal/tpax_dprintf_impl.h
@@ -0,0 +1,10 @@
+#ifndef TPAX_DPRINTF_IMPL_H
+#define TPAX_DPRINTF_IMPL_H
+
+#ifdef ARGV_DRIVER
+#define argv_dprintf tpax_dprintf
+#endif
+
+int tpax_dprintf(int fd, const char * fmt, ...);
+
+#endif
diff --git a/src/internal/tpax_driver_impl.h b/src/internal/tpax_driver_impl.h
new file mode 100644
index 0000000..49dea15
--- /dev/null
+++ b/src/internal/tpax_driver_impl.h
@@ -0,0 +1,112 @@
+/******************************************************/
+/* tpax: a topological pax implementation */
+/* Copyright (C) 2020 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.TPAX. */
+/******************************************************/
+
+#ifndef TPAX_DRIVER_IMPL_H
+#define TPAX_DRIVER_IMPL_H
+
+#include <stdint.h>
+#include <stdio.h>
+#include <sys/types.h>
+
+#include <tpax/tpax.h>
+#include "tpax_dprintf_impl.h"
+#include "argv/argv.h"
+
+#define TPAX_OPTV_ELEMENTS 64
+
+extern const struct argv_option tpax_default_options[];
+
+enum app_tags {
+ TAG_HELP,
+ TAG_VERSION,
+};
+
+struct tpax_driver_ctx_impl {
+ struct tpax_common_ctx cctx;
+ struct tpax_driver_ctx ctx;
+ struct tpax_fd_ctx fdctx;
+ const struct tpax_unit_ctx * euctx;
+ const char * eunit;
+ struct tpax_error_info ** errinfp;
+ struct tpax_error_info ** erricap;
+ struct tpax_error_info * erriptr[64];
+ struct tpax_error_info erribuf[64];
+};
+
+struct tpax_unit_ctx_impl {
+ const char * path;
+ struct tpax_unit_ctx uctx;
+};
+
+
+static inline struct tpax_driver_ctx_impl * tpax_get_driver_ictx(
+ const struct tpax_driver_ctx * dctx)
+{
+ uintptr_t addr;
+
+ if (dctx) {
+ addr = (uintptr_t)dctx - offsetof(struct tpax_driver_ctx_impl,ctx);
+ return (struct tpax_driver_ctx_impl *)addr;
+ }
+
+ return 0;
+}
+
+static inline void tpax_driver_set_ectx(
+ const struct tpax_driver_ctx * dctx,
+ const struct tpax_unit_ctx * uctx,
+ const char * unit)
+{
+ struct tpax_driver_ctx_impl * ictx;
+
+ ictx = tpax_get_driver_ictx(dctx);
+ ictx->euctx = uctx;
+ ictx->eunit = unit;
+}
+
+static inline int tpax_driver_fdin(const struct tpax_driver_ctx * dctx)
+{
+ struct tpax_fd_ctx fdctx;
+ tpax_get_driver_fdctx(dctx,&fdctx);
+ return fdctx.fdin;
+}
+
+static inline int tpax_driver_fdout(const struct tpax_driver_ctx * dctx)
+{
+ struct tpax_fd_ctx fdctx;
+ tpax_get_driver_fdctx(dctx,&fdctx);
+ return fdctx.fdout;
+}
+
+static inline int tpax_driver_fderr(const struct tpax_driver_ctx * dctx)
+{
+ struct tpax_fd_ctx fdctx;
+ tpax_get_driver_fdctx(dctx,&fdctx);
+ return fdctx.fderr;
+}
+
+static inline int tpax_driver_fdlog(const struct tpax_driver_ctx * dctx)
+{
+ struct tpax_fd_ctx fdctx;
+ tpax_get_driver_fdctx(dctx,&fdctx);
+ return fdctx.fdlog;
+}
+
+static inline int tpax_driver_fdcwd(const struct tpax_driver_ctx * dctx)
+{
+ struct tpax_fd_ctx fdctx;
+ tpax_get_driver_fdctx(dctx,&fdctx);
+ return fdctx.fdcwd;
+}
+
+static inline int tpax_driver_fddst(const struct tpax_driver_ctx * dctx)
+{
+ struct tpax_fd_ctx fdctx;
+ tpax_get_driver_fdctx(dctx,&fdctx);
+ return fdctx.fddst;
+}
+
+#endif
diff --git a/src/internal/tpax_errinfo_impl.c b/src/internal/tpax_errinfo_impl.c
new file mode 100644
index 0000000..b610869
--- /dev/null
+++ b/src/internal/tpax_errinfo_impl.c
@@ -0,0 +1,45 @@
+/******************************************************/
+/* tpax: a topological pax implementation */
+/* Copyright (C) 2020 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.TPAX. */
+/******************************************************/
+
+#include <tpax/tpax.h>
+#include "tpax_driver_impl.h"
+#include "tpax_errinfo_impl.h"
+
+int tpax_record_error(
+ const struct tpax_driver_ctx * dctx,
+ int esyscode,
+ int elibcode,
+ const char * efunction,
+ int eline,
+ unsigned eflags,
+ void * eany)
+{
+ struct tpax_driver_ctx_impl * ictx;
+ struct tpax_error_info * erri;
+
+ ictx = tpax_get_driver_ictx(dctx);
+
+ if (ictx->errinfp == ictx->erricap)
+ return -1;
+
+ *ictx->errinfp = &ictx->erribuf[ictx->errinfp - ictx->erriptr];
+ erri = *ictx->errinfp;
+
+ erri->euctx = ictx->euctx;
+ erri->eunit = ictx->eunit;
+
+ erri->edctx = dctx;
+ erri->esyscode = esyscode;
+ erri->elibcode = elibcode;
+ erri->efunction = efunction;
+ erri->eline = eline;
+ erri->eflags = eflags;
+ erri->eany = eany;
+
+ ictx->errinfp++;
+
+ return -1;
+}
diff --git a/src/internal/tpax_errinfo_impl.h b/src/internal/tpax_errinfo_impl.h
new file mode 100644
index 0000000..d85d159
--- /dev/null
+++ b/src/internal/tpax_errinfo_impl.h
@@ -0,0 +1,80 @@
+/******************************************************/
+/* tpax: a topological pax implementation */
+/* Copyright (C) 2020 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.TPAX. */
+/******************************************************/
+
+#include <errno.h>
+#include <tpax/tpax.h>
+
+int tpax_record_error(
+ const struct tpax_driver_ctx *,
+ int esyscode,
+ int elibcode,
+ const char * efunction,
+ int eline,
+ unsigned eflags,
+ void * ectx);
+
+#define TPAX_SYSTEM_ERROR(dctx) \
+ tpax_record_error( \
+ dctx, \
+ errno, \
+ 0, \
+ __func__, \
+ __LINE__, \
+ TPAX_ERROR_TOP_LEVEL, \
+ 0)
+
+#define TPAX_BUFFER_ERROR(dctx) \
+ tpax_record_error( \
+ dctx, \
+ ENOBUFS, \
+ 0, \
+ __func__, \
+ __LINE__, \
+ TPAX_ERROR_TOP_LEVEL, \
+ 0)
+
+#define TPAX_SPAWN_ERROR(dctx) \
+ tpax_record_error( \
+ dctx, \
+ errno, \
+ 0, \
+ __func__, \
+ __LINE__, \
+ TPAX_ERROR_TOP_LEVEL \
+ | (errno ? 0 \
+ : TPAX_ERROR_CHILD), \
+ 0)
+
+#define TPAX_FILE_ERROR(dctx) \
+ tpax_record_error( \
+ dctx, \
+ EIO, \
+ 0, \
+ __func__, \
+ __LINE__, \
+ TPAX_ERROR_TOP_LEVEL, \
+ 0)
+
+#define TPAX_CUSTOM_ERROR(dctx,elibcode) \
+ tpax_record_error( \
+ dctx, \
+ 0, \
+ elibcode, \
+ __func__, \
+ __LINE__, \
+ TPAX_ERROR_TOP_LEVEL \
+ | TPAX_ERROR_CUSTOM, \
+ 0)
+
+#define TPAX_NESTED_ERROR(dctx) \
+ tpax_record_error( \
+ dctx, \
+ 0, \
+ 0, \
+ __func__, \
+ __LINE__, \
+ TPAX_ERROR_NESTED, \
+ 0)
diff --git a/src/internal/tpax_readlink_impl.h b/src/internal/tpax_readlink_impl.h
new file mode 100644
index 0000000..328b302
--- /dev/null
+++ b/src/internal/tpax_readlink_impl.h
@@ -0,0 +1,31 @@
+/******************************************************/
+/* tpax: a topological pax implementation */
+/* Copyright (C) 2020 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.TPAX. */
+/******************************************************/
+
+#ifndef TPAX_READLINK_IMPL_H
+#define TPAX_READLINK_IMPL_H
+
+#include <unistd.h>
+#include <errno.h>
+
+static inline int tpax_readlink(
+ const char * restrict path,
+ char * restrict buf,
+ ssize_t bufsize)
+{
+ ssize_t ret;
+
+ if ((ret = readlink(path,buf,bufsize)) <= 0) {
+ return -1;
+ } else if (ret == bufsize) {
+ errno = ENOBUFS;
+ return -1;
+ } else {
+ buf[ret] = 0;
+ return 0;
+ }
+}
+
+#endif