Skip to content

Commit eb31363

Browse files
committed
Introduce file executable flag in FFS and expose it to user app
1 parent c83850b commit eb31363

File tree

8 files changed

+73
-14
lines changed

8 files changed

+73
-14
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ NASM=nasm -f elf32 -i include/ $(NASM_DEBUG)
7979

8080
CC=gcc -std=c11 -fno-builtin -Os -nostartfiles -nostdlib -static $(CC_DEBUG)
8181
KERNEL_CC = $(CC) -m32 -fno-pie -Isrc --sysroot=$(BUILD_DIR) -Iinclude -Isrc/usr/include
82-
USER_CC = $(CC) -m32 -fno-pie -Isrc --sysroot=$(BUILD_DIR)
82+
USER_CC = $(CC) -m32 -fno-pie -Isrc --sysroot=$(BUILD_DIR) -Isrc/usr/include
8383

8484
LD=ld -nostdlib -nostartfiles -nodefaultlibs $(LD_DEBUG)
8585
KERNEL_LD=$(LD) -m elf_i386 -T linker.ld -Ttext 0x0

external/src/mkfs_ffs.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ void write_first_block(FILE *out) {
1818
fwrite(block.bytes, 1, sizeof(block.bytes), out);
1919
}
2020

21-
void write_file(int file_id, FILE *outfile, int *outfile_nextdata_block, const char *filename, FILE *srcfile) {
21+
void write_file(int file_id, FILE *outfile, int *outfile_nextdata_block,
22+
const char *filename, FILE *srcfile,
23+
int is_executable) {
2224
fseek(srcfile, 0L, SEEK_END);
2325
int file_size = ftell(srcfile);
2426
rewind(srcfile);
@@ -27,6 +29,10 @@ void write_file(int file_id, FILE *outfile, int *outfile_nextdata_block, const c
2729
strncpy(entry.content.filename, filename, sizeof(entry.content.filename));
2830
entry.content.filesize = file_size;
2931
entry.content.start_block_id = (*outfile_nextdata_block);
32+
entry.content.flags = 0;
33+
if (is_executable) {
34+
entry.content.flags |= FFS_FILE_FLAG_EXECUTABLE;
35+
}
3036

3137
// Write file entry
3238
fseek(outfile, FILEENTRY_LOCATION(file_id), SEEK_SET);
@@ -88,9 +94,10 @@ int create_partition(char *src_dir, char *out_filepath) {
8894
fprintf("skipping non-regular file '%s': %d", buffer_filename, file_stat.st_mode);
8995
continue;
9096
}
97+
int is_executable = file_stat.st_mode & S_IXUSR;
9198

9299
FILE *file_src = fopen(buffer_filename, "rb");
93-
write_file(file_id++, out, &outfile_nextdata_block, de->d_name, file_src);
100+
write_file(file_id++, out, &outfile_nextdata_block, de->d_name, file_src, is_executable);
94101
fclose(file_src);
95102
}
96103
while (file_id < FS_FFS_FILEENTRY_COUNT) {

include/fuzzy/fs/ffs.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ Number of file which can be stored: 64
1616
*/
1717

1818
#include <stdint.h>
19+
#include <stddef.h>
1920

20-
#define FS_FFS_FILENAME_LIMIT 120 // including NULL
21+
#define FS_FFS_FILENAME_LIMIT 100 // including NULL, same as dirent.h
2122
#define FFS_UNIQUE_PARITION_ID 0 // only paritition 0 is supported for now
2223

24+
enum FFSFileFlagMask {
25+
FFS_FILE_FLAG_EXECUTABLE = 1 << 0
26+
};
27+
2328
union FFSMetaData {
2429
struct {
2530
char signature[16];
@@ -32,6 +37,7 @@ union FFSFileEntry {
3237
int32_t start_block_id; // 0 implies no file.
3338
int32_t filesize;
3439
char filename[FS_FFS_FILENAME_LIMIT];
40+
uint32_t flags;
3541
} content;
3642
char bytes[128];
3743
}; // size: 128 bytes

src/kernel/syscall/file_handler.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <stddef.h>
44
#include <string.h>
55
#include <stdio.h>
6+
#include <dirent.h>
67

78
#include <lib/utils/logging.h>
89

@@ -64,7 +65,8 @@ int _file_handler_read(int user_ds, int file_id, char _us_buffer[FILEIO_BUFFER_S
6465
return len;
6566
}
6667

67-
int _file_handler_read_dir(int user_ds, int last_file_id, char *_us_filename) {
68+
int _file_handler_read_dir(int user_ds, int last_file_id, struct dirent *_us_dirent) {
69+
// char *_us_filename
6870
union FFSFileEntry entry;
6971
int file_id;
7072
if (last_file_id < 0) {
@@ -80,9 +82,16 @@ int _file_handler_read_dir(int user_ds, int last_file_id, char *_us_filename) {
8082
continue;
8183
}
8284
// next file
85+
struct dirent centry = {0};
86+
memcpy(centry.d_name, entry.content.filename, sizeof(centry.d_name));
87+
centry.size = entry.content.filesize;
88+
if(entry.content.flags&FFS_FILE_FLAG_EXECUTABLE) {
89+
centry.flag |= DIRENT_EXECUTABLE;
90+
}
91+
8392
syscall_strncpy_kernel_to_user(
84-
user_ds, _us_filename,
85-
entry.content.filename, sizeof(entry.content.filename));
93+
user_ds, _us_dirent,
94+
&centry, sizeof(struct dirent));
8695
return file_id;
8796
}
8897
return -1; // there is no next file
@@ -95,7 +104,7 @@ int syscall_2_file_handler(int operation, int a1, int a2, int a3, int user_ds) {
95104
case SYSCALL_FILE_SUB_READBUFFER:
96105
return _file_handler_read(user_ds, a1, (char*)a2, a3);
97106
case SYSCALL_FILE_SUB_READ_DIR:
98-
return _file_handler_read_dir(user_ds, a1, (char*)a2);
107+
return _file_handler_read_dir(user_ds, a1, (struct dirent*)a2);
99108
}
100109
return -1;
101110
}

src/usr/include/dirent.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
#pragma once
22

3-
#define FILENAME_LIMIT 120 // same as fuzzy/fs/ffs.h
3+
#include <stddef.h>
4+
5+
#define FILENAME_LIMIT 100 // same as fuzzy/fs/ffs.h
6+
7+
enum dirent_flag {
8+
DIRENT_EXECUTABLE = 1 << 0
9+
};
410

511
struct dirent {
612
// In FFS the only file types are only normal file.
713
char d_name[FILENAME_LIMIT];
14+
uint32_t size;
15+
uint32_t flag;
816
};
917

1018
struct DIR {

src/usr/lib/dirent.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct dirent *readdir(struct DIR *dirp) {
1111
SYSCALL_FILE_OP,
1212
SYSCALL_FILE_SUB_READ_DIR,
1313
dirp->kernel_file_id,
14-
dirp->entry.d_name);
14+
&dirp->entry);
1515
if (dirp->kernel_file_id == -1) {
1616
return NULL;
1717
}

src/usr/local/src/cat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void process(char filename[]) {
2020
}
2121

2222
int print_usage() {
23-
printf("Usage: cat <filename>");
23+
printf("Usage: cat <filename>\n");
2424
return 0;
2525
}
2626

src/usr/local/src/ls.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,46 @@
22
#include <stdio.h>
33
#include <dirent.h>
44

5-
void process() {
5+
void process(int is_long_listing) {
66
struct DIR dir;
77
opendir(&dir);
88
struct dirent *dp;
99

1010
while ((dp = readdir(&dir)) !=NULL) {
11-
printf("%s\n", dp->d_name);
11+
if(is_long_listing) {
12+
printf("r-%c %d %s\n",
13+
(dp->flag & DIRENT_EXECUTABLE)?'x':'-',
14+
dp->size,
15+
dp->d_name
16+
);
17+
} else {
18+
printf("%s\n", dp->d_name);
19+
}
1220
}
1321
}
1422

23+
int print_usage() {
24+
printf("Usage: ls [-h] [-l]\n");
25+
return 0;
26+
}
27+
1528
int main(int argc,char *argv[]) {
16-
process();
29+
int is_long_listing = 0;
30+
31+
// command line argument parsing
32+
for (size_t i = 1; i < argc; i++) {
33+
if(strcmp(argv[i],"-h")==0) {
34+
print_usage();
35+
return 0;
36+
} else if(strcmp(argv[i],"-l")==0) {
37+
is_long_listing=1;
38+
} else {
39+
printf("unrecognized flag: '%s'\n", argv[i]);
40+
print_usage();
41+
return 1;
42+
}
43+
}
44+
45+
process(is_long_listing);
1746
return 0;
1847
}

0 commit comments

Comments
 (0)