summaryrefslogtreecommitdiff
path: root/src/arbits/slbt_archive_store.c
blob: a70e307800bd885cff06b38f852c446a0398a203 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*******************************************************************/
/*  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 <time.h>
#include <fcntl.h>
#include <stdio.h>
#include <stddef.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/stat.h>

#include <slibtool/slibtool.h>
#include <slibtool/slibtool_arbits.h>
#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;
	void *                          addr;
	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);

	addr = buf;
	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),addr,
		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;
}