From ccd45b229e2c155c6c8d9417297cfcbebfc3dfbb Mon Sep 17 00:00:00 2001
From: tianyi-luo <18252677381@163.com>
Date: Sun, 4 Aug 2024 21:18:50 +0800
Subject: [PATCH] fix(busybox): case 46 rm

---
 kernel/include/fs/buf.h |  1 +
 kernel/src/fs/bio.c     | 31 +++++++++++++++++++-
 kernel/src/fs/sysfile.c | 64 ++++++++++++++++++++++++++++++++---------
 xv6-user/busybox_test.c | 22 +++++++-------
 4 files changed, 93 insertions(+), 25 deletions(-)

diff --git a/kernel/include/fs/buf.h b/kernel/include/fs/buf.h
index 81f3141..6239c10 100644
--- a/kernel/include/fs/buf.h
+++ b/kernel/include/fs/buf.h
@@ -108,6 +108,7 @@ void ext4_eupdate(struct ext4_dirent *entry);
 void ext4_eput(struct ext4_dirent *entry);
 void ext4_elock(struct ext4_dirent *entry);
 void ext4_eunlock(struct ext4_dirent *entry);
+void ext4_eremove(struct ext4_dirent *entry, int type, char *path);
 // void ext4_kstat(struct ext4_dirent *de, struct kstat *kst);
 struct ext4_dirent *ext4_edup(struct ext4_dirent *entry);
 // void ext4_init_Hajimi(void);
diff --git a/kernel/src/fs/bio.c b/kernel/src/fs/bio.c
index 6f04f77..c37faf1 100644
--- a/kernel/src/fs/bio.c
+++ b/kernel/src/fs/bio.c
@@ -804,6 +804,35 @@ void ext4_eunlock(struct ext4_dirent *entry) {
     releasesleep(&entry->lock);
 }
 
+void ext4_eremove(struct ext4_dirent *entry, int type, char *path) {
+    printf("entry valid cnt is A %d\n", entry->valid);
+    if (entry->valid != 1) {
+        ext4_assert(0 == 1);
+        return;
+    }
+
+    if (type == 4) {
+        ext4_dir_rm(path);
+    } else {
+        int r = ext4_fremove(path);
+        // ext4_assert(0 == 1);
+    }
+
+    // ext4_assert(0 == 1);
+    // uint8 flag = EMPTY_ENTRY;
+    // uint32 off = entry->off;
+    // uint32 off2 = reloc_clus(entry->parent, off, 0);
+    // uint entcnt = 0;
+    // rw_clus(entry->parent->cur_clus, 0, 0, (uint64)&entcnt, off2, 1);
+    // entcnt &= ~LAST_LONG_ENTRY;
+
+    // for (int idx = 0; entcnt >= idx; ++idx) {
+    //     rw_clus(entry->parent->cur_clus, 1, 0, (uint64)&flag, off2, 1);
+    //     off += 32, off2 = reloc_clus(entry->parent, off, 0);
+    // }
+    entry->valid = -1;
+}
+
 void ext4_kstat(struct ext4_dirent *de, struct kstat *kst) {
     kst->st_dev = de->dev;
     //测评要求dev为1,有点奇怪
@@ -1050,7 +1079,7 @@ struct ext4_dirent *ext4_getdir_fcache(struct ext4_dirent *parent, char *name) {
     // printf("not dead3\n");
     for (ep = root_entry.prev; &root_entry != ep; ep = ep->prev) {  // LRU algo
         if (!ep->ref) {
-            ep->valid = 0;
+            ep->valid = 1;
             ep->dirty = 0;
             ep->off = 0;
             ep->dev = parent->dev;
diff --git a/kernel/src/fs/sysfile.c b/kernel/src/fs/sysfile.c
index 7c15f05..f39139e 100644
--- a/kernel/src/fs/sysfile.c
+++ b/kernel/src/fs/sysfile.c
@@ -449,6 +449,7 @@ uint64 open(char *path, int omode) {
     File->ep = ep;
 
     ext4_eunlock(ep);
+    // ext4_eput(ep);
     return fd;
 }
 
@@ -630,31 +631,68 @@ static int isdirempty(struct dirent *dp) {
     return ret == -1;
 }
 
+static int ext4_isdirempty(struct ext4_dirent *dp) {
+    int ret, count;
+    // struct ext4_dirent ep;
+    // ep.valid = 0;
+    // ret = enext(dp, &ep, 64, &count);  // skip the "." and ".."
+    if (ext4_dir_entry_next(&dp->dir) == NULL) {
+        return -1;
+    }
+    return 0;
+}
+
 // 只考虑sys_unlinkat(AT_FDCWD, path, 0);借鉴sys_remove
 uint64 sys_unlinkat(void) {
     int len;
-    struct dirent *ep;
+
+    // struct dirent *ep;
+    struct ext4_dirent *epp;
+
     char path[FAT32_MAX_PATH];
 
     if (0 >= (len = argstr(1, path, FAT32_MAX_PATH))) return -1;
 
-    char *s = len + path - 1;
-    while (*s == '/' && path <= s) {
-        --s;
-    }
-    if ((s == path || *--s == '/') && *s == '.' && s >= path) {
-        return -1;
+    // char *s = len + path - 1;
+    // while (*s == '/' && path <= s) {
+    //     --s;
+    // }
+    // if ((s == path || *--s == '/') && *s == '.' && s >= path) {
+    //     return -1;
+    // }
+    // ext4_assert(0 == 1);
+    char abs_path[FAT32_MAX_PATH];
+    if (myproc()->ext4_dir->filename == NULL) {
+        get_absolute_path(path, "/", abs_path);
+    } else {
+        get_absolute_path(path, myproc()->ext4_dir->filename, abs_path);
     }
 
-    if (NULL == (ep = ename(path))) {
+    int r = fileexists(abs_path);
+    if (r == 0) {
         return -1;
     }
-    elock(ep);
-    if ((ep->attribute & ATTR_DIRECTORY) && !isdirempty(ep)) {
-        eunlock(ep), eput(ep);
-        return -1;
+
+    printf("unlink path is %s\n", abs_path);
+
+    // ext4_assert(0 == 1);
+    epp = ext4_getdir_fcache(&(root_entry), abs_path);
+
+    ext4_elock(epp);
+    ext4_edup(epp);
+    if (r == 4) {
+        // dir
+        int ret = ext4_dir_open(&epp->dir, abs_path);
+        if (ext4_isdirempty(epp)) {
+            ext4_eunlock(epp), ext4_eput(epp);
+            return -1;
+        }
     }
-    elock(ep->parent), eremove(ep), eunlock(ep->parent), eunlock(ep), eput(ep);
+
+    // ext4_elock(epp->parent), ext4_eremove(epp, r, abs_path),
+    //     ext4_eunlock(epp->parent), ext4_eunlock(epp), ext4_eput(epp);
+
+    ext4_eremove(epp, r, abs_path), ext4_eunlock(epp), ext4_eput(epp);
 
     return 0;
 }
diff --git a/xv6-user/busybox_test.c b/xv6-user/busybox_test.c
index caefa05..e2472a2 100644
--- a/xv6-user/busybox_test.c
+++ b/xv6-user/busybox_test.c
@@ -330,17 +330,17 @@ static longtest busybox[] = {
     {0, {"busybox", "tail", "test.txt", 0}},
     {0, {"busybox", "hexdump", "-C", "test.txt", 0}},
     {0, {"busybox", "md5sum", "test.txt", 0}},
-    {1, {"busybox", "echo", "ccccccc", ">>", "test.txt", 0}},
-    {1, {"busybox", "echo", "bbbbbbb", ">>", "test.txt", 0}},
-    {1, {"busybox", "echo", "aaaaaaa", ">>", "test.txt", 0}},
-    {1, {"busybox", "echo", "2222222", ">>", "test.txt", 0}},
-    {1, {"busybox", "echo", "1111111", ">>", "test.txt", 0}},
-    {1, {"busybox", "sort", "test.txt", "|", "./busybox", "uniq", 0}},
-    {1, {"busybox", "stat", "test.txt", 0}},
-    {1, {"busybox", "strings", "test.txt", 0}},
-    {1, {"busybox", "wc", "test.txt", 0}},
-    {1, {"busybox", "[", "-f", "test.txt", "]", 0}},
-    {1, {"busybox", "more", "test.txt", 0}},
+    {0, {"busybox", "echo", "ccccccc", ">>", "test.txt", 0}},
+    {0, {"busybox", "echo", "bbbbbbb", ">>", "test.txt", 0}},
+    {0, {"busybox", "echo", "aaaaaaa", ">>", "test.txt", 0}},
+    {0, {"busybox", "echo", "2222222", ">>", "test.txt", 0}},
+    {0, {"busybox", "echo", "1111111", ">>", "test.txt", 0}},
+    {0, {"busybox", "sort", "test.txt", "|", "./busybox", "uniq", 0}},
+    {0, {"busybox", "stat", "test.txt", 0}},
+    {0, {"busybox", "strings", "test.txt", 0}},
+    {0, {"busybox", "wc", "test.txt", 0}},
+    {0, {"busybox", "[", "-f", "test.txt", "]", 0}},
+    {0, {"busybox", "more", "test.txt", 0}},
     {1, {"busybox", "rm", "test.txt", 0}},
     {1, {"busybox", "mkdir", "test_dir", 0}},
     {1, {"busybox", "mv", "test_dir", "test", 0}},
-- 
GitLab