Browse Source

feat(llseek): llseek return file->seek

TODO: impl. long long version of seek()
greatbridf 11 tháng trước cách đây
mục cha
commit
635ea3e5ee
4 tập tin đã thay đổi với 15 bổ sung8 xóa
  1. 2 1
      include/kernel/tty.hpp
  2. 10 6
      src/kernel/syscall.cpp
  3. 1 1
      src/kernel/tty.cpp
  4. 2 0
      src/kernel/vfs.cpp

+ 2 - 1
include/kernel/tty.hpp

@@ -26,7 +26,8 @@ public:
     tty();
     virtual void putchar(char c) = 0;
     void print(const char* str);
-    size_t read(char* buf, size_t buf_size, size_t n);
+    ssize_t read(char* buf, size_t buf_size, size_t n);
+    ssize_t write(const char* buf, size_t n);
 
     // characters committed to buffer will be handled
     // by the input line discipline (N_TTY)

+ 10 - 6
src/kernel/syscall.cpp

@@ -678,6 +678,9 @@ int _syscall_sendfile64(interrupt_stack* data)
     std::vector<char> buf(bufsize);
     size_t totn = 0;
     while (totn < count) {
+        if (current_thread->signals.pending_signal() != 0)
+            return (totn == 0) ? -EINTR : totn;
+
         size_t n = std::min(count - totn, bufsize);
         ssize_t ret = in_file->read(buf.data(), n);
         if (ret < 0)
@@ -694,8 +697,6 @@ int _syscall_sendfile64(interrupt_stack* data)
         //       one solution is to put the sendfile action into a kernel
         //       worker and pause the calling thread so that the worker
         //       thread could be interrupted normally.
-        if (current_thread->signals.pending_signal() != 0)
-            return (totn == 0) ? -EINTR : totn;
     }
 
     return totn;
@@ -1120,15 +1121,18 @@ int _syscall_llseek(interrupt_stack* data)
     if (!result)
         return -EFAULT;
 
-    if (offset_high != 0 || offset_low != 0 || whence != SEEK_END) {
+    if (offset_high) {
         NOT_IMPLEMENTED;
         return -EINVAL;
     }
 
-    // the function is used for check file size for now
-    // we return all 0
-    *result = 0;
+    // TODO: support long long result
+    auto ret = file->seek(offset_low, whence);
+
+    if (ret < 0)
+        return ret;
 
+    *result = ret;
     return 0;
 }
 

+ 1 - 1
src/kernel/tty.cpp

@@ -66,7 +66,7 @@ int tty::poll()
     return 1;
 }
 
-size_t tty::read(char* buf, size_t buf_size, size_t n)
+ssize_t tty::read(char* buf, size_t buf_size, size_t n)
 {
     n = std::max(buf_size, n);
     size_t orig_n = n;

+ 2 - 0
src/kernel/vfs.cpp

@@ -303,6 +303,8 @@ ssize_t fs::regular_file::seek(off_t n, int whence)
     case SEEK_END:
         pos = ind->size + n;
         break;
+    default:
+        return -EINVAL;
     }
 
     if (pos > ind->size)