Sfoglia il codice sorgente

[add] apply_fs write to file

isundil 9 anni fa
parent
commit
c15c1f7525
6 ha cambiato i file con 51 aggiunte e 15 eliminazioni
  1. 1 1
      CMakeLists.txt
  2. 36 5
      src/apply_fs.c
  3. 0 4
      src/ovr_syscall/ovr_open.c
  4. 2 4
      src/ovr_syscall/ovr_write.c
  5. 2 0
      test/common.h
  6. 10 1
      test/write/main.c

+ 1 - 1
CMakeLists.txt

@@ -18,7 +18,7 @@ add_executable(test_sllist test/sllist/main.c)
 set_property(TARGET test_sllist PROPERTY RUNTIME_OUTPUT_DIRECTORY test/bin)
 target_link_libraries(test_sllist sllist)
 
-add_executable(test_write test/write/main.c test/write/functions.c src/ovr_syscall/ovr_close.c src/ovr_syscall/ovr_open.c src/ovr_syscall/ovr_write.c src/ovr_syscall/ovr_lseek.c src/ovr_syscall/ovr_read.c src/pathutil.c src/registers.c)
+add_executable(test_write test/write/main.c test/write/functions.c src/ovr_syscall/ovr_close.c src/ovr_syscall/ovr_open.c src/ovr_syscall/ovr_write.c src/ovr_syscall/ovr_lseek.c src/ovr_syscall/ovr_read.c src/pathutil.c src/registers.c src/apply_fs.c)
 set_property(TARGET test_write PROPERTY RUNTIME_OUTPUT_DIRECTORY test/bin)
 target_link_libraries(test_write sllist)
 

+ 36 - 5
src/apply_fs.c

@@ -1,13 +1,44 @@
+#include <sys/sendfile.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <errno.h>
+#include <fcntl.h>
 #include "sandbox.h"
 
+struct s_file_wrapper {
+    struct s_sandboxenv *env;
+    t_fileinfo *current_file;
+    int outfd;
+};
+
+int apply_page(void **_page, void *_wrapper)
+{
+    struct s_file_wrapper *wrapper = (struct s_file_wrapper *) _wrapper;
+    t_filepage *page = (t_filepage *)*_page;
+    size_t bwritten;
+
+    lseek(wrapper->outfd, page->offset_start, SEEK_SET);
+    bwritten = sendfile(wrapper->outfd, wrapper->current_file->local_fd, NULL, page->len);
+    if (bwritten != page->len)
+        fprintf(wrapper->env->errorOutput, "Error: write error to %s\n", wrapper->current_file->filename);
+    return 0;
+}
+
 int apply_file(void **_data, void *_env)
 {
-    struct s_sandboxenv *env = * (struct s_sandboxenv **) _env;
-    t_fileinfo *fi = (t_fileinfo *) _data;
+    struct s_sandboxenv *env = (struct s_sandboxenv *) _env;
+    t_fileinfo *fi = (t_fileinfo *) *_data;
+    struct s_file_wrapper wrapper;
 
-#warning todo
-    (void) fi;
-    (void) env;
+    lseek(fi->local_fd, 0, SEEK_SET);
+    wrapper.env = env;
+    wrapper.current_file = fi;
+    wrapper.outfd = open(fi->filename, O_WRONLY, 0644);
+    if (wrapper.outfd == -1)
+        fprintf(env->errorOutput, "Error: write error to %s (%s)\n", fi->filename, strerror(errno));
+    else
+        sllist_foreach(fi->modif_write, apply_page, &wrapper);
     return 0;
 }
 

+ 0 - 4
src/ovr_syscall/ovr_open.c

@@ -40,8 +40,6 @@ int ovr_open(struct s_sandboxenv *env)
         ro = 0;
     if (!fileentry && ro)
     {
-        printf("DO open %s (unmanaged)\n", pathname);
-        fflush(stdout);
         free(pathname);
         return 0;
     }
@@ -56,8 +54,6 @@ int ovr_open(struct s_sandboxenv *env)
     fd->mod = mode;
     sllist_pushback(fileentry->fds, fd);
 
-    printf("DO open ! ([%s], [%d], [%d])%s = %d\n", pathname, flags, mode, ro ? "- RO" : "- RW", (int) env->syscall_no.syscall_return);
-    fflush(stdout);
     free(pathname);
     return 0;
 }

+ 2 - 4
src/ovr_syscall/ovr_write.c

@@ -16,12 +16,10 @@ int ovr_write(struct s_sandboxenv *env)
 
     if (!file)
         return 0;
-    result = sllist_find(file->fds, fileinfo_compare_tfd, (void *)env->syscall_args[0]) != -1;
-    if (!result)
+    result = sllist_find(file->fds, fileinfo_compare_tfd, (void *)env->syscall_args[0]);
+    if (result == -1)
         return 0;
     _fd = sllist_at(file->fds, result);
-    printf("DO WRITE ! ([%s], [%s], [%d])\n", file->filename, buf, buflen);
-    fflush(stdout);
     result = write(file->local_fd, buf, buflen);
     registerSetResult(env, result);
     page = malloc(sizeof(*page));

+ 2 - 0
test/common.h

@@ -15,6 +15,8 @@
 
 #define _assertDiff(a, b) { REGISTER_TYPE _a = (REGISTER_TYPE) a; REGISTER_TYPE _b = (REGISTER_TYPE) b; if(_a == _b) { fprintf(stderr, "File %s, line %d: fail asserting %lld is different than %lld\n", __FILE__, __LINE__, (long long int) _a, (long long int )_b); return -1; }}
 
+#define _assertStrNEqual(a, b, c) { if (strncmp(a, b, c) != 0) { fprintf(stderr, "File %s, line %d: fail asserting %s match expected string %s\n", __FILE__, __LINE__, a, b); return -1; }}
+
 void tests_init_env(struct s_sandboxenv *env, t_param *params);
 void tests_release_env(struct s_sandboxenv *env, t_param *params);
 

+ 10 - 1
test/write/main.c

@@ -113,15 +113,24 @@ int test_write(struct s_sandboxenv *env, int fd)
 {
     struct stat st;
     char buf[4];
+    int lfd;
 
     _assertEqual(fakewrite(fd, "test", 4, env), 4);
     _assertEqual(stat("_test", &st), 0);
     _assertEqual(st.st_size, 0);
 #warning TODO
-    return 0;
+    /*
     _assertEqual(fakelseek(fd, 0, SEEK_SET, env), 0);
     _assertEqual(fakeread(fd, buf, 4, env), 4);
     _assertEqual(strcmp(buf, "test"), 0);
+    */
+    apply_fs(env);
+    _assertEqual(fakewrite(fd, "test", 4, env), 4);
+    _assertEqual(stat("_test", &st), 0);
+    _assertEqual(st.st_size, 4);
+    lfd = open("_test", O_RDONLY);
+    read(lfd, buf, 4);
+    _assertStrNEqual(buf, "test", 4);
     return 0;
 }