Skip to content

Commit c657f39

Browse files
committed
Implement support for block and char dev nodes, fifos and sockets.
Signed-off-by: Jo-Philipp Wich <[email protected]>
1 parent 62768db commit c657f39

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

contents.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,40 @@ u32 make_link(const char *link)
244244
return inode_num;
245245
}
246246

247+
/* Creates a special file on disk. Returns the inode number of the new file */
248+
u32 make_special(const char *path)
249+
{
250+
struct ext4_inode *inode;
251+
struct stat s;
252+
u32 inode_num;
253+
254+
if (stat(path, &s)) {
255+
error("failed to stat file\n");
256+
return EXT4_ALLOCATE_FAILED;
257+
}
258+
259+
inode_num = allocate_inode(info);
260+
if (inode_num == EXT4_ALLOCATE_FAILED) {
261+
error("failed to allocate inode\n");
262+
return EXT4_ALLOCATE_FAILED;
263+
}
264+
265+
inode = get_inode(inode_num);
266+
if (inode == NULL) {
267+
error("failed to get inode %u", inode_num);
268+
return EXT4_ALLOCATE_FAILED;
269+
}
270+
271+
inode->i_mode = s.st_mode & S_IFMT;
272+
inode->i_links_count = 1;
273+
inode->i_flags |= aux_info.default_i_flags;
274+
275+
((u8 *)inode->i_block)[0] = major(s.st_rdev);
276+
((u8 *)inode->i_block)[1] = minor(s.st_rdev);
277+
278+
return inode_num;
279+
}
280+
247281
int inode_set_permissions(u32 inode_num, u16 mode, u16 uid, u16 gid, u32 mtime)
248282
{
249283
struct ext4_inode *inode = get_inode(inode_num);

contents.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ u32 make_directory(u32 dir_inode_num, u32 entries, struct dentry *dentries,
3737
u32 dirs);
3838
u32 make_file(const char *filename, u64 len);
3939
u32 make_link(const char *link);
40+
u32 make_special(const char *path);
4041
int inode_set_permissions(u32 inode_num, u16 mode, u16 uid, u16 gid, u32 mtime);
4142
int inode_set_selinux(u32 inode_num, const char *secon);
4243
int inode_set_capabilities(u32 inode_num, uint64_t capabilities);

make_ext4fs.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
/* TODO: Not implemented:
4545
Allocating blocks in the same block group as the file inode
4646
Hash or binary tree directories
47-
Special files: sockets, devices, fifos
4847
*/
4948

5049
static int filter_dot(const struct dirent *d)
@@ -235,6 +234,11 @@ static u32 build_directory_structure(const char *full_path, const char *dir_path
235234
free(subdir_dir_path);
236235
} else if (dentries[i].file_type == EXT4_FT_SYMLINK) {
237236
entry_inode = make_link(dentries[i].link);
237+
} else if (dentries[i].file_type == EXT4_FT_CHRDEV ||
238+
dentries[i].file_type == EXT4_FT_BLKDEV ||
239+
dentries[i].file_type == EXT4_FT_SOCK ||
240+
dentries[i].file_type == EXT4_FT_FIFO) {
241+
entry_inode = make_special(dentries[i].full_path);
238242
} else {
239243
error("unknown file type on %s", dentries[i].path);
240244
entry_inode = 0;

0 commit comments

Comments
 (0)