-
Notifications
You must be signed in to change notification settings - Fork 0
/
mke2img.c
260 lines (212 loc) · 5.49 KB
/
mke2img.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
251
252
253
254
255
256
257
258
259
260
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <ext2fs/ext2fs.h>
#include <stdarg.h>
#include <fcntl.h>
static char buf[16384];
static void
error(errcode_t e, const char *mesg, ...)
{
va_list args;
va_start(args, mesg);
vfprintf(stderr, mesg, args);
va_end(args);
fprintf(stderr, ": %s\n", error_message(e));
}
static void
trawl_dir(ext2_filsys image, ext2fs_inode_bitmap bitmap, ext2_ino_t parent_dir)
{
DIR *d = opendir(".");
struct dirent *dd;
errcode_t e;
if (d == NULL)
{
perror("opendir");
exit(1);
}
while((dd = readdir(d)) != NULL)
{
struct stat info;
struct ext2_inode inode_info;
ext2_ino_t new;
int type;
if (strcmp(dd->d_name, ".") == 0 ||
strcmp(dd->d_name, "..") == 0)
{
/* We don't need to duplicate these. */
continue;
}
if (lstat(dd->d_name, &info))
{
perror(dd->d_name);
continue;
}
if ((e = ext2fs_new_inode(image, parent_dir, info.st_mode, 0, &new)))
{
error(e, "Error creating new inode '%s'", dd->d_name);
continue;
}
if (S_ISDIR(info.st_mode))
{
if ((e = ext2fs_mkdir(image, parent_dir, new, dd->d_name)))
{
error(e, "Couldn't create dir '%s'", dd->d_name);
continue;
}
printf("recursing -> %s\n", dd->d_name);
if (! chdir(dd->d_name))
{
trawl_dir(image, bitmap, new);
chdir("..");
}
else
perror(dd->d_name);
}
else if (S_ISREG(info.st_mode))
{
ext2_file_t f;
blk_t blockno;
int fd, len, out;
char buf[1024];
printf("Creating %s\n", dd->d_name);
memset(&inode_info, 0, sizeof(inode_info));
inode_info.i_mode = LINUX_S_IFREG | (info.st_mode & 0777);
inode_info.i_uid = info.st_uid;
inode_info.i_gid = info.st_gid;
inode_info.i_atime = info.st_atime;
inode_info.i_mtime = info.st_mtime;
inode_info.i_ctime = info.st_ctime;
inode_info.i_links_count = 1;
inode_info.i_size = info.st_size;
if ((e = ext2fs_write_new_inode(image, new, &inode_info)))
error(e, "Setting new file attributes");
ext2fs_inode_alloc_stats2(image, new, +1, 1);
if ((fd = open(dd->d_name, O_RDONLY)) < 0)
{
perror(dd->d_name);
continue;
}
if ((e = ext2fs_file_open(image, new, EXT2_FILE_WRITE, &f)))
{
error(e, "Creating file '%s'", dd->d_name);
continue;
}
while((len = read(fd, buf, image->blocksize)) > 0)
{
if ((e = ext2fs_file_write(f, buf, len, &out)) ||
(out < len))
{
error(e, "Copying '%s'", dd->d_name);
ext2fs_file_close(f);
continue;
}
if ((e = ext2fs_file_flush(f)))
error(e, "Flushing");
}
if ((e = ext2fs_file_close(f)))
error(e, "Closing %s", dd->d_name);
close(fd);
/* If dir is full, call ext2fs_expand_dir. */
if ((e = ext2fs_link(image, parent_dir, dd->d_name, new, EXT2_FT_REG_FILE)))
{
error(e, "Failed to copy '%s'", dd->d_name);
continue;
}
}
else
{
if (S_ISCHR(info.st_mode))
type = EXT2_FT_CHRDEV;
else if (S_ISBLK(info.st_mode))
type = EXT2_FT_BLKDEV;
else if (S_ISFIFO(info.st_mode))
type = EXT2_FT_FIFO;
else if (S_ISSOCK(info.st_mode))
type = EXT2_FT_SOCK;
else if (S_ISLNK(info.st_mode))
type = EXT2_FT_SYMLINK;
else
{
fprintf(stderr, "%s: not file or dir (specials unimplemented)\n", dd->d_name);
continue;
}
memset(&inode_info, 0, sizeof(inode_info));
inode_info.i_mode = info.st_mode;
inode_info.i_uid = info.st_uid;
inode_info.i_gid = info.st_gid;
inode_info.i_atime = info.st_atime;
inode_info.i_mtime = info.st_mtime;
inode_info.i_ctime = info.st_ctime;
inode_info.i_links_count = 1;
inode_info.i_size = info.st_size;
inode_info.i_block[0] = info.st_rdev;
if ((e = ext2fs_write_new_inode(image, new, &inode_info)))
error(e, "Writing new device inode");
ext2fs_inode_alloc_stats2(image, new, +1, 1);
if (type == EXT2_FT_SYMLINK)
{
ext2_file_t f;
char buf[1024];
if (readlink(dd->d_name, buf, sizeof(buf)) >= 0)
{
int out;
if ((e = ext2fs_file_open(image, new, EXT2_FILE_WRITE, &f)) != 0 ||
(e = ext2fs_file_write(f, buf, info.st_size, &out)) != 0)
error(e, "Copying symlink '%s'", dd->d_name);
ext2fs_file_close(f);
}
else
perror(dd->d_name);
}
if ((e = ext2fs_link(image, parent_dir, dd->d_name, new, type)))
{
error(e, "Failed to copy '%s'", dd->d_name);
continue;
}
}
}
closedir(d);
}
int
main(int argc, char **argv)
{
ext2_filsys image;
ext2_ino_t parent_dir = EXT2_ROOT_INO;
ext2fs_inode_bitmap bitmap;
errcode_t e;
if (argc < 3)
{
fprintf(stderr, "Usage:\n\t%s ext2-image dirtree\n", argv[0]);
exit(1);
}
initialize_ext2_error_table();
if ((e = ext2fs_open(argv[1],
EXT2_FLAG_RW,
0 /*primary SUPERBLOCK*/,
0/* DEFAULT BLOCKSIZE */,
unix_io_manager,
&image)))
{
fputs("Eep!\n", stderr);
exit(1);
}
if ((e = ext2fs_read_bitmaps(image)))
{
error(e, "Reading bitmaps");
}
chdir(argv[2]);
trawl_dir(image, 0, parent_dir);
if ((e = ext2fs_write_bitmaps(image)))
{
error(e, "Writing inode bitmap");
}
if (ext2fs_close(image))
{
fputs("Error closing off the image - things may be broken\n", stderr);
exit(1);
}
return 0;
}
/* $Id: mke2img.c,v 1.1 2006/07/17 06:55:31 john Exp $ -- EOF */