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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
/**************************************************************/
/* 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 <tpax/tpax.h>
#include "tpax_driver_impl.h"
#include "tpax_errinfo_impl.h"
#include "tpax_visibility_impl.h"
#define TPAX_MAX_DEPTH 512
tpax_hidden const char * tpax_queue_item_full_path(
const struct tpax_driver_ctx * dctx,
const struct tpax_dirent * cdent)
{
char * ch;
char * pathbuf;
const struct tpax_dirent * pparent;
const struct tpax_dirent ** pdirent;
const struct tpax_dirent * dirstck[TPAX_MAX_DEPTH];
if (cdent->depth >= TPAX_MAX_DEPTH)
return 0;
ch = pathbuf = (tpax_get_driver_ictx(dctx))->dirbuff;
for (pparent=cdent,pdirent=dirstck; pparent; pparent=pparent->parent)
*pdirent++ = pparent;
*pdirent-- = 0;
if (pdirent[0]->prefix)
ch += sprintf(ch,"%s",pdirent[0]->prefix);
for (; pdirent > dirstck; ) {
if (!(pdirent[0]->flags & TPAX_ITEM_SYMLINK))
ch += sprintf(ch,"%s/",pdirent[0]->dirent.d_name);
pdirent--;
}
if (pdirent[0]->flags & TPAX_ITEM_SYMLINK) {
*--ch = '\0';
} else {
sprintf(ch,"%s",pdirent[0]->dirent.d_name);
}
return pathbuf;
}
static int tpax_cpio_dirent_compare(const void * a, const void * b)
{
const struct tpax_dirent * direnta;
const struct tpax_dirent * direntb;
direnta = *(const struct tpax_dirent **)a;
direntb = *(const struct tpax_dirent **)b;
return (direnta->stdev == direntb->stdev)
? direnta->stino - direntb->stino
: direnta->stdev - direntb->stdev;
}
static int tpax_update_cpio_queue_vector(const struct tpax_driver_ctx * dctx)
{
struct tpax_driver_ctx_impl * ictx;
struct tpax_dirent ** cpiov;
struct tpax_dirent ** direntv;
struct tpax_dirent * cdent;
dev_t stdev;
ino_t stino;
int cpdev;
int cpino;
int nlink;
int idx;
/* driver */
ictx = tpax_get_driver_ictx(dctx);
/* not needed? */
if (ictx->nqueued == 0)
return 0;
/* vector copy */
direntv = ictx->direntv;
cpiov = ictx->cpiov;
for (; *direntv; )
*cpiov++ = *direntv++;
/* sort by real st_dev, st_ino */
qsort(ictx->cpiov,ictx->nqueued,sizeof(*cpiov),tpax_cpio_dirent_compare);
/* set the cpio internal c_dev, c_ino, and c_nlink fields (U+1F620) */
cpiov = ictx->cpiov;
cdent = cpiov[0];
cpiov++;
cpdev = 1;
cpino = 1;
nlink = 1;
stdev = cdent->stdev;
stino = cdent->stino;
cdent->cpdev = cpdev;
cdent->cpino = cpino;
cdent->nlink = nlink;
for (; *cpiov; ) {
cdent = *cpiov;
if (cdent->srdev > 0777777)
return TPAX_CUSTOM_ERROR(
dctx,
TPAX_ERR_FLOW_ERROR);
if ((cdent->stdev == stdev) && (cdent->stino == stino)) {
nlink++;
} else if (cdent->stdev > stdev) {
if (nlink > 1)
for (idx=2; idx<=nlink; idx++)
cpiov[-idx]->nlink = nlink;
cpdev++;
cpino = 1;
nlink = 1;
stdev = cdent->stdev;
stino = cdent->stino;
if (cpdev > 0777777)
return TPAX_CUSTOM_ERROR(
dctx,
TPAX_ERR_FLOW_ERROR);
} else if (cdent->stino > stino) {
if (nlink > 1)
for (idx=2; idx<=nlink; idx++)
cpiov[-idx]->nlink = nlink;
cpino++;
stino = cdent->stino;
nlink = 1;
if (cpino > 0777777)
return TPAX_CUSTOM_ERROR(
dctx,
TPAX_ERR_FLOW_ERROR);
}
cdent->cpdev = cpdev;
cdent->cpino = cpino;
cdent->nlink = nlink;
cpiov++;
}
if (nlink > 1)
for (idx=2; idx<=nlink; idx++)
cpiov[-idx]->nlink = nlink;
return 0;
}
tpax_hidden int tpax_update_queue_vector(const struct tpax_driver_ctx * dctx)
{
uintptr_t addr;
struct tpax_driver_ctx_impl * ictx;
struct tpax_dirent_buffer * dentbuf;
struct tpax_dirent ** direntv;
struct tpax_dirent * cdent;
struct tpax_dirent * cnext;
size_t arrsize;
/* driver */
ictx = tpax_get_driver_ictx(dctx);
/* vector alloc */
if (ictx->direntv) {
free(ictx->direntv);
free(ictx->cpiov);
ictx->direntv = 0;
ictx->cpiov = 0;
}
arrsize = ictx->nqueued + 1;
if (!(ictx->direntv = calloc(arrsize,sizeof(struct tpax_dirent *))))
return TPAX_SYSTEM_ERROR(dctx);
if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_CPIO)
if (!(ictx->cpiov = calloc(arrsize,sizeof(struct tpax_dirent *))))
return TPAX_SYSTEM_ERROR(dctx);
/* queue vector */
dentbuf = tpax_get_driver_dirents(dctx);
cdent = ictx->nqueued ? dentbuf->dbuf : 0;
for (direntv=ictx->direntv; cdent; direntv++) {
*direntv = cdent;
addr = (uintptr_t)cdent;
addr += cdent->nsize;
cnext = (struct tpax_dirent *)addr;
if (cnext == dentbuf->cdent) {
dentbuf = dentbuf->next;
cnext = dentbuf ? dentbuf->dbuf : 0;
}
cdent = cnext;
}
if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_CPIO)
if (tpax_update_cpio_queue_vector(dctx) < 0)
return TPAX_NESTED_ERROR(dctx);
return 0;
}
|