From c8d6c1ddc33224c4a55d80a4d762b5040487d373 Mon Sep 17 00:00:00 2001 From: Tobias Alam Date: Thu, 7 Nov 2024 15:36:33 -0500 Subject: [PATCH 1/4] file system api c++: added support for creating temp. files --- libopenage/util/path.cpp | 11 +++++++++++ libopenage/util/path.h | 3 +-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/libopenage/util/path.cpp b/libopenage/util/path.cpp index ff23253533..34ed9adbba 100644 --- a/libopenage/util/path.cpp +++ b/libopenage/util/path.cpp @@ -3,6 +3,7 @@ #include "path.h" #include +#include #include "../error/error.h" #include "compiler.h" @@ -11,6 +12,7 @@ #include "fslike/python.h" #include "misc.h" #include "strings.h" +#include "file.h" namespace openage::util { @@ -392,5 +394,14 @@ std::string dirname(const std::string &fullpath) { } } +static File get_temp_file() { + std::FILE* tmp_file = std::tmpfile(); + int temp_fd = fileno(tmp_file); + std::string tf_path = "/proc/self/fd/" + std::to_string(temp_fd); + mode_t mode = 0777; + File file_wrapper = File(tf_path, mode); + return file_wrapper; +} + } // namespace openage::util diff --git a/libopenage/util/path.h b/libopenage/util/path.h index d16e1734c6..f9161343d2 100644 --- a/libopenage/util/path.h +++ b/libopenage/util/path.h @@ -96,7 +96,7 @@ class OAAPI Path { File open_rw() const; File open_a() const; File open_ar() const; - + static File get_temp_file(); /** * Resolve the native path by flattening all underlying * filesystem objects (like unions). @@ -137,7 +137,6 @@ class OAAPI Path { Path operator[](const parts_t &subpaths) const; Path operator[](const part_t &subpath) const; Path operator/(const part_t &subpath) const; - Path with_name(const part_t &name) const; Path with_suffix(const part_t &suffix) const; From 1195687eaf5099d68e25bc6e92453917ceaf2ec3 Mon Sep 17 00:00:00 2001 From: Tobias Alam Date: Sat, 9 Nov 2024 14:22:45 -0500 Subject: [PATCH 2/4] placed static temp file creation method in file.cpp, created temp directory creation method in directory.cpp --- libopenage/util/file.cpp | 8 ++++++++ libopenage/util/file.h | 3 ++- libopenage/util/fslike/directory.cpp | 8 ++++++++ libopenage/util/fslike/directory.h | 6 +++--- libopenage/util/path.cpp | 11 ----------- libopenage/util/path.h | 3 ++- 6 files changed, 23 insertions(+), 16 deletions(-) diff --git a/libopenage/util/file.cpp b/libopenage/util/file.cpp index 5aa630d4cc..10fcfe6910 100644 --- a/libopenage/util/file.cpp +++ b/libopenage/util/file.cpp @@ -121,5 +121,13 @@ std::ostream &operator<<(std::ostream &stream, const File &file) { return stream; } +static File get_temp_file() { + std::FILE* tmp_file = std::tmpfile(); + int temp_fd = fileno(tmp_file); + std::string tf_path = "/proc/self/fd/" + std::to_string(temp_fd); + mode_t mode = 0777; + File file_wrapper = File(tf_path, mode); + return file_wrapper; +} } // namespace openage::util diff --git a/libopenage/util/file.h b/libopenage/util/file.h index 6724b5f795..f5339adf67 100644 --- a/libopenage/util/file.h +++ b/libopenage/util/file.h @@ -98,8 +98,9 @@ class OAAPI File { void flush(); ssize_t size(); std::vector get_lines(); - std::shared_ptr get_fileobj() const; + + static File get_temp_file(); protected: std::shared_ptr filelike; diff --git a/libopenage/util/fslike/directory.cpp b/libopenage/util/fslike/directory.cpp index 420757f3a6..5695b3fc13 100644 --- a/libopenage/util/fslike/directory.cpp +++ b/libopenage/util/fslike/directory.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -292,4 +293,11 @@ std::ostream &Directory::repr(std::ostream &stream) { return stream; } +static Directory get_temp_directory() { + std::string temp_dir_path = std::filesystem::temp_directory_path() / std::tmpnam(nullptr); + bool create = true; + Directory directory = Directory(temp_dir_path, create); + return directory; +} + } // namespace openage::util::fslike diff --git a/libopenage/util/fslike/directory.h b/libopenage/util/fslike/directory.h index 2872b9554b..9fd1e2a020 100644 --- a/libopenage/util/fslike/directory.h +++ b/libopenage/util/fslike/directory.h @@ -47,6 +47,8 @@ class Directory : public FSLike { uint64_t get_filesize(const Path::parts_t &parts) override; std::ostream &repr(std::ostream &) override; + + static Directory get_temp_directory(); protected: /** @@ -54,12 +56,10 @@ class Directory : public FSLike { * basically basepath + "/".join(parts) */ std::string resolve(const Path::parts_t &parts) const; - std::tuple do_stat(const Path::parts_t &parts) const; - + std::string basepath; }; - } // namespace fslike } // namespace util } // namespace openage diff --git a/libopenage/util/path.cpp b/libopenage/util/path.cpp index 34ed9adbba..ff23253533 100644 --- a/libopenage/util/path.cpp +++ b/libopenage/util/path.cpp @@ -3,7 +3,6 @@ #include "path.h" #include -#include #include "../error/error.h" #include "compiler.h" @@ -12,7 +11,6 @@ #include "fslike/python.h" #include "misc.h" #include "strings.h" -#include "file.h" namespace openage::util { @@ -394,14 +392,5 @@ std::string dirname(const std::string &fullpath) { } } -static File get_temp_file() { - std::FILE* tmp_file = std::tmpfile(); - int temp_fd = fileno(tmp_file); - std::string tf_path = "/proc/self/fd/" + std::to_string(temp_fd); - mode_t mode = 0777; - File file_wrapper = File(tf_path, mode); - return file_wrapper; -} - } // namespace openage::util diff --git a/libopenage/util/path.h b/libopenage/util/path.h index f9161343d2..d16e1734c6 100644 --- a/libopenage/util/path.h +++ b/libopenage/util/path.h @@ -96,7 +96,7 @@ class OAAPI Path { File open_rw() const; File open_a() const; File open_ar() const; - static File get_temp_file(); + /** * Resolve the native path by flattening all underlying * filesystem objects (like unions). @@ -137,6 +137,7 @@ class OAAPI Path { Path operator[](const parts_t &subpaths) const; Path operator[](const part_t &subpath) const; Path operator/(const part_t &subpath) const; + Path with_name(const part_t &name) const; Path with_suffix(const part_t &suffix) const; From 8c525dddfcd88679c3540cc7fbb8f34a7d9a0732 Mon Sep 17 00:00:00 2001 From: Tobias Alam Date: Wed, 13 Nov 2024 10:26:53 -0500 Subject: [PATCH 3/4] modified file.cpp to utilize get_temp_dir from directory.cpp and then place a file into it --- libopenage/util/file.cpp | 10 ++++++---- libopenage/util/fslike/directory.h | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/libopenage/util/file.cpp b/libopenage/util/file.cpp index 10fcfe6910..3d0d4032ac 100644 --- a/libopenage/util/file.cpp +++ b/libopenage/util/file.cpp @@ -16,6 +16,7 @@ #include "util/filelike/python.h" #include "util/path.h" #include "util/strings.h" +#include "util/fslike/directory.h" namespace openage::util { @@ -122,11 +123,12 @@ std::ostream &operator<<(std::ostream &stream, const File &file) { } static File get_temp_file() { - std::FILE* tmp_file = std::tmpfile(); - int temp_fd = fileno(tmp_file); - std::string tf_path = "/proc/self/fd/" + std::to_string(temp_fd); + fslike::Directory temp_dir = fslike::Directory::get_temp_directory(); + std::string file_name = std::tmpnam(nullptr); + std::ostringstream dir_path; + temp_dir.repr(dir_path); mode_t mode = 0777; - File file_wrapper = File(tf_path, mode); + File file_wrapper = File(dir_path.str() + file_name, mode); return file_wrapper; } diff --git a/libopenage/util/fslike/directory.h b/libopenage/util/fslike/directory.h index 9fd1e2a020..43c922b8b5 100644 --- a/libopenage/util/fslike/directory.h +++ b/libopenage/util/fslike/directory.h @@ -56,8 +56,9 @@ class Directory : public FSLike { * basically basepath + "/".join(parts) */ std::string resolve(const Path::parts_t &parts) const; + std::tuple do_stat(const Path::parts_t &parts) const; - + std::string basepath; }; } // namespace fslike From a144c8cda72a4e2fa90beb8cde5915e3f13c3cbe Mon Sep 17 00:00:00 2001 From: Tobias Alam Date: Mon, 18 Nov 2024 08:29:11 -0500 Subject: [PATCH 4/4] sanity check fixes --- copying.md | 1 + libopenage/util/file.h | 2 +- libopenage/util/fslike/directory.h | 2 +- libopenage/util/path.cpp | 2 +- libopenage/util/path.h | 2 +- 5 files changed, 5 insertions(+), 4 deletions(-) diff --git a/copying.md b/copying.md index ec1ddd2a46..61153fb9cf 100644 --- a/copying.md +++ b/copying.md @@ -156,6 +156,7 @@ _the openage authors_ are: | Nikhil Ghosh | NikhilGhosh75 | nghosh606 à gmail dawt com | | Edvin Lindholm | EdvinLndh | edvinlndh à gmail dawt com | | Jeremiah Morgan | jere8184 | jeremiahmorgan dawt bham à outlook dawt com | +| Tobias Alam | alamt22 | tobiasal à umich dawt edu | If you're a first-time committer, add yourself to the above list. This is not just for legal reasons, but also to keep an overview of all those nicknames. diff --git a/libopenage/util/file.h b/libopenage/util/file.h index f5339adf67..44d24b027a 100644 --- a/libopenage/util/file.h +++ b/libopenage/util/file.h @@ -99,7 +99,7 @@ class OAAPI File { ssize_t size(); std::vector get_lines(); std::shared_ptr get_fileobj() const; - + static File get_temp_file(); protected: diff --git a/libopenage/util/fslike/directory.h b/libopenage/util/fslike/directory.h index 43c922b8b5..9f72d49766 100644 --- a/libopenage/util/fslike/directory.h +++ b/libopenage/util/fslike/directory.h @@ -47,7 +47,7 @@ class Directory : public FSLike { uint64_t get_filesize(const Path::parts_t &parts) override; std::ostream &repr(std::ostream &) override; - + static Directory get_temp_directory(); protected: diff --git a/libopenage/util/path.cpp b/libopenage/util/path.cpp index ff23253533..e521a4ed9e 100644 --- a/libopenage/util/path.cpp +++ b/libopenage/util/path.cpp @@ -1,4 +1,4 @@ -// Copyright 2015-2023 the openage authors. See copying.md for legal info. +// Copyright 2015-2024 the openage authors. See copying.md for legal info. #include "path.h" diff --git a/libopenage/util/path.h b/libopenage/util/path.h index d16e1734c6..5d4e079812 100644 --- a/libopenage/util/path.h +++ b/libopenage/util/path.h @@ -1,4 +1,4 @@ -// Copyright 2015-2023 the openage authors. See copying.md for legal info. +// Copyright 2015-2024 the openage authors. See copying.md for legal info. #pragma once