-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtar.c
250 lines (213 loc) · 6.25 KB
/
tar.c
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <linux/slab.h>
#include <linux/stat.h>
#include <linux/fs.h>
#include "tar.h"
#include "device.h"
/** Aligns \a x on a 512 boundary. */
#define ALIGN_SECTOR(x) (((x) % 512 > 0) ? 512 - ((x) % 512) : 0)
#define OCTAL (8)
mode_t tar_type_to_posix(int typeflag)
{
switch(typeflag) {
case REGTYPE:
case AREGTYPE:
return S_IFREG;
case DIRTYPE:
return S_IFDIR;
case SYMTYPE:
return S_IFLNK;
case CHRTYPE:
return S_IFCHR;
case BLKTYPE:
return S_IFBLK;
case FIFOTYPE:
return S_IFIFO;
default:
return 0;
}
}
/**
* @brief Allocates a string by combining prefix and name from \a header.
* @return the string, must be free'd later by the caller.
*/
static char *build_name(struct star_header *header)
{
char *prefix_end = memchr(header->prefix, 0, sizeof(header->prefix));
char *name_end = memchr(header->name, 0, sizeof(header->name));
size_t prefix_len = prefix_end - header->prefix;
size_t name_len = name_end - header->name;
char *name = kmalloc(prefix_len + name_len + 1, GFP_KERNEL);
if (!name)
return NULL;
memcpy(name, header->prefix, prefix_len);
memcpy(name + prefix_len, header->name, name_len);
name[prefix_len + name_len] = 0x0;
// The path name ends with a slash if the entry is a directory
if (name[prefix_len + name_len - 1] == '/')
name[prefix_len + name_len - 1] = 0x0;
return name;
}
/**
* @brief Reads a tar header at the \a offset.
* @param sb the superblock to read from
* @param offset the 512-byte aligned offset
* @return the entry on success, else \c NULL
*/
struct tar_entry *tar_read_entry(struct super_block *sb, off_t offset)
{
struct star_header header;
char *full_name = NULL;
char *basename = NULL;
struct tar_entry *entry = NULL;
unsigned int length = 0;
unsigned int mode = 0;
uid_t uid = 0;
gid_t gid = 0;
struct timespec atime, mtime, ctime;
// Read the header, and return NULL if there can't be a header.
if (tarfs_read(&header, sizeof(header), offset, sb) != sizeof(header)) {
pr_err("tarfs: read failure");
return NULL;
}
// Check for the header magic value
if (memcmp(header.magic, OLDGNU_MAGIC, sizeof(header.magic)) != 0) {
return NULL;
}
// Parse the data length from the header
if (kstrtouint(header.size, OCTAL, &length) != 0) {
pr_info("tarfs: failed to read size");
return NULL;
}
// Parse mode
if (kstrtouint(header.mode, OCTAL, &mode) != 0) {
pr_info("tarfs: failed to read mode");
return NULL;
}
if (kstrtouint(header.uid, OCTAL, &uid) != 0) {
pr_info("tarfs: failed to read uid");
return NULL;
}
if (kstrtouint(header.gid, OCTAL, &gid) != 0) {
pr_info("tarfs: failed to read gid");
return NULL;
}
// Modification time is the most likely to be present
if (kstrtoul(header.mtime, OCTAL, &mtime.tv_sec) != 0) {
mtime.tv_sec = 0;
mtime.tv_nsec = 0;
}
if (kstrtoul(header.atime, OCTAL, &atime.tv_sec) != 0) {
atime = mtime; // Copy mtime if not set
}
if (kstrtoul(header.ctime, OCTAL, &ctime.tv_sec) != 0) {
ctime = mtime; // Copy mtime if not set
}
// Build the full name of the entry
full_name = build_name(&header);
if (!full_name) {
pr_info("tarfs: name allocation error");
return NULL;
}
// Split path name into dirname and basename. If the file resides in the
// root, take the full name as basename and point dirname to the trailing
// NUL byte. Else, NUL the last slash ("/") and set the pointers accordingly.
basename = strrchr(full_name, '/');
if (basename) {
*basename = 0x0;
basename++;
} else {
basename = full_name;
full_name = basename + strlen(basename);
}
// Fill in structure
entry = kzalloc(sizeof(struct tar_entry), GFP_KERNEL);
entry->header = header;
entry->dirname = full_name;
entry->basename = basename;
entry->offset = offset;
entry->data_offset = offset + sizeof(header) + ALIGN_SECTOR(sizeof(header));
entry->length = length;
entry->mode = mode;
entry->uid = uid;
entry->gid = gid;
entry->atime = atime;
entry->mtime = mtime;
entry->ctime = ctime;
return entry;
}
/**
* @brief Reads all file headers from the \a sb
* @param sb the underlying super block
* @return the first entry, pointing at all other entries
*/
struct tar_entry *tar_open(struct super_block *sb)
{
struct tar_entry *first = tar_read_entry(sb, 0);
struct tar_entry *parent = first;
struct tar_entry *next;
unsigned long count = 2;
off_t offset = 0;
off_t length;
while (parent) {
parent->inode = count; // Assign inode number
count++;
length = parent->data_offset + parent->length;
// Skip leading data of the previously read entry
offset = length + ALIGN_SECTOR(length);
// Read next entry
next = tar_read_entry(sb, offset);
parent->next = next;
parent = next;
}
return first;
}
/**
* @brief Frees the \a entry, and all connected entries
* @param entry the entry to deallocate
*/
void tar_free(struct tar_entry *entry)
{
while (entry) {
struct tar_entry *next = entry->next;
if (entry->dirname < entry->basename)
kfree(entry->dirname);
else
kfree(entry->basename);
kfree(entry);
entry = next;
}
}
/**
* @brief Reads the payload of \a entry into \a buffer.
* @param sb the underlying super block
* @param entry the entry to read from
* @param off the offset inside the file data
* @param buffer the target buffer
* @param len byte count to read
* @return count of read bytes
*/
size_t tar_read(struct super_block *sb, struct tar_entry *entry,
unsigned int off, void *buffer, size_t len)
{
size_t to_read = min_t(size_t, len, entry->length - off);
return tarfs_read(buffer, to_read, entry->data_offset + off, sb);
}
/**
* @brief Finds an entry by its path.
* @param entry the start entry
* @param dirname the path to the directory containing the target entry
* @param basename the name of the target entry in its parent directory
* @return the found entry, or \c NULL
*/
struct tar_entry *tar_find(struct tar_entry *entry, const char *dirname,
const char *basename)
{
while (entry) {
if (!strcmp(entry->basename, basename) &&
!strcmp(entry->dirname, dirname)) {
return entry;
}
entry = entry->next;
}
return NULL;
}