From b3f95749993b5c4018f98c6dcd34772aae346f38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=AA=20Xu=C3=A2n=20Tu=E1=BA=A5n=20Anh?= Date: Mon, 24 Jun 2024 14:37:41 +0700 Subject: [PATCH 1/2] Replace the type of filename of DownloadObjectArgs and UploadObjectArgs from std::string to std::filesystem::path --- include/miniocpp/args.h | 5 +++-- src/args.cc | 11 +++++------ src/client.cc | 9 +++++---- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/include/miniocpp/args.h b/include/miniocpp/args.h index d1c4d12..3d1ec2b 100644 --- a/include/miniocpp/args.h +++ b/include/miniocpp/args.h @@ -23,6 +23,7 @@ #include #include #include +#include #include "error.h" #include "http.h" @@ -192,7 +193,7 @@ using StatObjectArgs = ObjectConditionalReadArgs; using RemoveObjectArgs = ObjectVersionArgs; struct DownloadObjectArgs : public ObjectReadArgs { - std::string filename; + std::filesystem::path filename; bool overwrite; http::ProgressFunction progressfunc = nullptr; void* progress_userdata = nullptr; @@ -359,7 +360,7 @@ struct ComposeObjectArgs : public ObjectWriteArgs { }; // struct ComposeObjectArgs struct UploadObjectArgs : public PutObjectBaseArgs { - std::string filename; + std::filesystem::path filename; http::ProgressFunction progressfunc = nullptr; void* progress_userdata = nullptr; diff --git a/src/args.cc b/src/args.cc index e361d77..3e1363c 100644 --- a/src/args.cc +++ b/src/args.cc @@ -213,12 +213,12 @@ error::Error DownloadObjectArgs::Validate() const { if (error::Error err = ObjectReadArgs::Validate()) { return err; } - if (!utils::CheckNonEmptyString(filename)) { + if (!utils::CheckNonEmptyString(filename.u8string())) { return error::Error("filename cannot be empty"); } if (!overwrite && std::filesystem::exists(filename)) { - return error::Error("file " + filename + " already exists"); + return error::Error("file " + filename.u8string() + " already exists"); } return error::SUCCESS; @@ -415,16 +415,15 @@ error::Error UploadObjectArgs::Validate() { if (error::Error err = ObjectArgs::Validate()) { return err; } - if (!utils::CheckNonEmptyString(filename)) { + if (!utils::CheckNonEmptyString(filename.u8string())) { return error::Error("filename cannot be empty"); } if (!std::filesystem::exists(filename)) { - return error::Error("file " + filename + " does not exist"); + return error::Error("file " + filename.u8string() + " does not exist"); } - std::filesystem::path file_path = filename; - size_t obj_size = std::filesystem::file_size(file_path); + size_t obj_size = std::filesystem::file_size(filename); object_size = static_cast(obj_size); return utils::CalcPartInfo(object_size, part_size, part_count); } diff --git a/src/client.cc b/src/client.cc index 94ffd56..c573202 100644 --- a/src/client.cc +++ b/src/client.cc @@ -697,12 +697,13 @@ DownloadObjectResponse Client::DownloadObject(DownloadObjectArgs args) { etag = resp.etag; } - std::string temp_filename = - args.filename + "." + curlpp::escape(etag) + ".part.minio"; + std::filesystem::path temp_filename = args.filename; + temp_filename.concat("." + curlpp::escape(etag) + ".part.minio"); + std::ofstream fout(temp_filename, std::ios::trunc | std::ios::out); if (!fout.is_open()) { return error::make("unable to open file " + - temp_filename); + temp_filename.u8string()); } std::string region; @@ -780,7 +781,7 @@ UploadObjectResponse Client::UploadObject(UploadObjectArgs args) { file.open(args.filename); } catch (std::system_error& err) { return error::make( - "unable to open file " + args.filename + "; " + err.code().message()); + "unable to open file " + args.filename.u8string() + "; " + err.code().message()); } PutObjectArgs po_args(file, args.object_size, 0); From fc54222906528200a196dbe7f9a8626d34838df8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=AA=20Xu=C3=A2n=20Tu=E1=BA=A5n=20Anh?= Date: Mon, 24 Jun 2024 15:05:30 +0700 Subject: [PATCH 2/2] apply coding style --- include/miniocpp/args.h | 2 +- src/client.cc | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/include/miniocpp/args.h b/include/miniocpp/args.h index 3d1ec2b..46db3de 100644 --- a/include/miniocpp/args.h +++ b/include/miniocpp/args.h @@ -18,12 +18,12 @@ #ifndef MINIO_CPP_ARGS_H_INCLUDED #define MINIO_CPP_ARGS_H_INCLUDED +#include #include #include #include #include #include -#include #include "error.h" #include "http.h" diff --git a/src/client.cc b/src/client.cc index c573202..1b48e0d 100644 --- a/src/client.cc +++ b/src/client.cc @@ -780,8 +780,9 @@ UploadObjectResponse Client::UploadObject(UploadObjectArgs args) { try { file.open(args.filename); } catch (std::system_error& err) { - return error::make( - "unable to open file " + args.filename.u8string() + "; " + err.code().message()); + return error::make("unable to open file " + + args.filename.u8string() + "; " + + err.code().message()); } PutObjectArgs po_args(file, args.object_size, 0);