:: commit 28dd8a106258ba4f55f104cd3b9b26265bd8787d

mintsuki <mintsuki@protonmail.com> — 2023-01-20 16:27

parents: 2091aa9c21

file: Fix several bugs to do with handling of path field

diff --git a/common/fs/file.h b/common/fs/file.h
index 6f5f682b..2efd246a 100644
--- a/common/fs/file.h
+++ b/common/fs/file.h
@@ -19,6 +19,7 @@ struct file_handle {
     bool       readall;
     struct volume *vol;
     char      *path;
+    size_t     path_len;
     void      *fd;
     void     (*read)(void *fd, void *buf, uint64_t loc, uint64_t count);
     void     (*close)(void *fd);
diff --git a/common/fs/file.s2.c b/common/fs/file.s2.c
index f109f605..aa603e6f 100644
--- a/common/fs/file.s2.c
+++ b/common/fs/file.s2.c
@@ -53,7 +53,7 @@ struct file_handle *fopen(struct volume *part, const char *filename) {
         if ((ret = tftp_open(part, "", filename)) == NULL) {
             return NULL;
         }
-        goto success;
+        return ret;
     }
 
     if ((ret = ext2_open(part, filename)) != NULL) {
@@ -70,6 +70,7 @@ struct file_handle *fopen(struct volume *part, const char *filename) {
 
 success:
     ret->path = (char *)filename;
+    ret->path_len = filename_new_len;
 
     return ret;
 }
@@ -82,6 +83,7 @@ void fclose(struct file_handle *fd) {
     } else {
         fd->close(fd);
     }
+    pmm_free(fd->path, fd->path_len);
     pmm_free(fd, sizeof(struct file_handle));
 }
 
diff --git a/common/lib/uri.c b/common/lib/uri.c
index 734bb897..e053f2cd 100644
--- a/common/lib/uri.c
+++ b/common/lib/uri.c
@@ -279,6 +279,9 @@ struct file_handle *uri_open(char *uri) {
             panic(true, "tinf error");
         }
         compressed_fd->vol = ret->vol;
+        compressed_fd->path = ext_mem_alloc(ret->path_len);
+        memcpy(compressed_fd->path, ret->path, ret->path_len);
+        compressed_fd->path_len = ret->path_len;
         fclose(ret);
         compressed_fd->is_memfile = true;
         ret = compressed_fd;
diff --git a/common/protos/limine.c b/common/protos/limine.c
index b6a82386..d65005af 100644
--- a/common/protos/limine.c
+++ b/common/protos/limine.c
@@ -227,7 +227,10 @@ static struct limine_file get_file(struct file_handle *file, char *cmdline) {
         }
     }
 
-    ret.path = reported_addr(file->path);
+    char *path = ext_mem_alloc(file->path_len);
+    memcpy(path, file->path, file->path_len);
+
+    ret.path = reported_addr(path);
 
     ret.address = reported_addr(freadall(file, MEMMAP_KERNEL_AND_MODULES));
     ret.size = file->size;
diff --git a/common/pxe/tftp.s2.c b/common/pxe/tftp.s2.c
index dc6c14d0..7fdc9829 100644
--- a/common/pxe/tftp.s2.c
+++ b/common/pxe/tftp.s2.c
@@ -69,6 +69,12 @@ struct file_handle *tftp_open(struct volume *part, const char *server_addr, cons
     handle->pxe_ip = server_ip;
     handle->pxe_port = server_port;
 
+    size_t name_len = strlen(name);
+    handle->path = ext_mem_alloc(1 + name_len + 1);
+    handle->path[0] = '/';
+    memcpy(&handle->path[1], name, name_len);
+    handle->path_len = 1 + name_len + 1;
+
     struct pxenv_open open = {
         .status = 0,
         .sip = server_ip,
@@ -179,6 +185,12 @@ struct file_handle *tftp_open(struct volume *part, const char *server_addr, cons
     handle->pxe_ip = *(uint32_t *)&ip;
     handle->pxe_port = 69;
 
+    size_t name_len = strlen(name);
+    handle->path = ext_mem_alloc(1 + name_len + 1);
+    handle->path[0] = '/';
+    memcpy(&handle->path[1], name, name_len);
+    handle->path_len = 1 + name_len + 1;
+
     handle->fd = ext_mem_alloc(handle->size);
 
     status = part->pxe_base_code->Mtftp(
tab: 248 wrap: offon