Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
K=kernel
U=user

DIR = dir1 dir2

OBJS = \
$K/entry.o \
$K/start.o \
Expand Down Expand Up @@ -128,13 +130,14 @@ UPROGS=\
$U/_rm\
$U/_sh\
$U/_stressfs\
$U/_tree\
$U/_usertests\
$U/_grind\
$U/_wc\
$U/_zombie\

fs.img: mkfs/mkfs README.md $(UPROGS)
mkfs/mkfs fs.img README.md $(UPROGS)
fs.img: mkfs/mkfs README.md $(UPROGS) $(DIR)
mkfs/mkfs fs.img README.md $(UPROGS) $(DIR)

-include kernel/*.d user/*.d

Expand Down
1 change: 1 addition & 0 deletions dir1/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
1 change: 1 addition & 0 deletions dir2/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
93 changes: 91 additions & 2 deletions mkfs/mkfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>

#define dirent xv6_dirent
#define stat xv6_stat // avoid clash with host struct stat
#include "kernel/types.h"
#include "kernel/fs.h"
#include "kernel/stat.h"
#include "kernel/param.h"
#undef dirent
#undef stat

#ifndef static_assert
#define static_assert(a, b) do { switch (0) case 0: case (a): ; } while (0)
Expand Down Expand Up @@ -65,12 +71,88 @@ xint(uint x)
return y;
}

void
add_file(int parent_fd, uint parentino, char *filename)
{
uint inum;
int fd;
char buf[BSIZE];
struct xv6_dirent de;

if((fd = openat(parent_fd, filename, 0)) < 0)
die(filename);

inum = ialloc(T_FILE);

bzero(&de, sizeof(de));
de.inum = xshort(inum);
strncpy(de.name, filename, DIRSIZ);
iappend(parentino, &de, sizeof(de));

ssize_t cc;
while((cc = read(fd, buf, sizeof(buf))) > 0)
iappend(inum, buf, cc);

close(fd);
}

uint
add_dir(int level, int parent_fd, uint parentino, char *dirname)
{
struct xv6_dirent de;
uint dino = ialloc(T_DIR);
bzero(&de, sizeof(de));
de.inum = xshort(dino);
strcpy(de.name, dirname);
iappend(parentino, &de, sizeof(de));

bzero(&de, sizeof(de));
de.inum = xshort(dino);
strcpy(de.name, ".");
iappend(dino, &de, sizeof(de));

bzero(&de, sizeof(de));
de.inum = xshort(parentino);
strcpy(de.name, "..");
iappend(dino, &de, sizeof(de));

int dir_fd = -1;
if ((dir_fd = openat(parent_fd, dirname, O_RDONLY)) == -1) {
perror("open");
return dino;
}

DIR *d = fdopendir(dir_fd);
if (d == NULL) {
perror("fdopendir");
return dino;
}

struct dirent *e;
while ((e = readdir(d)) != NULL) {
if (e->d_name[0] == '.') {
continue;
}

if (e->d_type == DT_REG) {
printf("%*s+ %s\n", level * 2, "", e->d_name);
add_file(dir_fd, dino, e->d_name);
} else if (e->d_type == DT_DIR) {
printf("%*s+ /%s\n", level * 2, "", e->d_name);
add_dir(level + 1, dir_fd, dino, e->d_name);
}
}
close(dir_fd);

return dino;
}

int
main(int argc, char *argv[])
{
int i, cc, fd;
uint rootino, inum, off;
struct dirent de;
struct xv6_dirent de;
char buf[BSIZE];
struct dinode din;

Expand All @@ -83,7 +165,7 @@ main(int argc, char *argv[])
}

assert((BSIZE % sizeof(struct dinode)) == 0);
assert((BSIZE % sizeof(struct dirent)) == 0);
assert((BSIZE % sizeof(struct xv6_dirent)) == 0);

fsfd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666);
if(fsfd < 0)
Expand Down Expand Up @@ -128,6 +210,13 @@ main(int argc, char *argv[])
iappend(rootino, &de, sizeof(de));

for(i = 2; i < argc; i++){
struct stat sb;
stat(argv[i], &sb);
if (S_ISDIR(sb.st_mode)) {
add_dir(0, AT_FDCWD, rootino, argv[i]);
continue;
}

// get rid of "user/"
char *shortname;
if(strncmp(argv[i], "user/", 5) == 0)
Expand Down
1 change: 1 addition & 0 deletions testfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hi
Loading