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
|
/**************************************************************/
/* tpax: a topological pax implementation */
/* Copyright (C) 2020--2024 SysDeer Technologies, LLC */
/* Released under GPLv2 and GPLv3; see COPYING.TPAX. */
/**************************************************************/
#include <tpax/tpax.h>
#include "tpax_driver_impl.h"
#include "tpax_errinfo_impl.h"
#include "tpax_visibility_impl.h"
static int tpax_archive_append_memory_data(
int fdout,
void * buf,
ssize_t nbytes)
{
ssize_t ret;
char * ch;
for (ch=buf; nbytes; ch+=ret) {
ret = write(fdout,ch,nbytes);
while ((ret < 0) && (errno == EINTR))
ret = write(fdout,ch,nbytes);
if (ret < 0)
return ret;
nbytes -= ret;
}
return 0;
}
static int tpax_archive_append_pad(
const struct tpax_driver_ctx * dctx,
int fdout,
const struct stat * st)
{
int ret;
off_t cpos;
ssize_t nbytes;
char buf[512];
nbytes = st->st_size;
nbytes += 0x1ff;
nbytes |= 0x1ff;
nbytes ^= 0x1ff;
nbytes -= st->st_size;
memset(buf,0,nbytes);
cpos = tpax_get_driver_cpos(dctx);
cpos += st->st_size + nbytes;
if (!(ret = tpax_archive_append_memory_data(fdout,buf,nbytes)))
tpax_set_driver_cpos(dctx,cpos);
return ret;
}
int tpax_archive_seal(const struct tpax_driver_ctx * dctx)
{
int fdout;
off_t cpos;
ssize_t nbytes;
ssize_t nwritten;
ssize_t blksize;
char buf[512];
blksize = tpax_get_archive_block_size(dctx);
cpos = tpax_get_driver_cpos(dctx);
if (cpos % 512)
return TPAX_CUSTOM_ERROR(dctx,TPAX_ERR_FLOW_ERROR);
fdout = tpax_driver_fdout(dctx);
memset(buf,0,sizeof(buf));
switch (cpos % blksize) {
case 0:
nbytes = cpos + blksize;
break;
default:
nbytes = cpos / blksize;
nbytes *= blksize;
nbytes += blksize;
if (nbytes-cpos == 512)
nbytes += blksize;
}
for (nwritten=cpos; nwritten<nbytes; nwritten+=512) {
if (tpax_archive_append_memory_data(fdout,buf,512) < 0)
return TPAX_SYSTEM_ERROR(dctx);
tpax_set_driver_cpos(dctx,nwritten);
}
(void)tpax_archive_append_pad;
return 0;
}
|