Skip to content

Commit df7350d

Browse files
committed
Initial commit
0 parents  commit df7350d

File tree

10 files changed

+1014
-0
lines changed

10 files changed

+1014
-0
lines changed

.editorconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
root = true
3+
4+
[**.{c,h,sh}]
5+
indent_style = space
6+
indent_size = 2
7+

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.o
2+
memfs

Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
CC=gcc
3+
CFLAGS=-g -Wall -D_FILE_OFFSET_BITS=64
4+
LDFLAGS=-L~/fuse-2.9.5/lib/ -lfuse
5+
6+
OBJ=memfs.o node.o dir.o
7+
8+
%.o: %.c
9+
$(CC) -c $(CFLAGS) $< -o $@
10+
11+
memfs: $(OBJ)
12+
$(CC) $(OBJ) $(LDFLAGS) -o memfs
13+
14+
.PHONY: clean
15+
clean:
16+
rm -f *.o memfs
17+

debug.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
set -e
3+
make
4+
gdb --args ./memfs -d -s -o default_permissions -o auto_unmount testmount

dir.c

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
2+
#include <errno.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
6+
#include "dir.h"
7+
8+
int dir_add(struct node *dirnode, struct direntry *entry, int replace, int *added) {
9+
struct direntry **dir = (struct direntry **) &dirnode->data;
10+
struct direntry *existing_entry;
11+
12+
if(dir_find(dirnode, entry->name, strlen(entry->name), &existing_entry)) {
13+
if(replace) {
14+
*added = 0;
15+
existing_entry->node = entry->node;
16+
return 1;
17+
} else {
18+
errno = EEXIST;
19+
return 0;
20+
}
21+
}
22+
23+
*added = 1;
24+
25+
if(*dir == NULL) {
26+
*dir = entry;
27+
entry->next = NULL;
28+
} else {
29+
entry->next = *dir;
30+
*dir = entry;
31+
}
32+
33+
// The entry is now linked in the directory
34+
entry->node->vstat.st_nlink++;
35+
36+
// If the entry is a directory, .. is an implicit hardlink to the parent
37+
// directory.
38+
if(S_ISDIR(entry->node->vstat.st_mode)) {
39+
dirnode->vstat.st_nlink++;
40+
}
41+
42+
return 1;
43+
}
44+
45+
int dir_add_alloc(struct node *dirnode, const char *name, struct node *node, int replace) {
46+
struct direntry *entry = malloc(sizeof(struct direntry));
47+
int added;
48+
49+
if(!entry) {
50+
errno = ENOMEM;
51+
return 0;
52+
}
53+
54+
strcpy(entry->name, name);
55+
entry->node = node;
56+
57+
if(!dir_add(dirnode, entry, replace, &added)) {
58+
free(entry);
59+
return 0;
60+
}
61+
62+
if(!added) free(entry);
63+
64+
return 1;
65+
}
66+
67+
int dir_remove(struct node *dirnode, const char *name) {
68+
struct direntry **dir = (struct direntry **) &dirnode->data;
69+
70+
struct direntry *ent = *dir;
71+
struct direntry **ptr = dir;
72+
73+
while(ent != NULL) {
74+
if(strcmp(ent->name, name) == 0) {
75+
*ptr = ent->next;
76+
77+
// See dir_add for details
78+
if(S_ISDIR(ent->node->vstat.st_mode)) {
79+
dirnode->vstat.st_nlink--;
80+
}
81+
82+
free(ent);
83+
84+
return 1;
85+
}
86+
87+
ptr = &ent->next;
88+
ent = ent->next;
89+
}
90+
91+
errno = ENOENT;
92+
93+
return 0;
94+
}
95+
96+
int dir_find(struct node *dirnode, const char *name, int namelen, struct direntry **entry) {
97+
struct direntry *ent = (struct direntry *) dirnode->data;
98+
99+
while(ent != NULL) {
100+
if(strlen(ent->name) == namelen) {
101+
if(strncmp(ent->name, name, namelen) == 0) {
102+
if(entry != NULL) *entry = ent;
103+
return 1;
104+
}
105+
}
106+
ent = ent->next;
107+
}
108+
109+
errno = ENOENT;
110+
111+
return 0;
112+
}
113+

dir.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
#include <linux/limits.h>
3+
#include "node.h"
4+
5+
#ifndef _DIR_H
6+
#define _DIR_H
7+
8+
struct direntry {
9+
char name[PATH_MAX];
10+
struct node *node;
11+
struct direntry *next;
12+
};
13+
14+
int dir_add(struct node *dir, struct direntry *entry, int replace, int *added);
15+
16+
int dir_add_alloc(struct node *dir, const char *name, struct node *node, int replace);
17+
18+
int dir_remove(struct node *dir, const char *name);
19+
20+
int dir_find(struct node *dir, const char *name, int namelen, struct direntry **entry);
21+
22+
#endif
23+

0 commit comments

Comments
 (0)