Quellcode durchsuchen

LibC: Add pwrite(..) implementation to match the existing pread(..)

Add a basic non-thread safe implementation of pwrite, following the
existing pread(..) design.

This is needed for https://fio.readthedocs.io
Brian Gianforcaro vor 4 Jahren
Ursprung
Commit
14f6425b8f
2 geänderte Dateien mit 21 neuen und 10 gelöschten Zeilen
  1. 20 10
      Userland/Libraries/LibC/unistd.cpp
  2. 1 0
      Userland/Libraries/LibC/unistd.h

+ 20 - 10
Userland/Libraries/LibC/unistd.cpp

@@ -289,12 +289,32 @@ ssize_t read(int fd, void* buf, size_t count)
     __RETURN_WITH_ERRNO(rc, rc, -1);
 }
 
+ssize_t pread(int fd, void* buf, size_t count, off_t offset)
+{
+    // FIXME: This is not thread safe and should be implemented in the kernel instead.
+    off_t old_offset = lseek(fd, 0, SEEK_CUR);
+    lseek(fd, offset, SEEK_SET);
+    ssize_t nread = read(fd, buf, count);
+    lseek(fd, old_offset, SEEK_SET);
+    return nread;
+}
+
 ssize_t write(int fd, const void* buf, size_t count)
 {
     int rc = syscall(SC_write, fd, buf, count);
     __RETURN_WITH_ERRNO(rc, rc, -1);
 }
 
+ssize_t pwrite(int fd, const void* buf, size_t count, off_t offset)
+{
+    // FIXME: This is not thread safe and should be implemented in the kernel instead.
+    off_t old_offset = lseek(fd, 0, SEEK_CUR);
+    lseek(fd, offset, SEEK_SET);
+    ssize_t nwritten = write(fd, buf, count);
+    lseek(fd, old_offset, SEEK_SET);
+    return nwritten;
+}
+
 int ttyname_r(int fd, char* buffer, size_t size)
 {
     int rc = syscall(SC_ttyname, fd, buffer, size);
@@ -769,16 +789,6 @@ int unveil(const char* path, const char* permissions)
     __RETURN_WITH_ERRNO(rc, rc, -1);
 }
 
-ssize_t pread(int fd, void* buf, size_t count, off_t offset)
-{
-    // FIXME: This is not thread safe and should be implemented in the kernel instead.
-    off_t old_offset = lseek(fd, 0, SEEK_CUR);
-    lseek(fd, offset, SEEK_SET);
-    ssize_t nread = read(fd, buf, count);
-    lseek(fd, old_offset, SEEK_SET);
-    return nread;
-}
-
 char* getpass(const char* prompt)
 {
     dbgln("FIXME: getpass('{}')", prompt);

+ 1 - 0
Userland/Libraries/LibC/unistd.h

@@ -101,6 +101,7 @@ int tcsetpgrp(int fd, pid_t pgid);
 ssize_t read(int fd, void* buf, size_t count);
 ssize_t pread(int fd, void* buf, size_t count, off_t);
 ssize_t write(int fd, const void* buf, size_t count);
+ssize_t pwrite(int fd, const void* buf, size_t count, off_t);
 int close(int fd);
 int chdir(const char* path);
 int fchdir(int fd);