Bläddra i källkod

LibCore: Make File take String instead of StringView

Andreas Kling 4 år sedan
förälder
incheckning
a1a6d30b54
2 ändrade filer med 15 tillägg och 17 borttagningar
  1. 11 13
      Userland/Libraries/LibCore/File.cpp
  2. 4 4
      Userland/Libraries/LibCore/File.h

+ 11 - 13
Userland/Libraries/LibCore/File.cpp

@@ -46,17 +46,17 @@
 
 namespace Core {
 
-Result<NonnullRefPtr<File>, String> File::open(const String& filename, IODevice::OpenMode mode, mode_t permissions)
+Result<NonnullRefPtr<File>, String> File::open(String filename, IODevice::OpenMode mode, mode_t permissions)
 {
-    auto file = File::construct(filename);
+    auto file = File::construct(move(filename));
     if (!file->open_impl(mode, permissions))
         return String(file->error_string());
     return file;
 }
 
-File::File(const StringView& filename, Object* parent)
+File::File(String filename, Object* parent)
     : IODevice(parent)
-    , m_filename(filename)
+    , m_filename(move(filename))
 {
 }
 
@@ -191,12 +191,12 @@ bool File::ensure_parent_directories(const String& path)
 
 #ifdef __serenity__
 
-String File::read_link(const StringView& link_path)
+String File::read_link(String const& link_path)
 {
     // First, try using a 64-byte buffer, that ought to be enough for anybody.
     char small_buffer[64];
 
-    int rc = serenity_readlink(link_path.characters_without_null_termination(), link_path.length(), small_buffer, sizeof(small_buffer));
+    int rc = serenity_readlink(link_path.characters(), link_path.length(), small_buffer, sizeof(small_buffer));
     if (rc < 0)
         return {};
 
@@ -210,7 +210,7 @@ String File::read_link(const StringView& link_path)
     char* large_buffer_ptr;
     auto large_buffer = StringImpl::create_uninitialized(size, large_buffer_ptr);
 
-    rc = serenity_readlink(link_path.characters_without_null_termination(), link_path.length(), large_buffer_ptr, size);
+    rc = serenity_readlink(link_path.characters(), link_path.length(), large_buffer_ptr, size);
     if (rc < 0)
         return {};
 
@@ -232,17 +232,15 @@ String File::read_link(const StringView& link_path)
 
 // This is a sad version for other systems. It has to always make a copy of the
 // link path, and to always make two syscalls to get the right size first.
-String File::read_link(const StringView& link_path)
+String File::read_link(String const& link_path)
 {
-    String link_path_str = link_path;
-    struct stat statbuf;
-    int rc = lstat(link_path_str.characters(), &statbuf);
+    struct stat statbuf = {};
+    int rc = lstat(link_path.characters(), &statbuf);
     if (rc < 0)
         return {};
     char* buffer_ptr;
     auto buffer = StringImpl::create_uninitialized(statbuf.st_size, buffer_ptr);
-    rc = readlink(link_path_str.characters(), buffer_ptr, statbuf.st_size);
-    if (rc < 0)
+    if (readlink(link_path.characters(), buffer_ptr, statbuf.st_size) < 0)
         return {};
     // (See above.)
     if (rc == statbuf.st_size)

+ 4 - 4
Userland/Libraries/LibCore/File.h

@@ -39,10 +39,10 @@ class File final : public IODevice {
 public:
     virtual ~File() override;
 
-    static Result<NonnullRefPtr<File>, String> open(const String& filename, IODevice::OpenMode, mode_t = 0644);
+    static Result<NonnullRefPtr<File>, String> open(String filename, IODevice::OpenMode, mode_t = 0644);
 
     String filename() const { return m_filename; }
-    void set_filename(const StringView& filename) { m_filename = filename; }
+    void set_filename(const String filename) { m_filename = move(filename); }
 
     bool is_directory() const;
     static bool is_directory(const String& filename);
@@ -78,7 +78,7 @@ public:
     static Result<void, CopyError> copy_file_or_directory(const String& dst_path, const String& src_path, RecursionMode = RecursionMode::Allowed, LinkMode = LinkMode::Disallowed, AddDuplicateFileMarker = AddDuplicateFileMarker::Yes);
 
     static String real_path_for(const String& filename);
-    static String read_link(const StringView& link_path);
+    static String read_link(String const& link_path);
     static Result<void, OSError> link_file(const String& dst_path, const String& src_path);
 
     struct RemoveError {
@@ -104,7 +104,7 @@ private:
         : IODevice(parent)
     {
     }
-    explicit File(const StringView&, Object* parent = nullptr);
+    explicit File(String filename, Object* parent = nullptr);
 
     bool open_impl(IODevice::OpenMode, mode_t);