1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-28 23:22:11 +00:00

LibCore: Make File take String instead of StringView

This commit is contained in:
Andreas Kling 2021-04-17 00:48:31 +02:00
parent 510aad6515
commit a1a6d30b54
2 changed files with 15 additions and 17 deletions

View file

@ -46,17 +46,17 @@
namespace Core { 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)) if (!file->open_impl(mode, permissions))
return String(file->error_string()); return String(file->error_string());
return file; return file;
} }
File::File(const StringView& filename, Object* parent) File::File(String filename, Object* parent)
: IODevice(parent) : IODevice(parent)
, m_filename(filename) , m_filename(move(filename))
{ {
} }
@ -191,12 +191,12 @@ bool File::ensure_parent_directories(const String& path)
#ifdef __serenity__ #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. // First, try using a 64-byte buffer, that ought to be enough for anybody.
char small_buffer[64]; 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) if (rc < 0)
return {}; return {};
@ -210,7 +210,7 @@ String File::read_link(const StringView& link_path)
char* large_buffer_ptr; char* large_buffer_ptr;
auto large_buffer = StringImpl::create_uninitialized(size, 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) if (rc < 0)
return {}; 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 // 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. // 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 = {};
struct stat statbuf; int rc = lstat(link_path.characters(), &statbuf);
int rc = lstat(link_path_str.characters(), &statbuf);
if (rc < 0) if (rc < 0)
return {}; return {};
char* buffer_ptr; char* buffer_ptr;
auto buffer = StringImpl::create_uninitialized(statbuf.st_size, buffer_ptr); auto buffer = StringImpl::create_uninitialized(statbuf.st_size, buffer_ptr);
rc = readlink(link_path_str.characters(), buffer_ptr, statbuf.st_size); if (readlink(link_path.characters(), buffer_ptr, statbuf.st_size) < 0)
if (rc < 0)
return {}; return {};
// (See above.) // (See above.)
if (rc == statbuf.st_size) if (rc == statbuf.st_size)

View file

@ -39,10 +39,10 @@ class File final : public IODevice {
public: public:
virtual ~File() override; 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; } 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; bool is_directory() const;
static bool is_directory(const String& filename); 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 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 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); static Result<void, OSError> link_file(const String& dst_path, const String& src_path);
struct RemoveError { struct RemoveError {
@ -104,7 +104,7 @@ private:
: IODevice(parent) : IODevice(parent)
{ {
} }
explicit File(const StringView&, Object* parent = nullptr); explicit File(String filename, Object* parent = nullptr);
bool open_impl(IODevice::OpenMode, mode_t); bool open_impl(IODevice::OpenMode, mode_t);