Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace the filename type of DownloadObjectArgs and UploadObjectArgs #149

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Replace the type of filename of DownloadObjectArgs and UploadObjectAr…
…gs from std::string to std::filesystem::path
  • Loading branch information
Lê Xuân Tuấn Anh committed Jun 24, 2024
commit b3f95749993b5c4018f98c6dcd34772aae346f38
5 changes: 3 additions & 2 deletions include/miniocpp/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 23,7 @@
#include <map>
#include <string>
#include <type_traits>
#include <filesystem>

#include "error.h"
#include "http.h"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
11 changes: 5 additions & 6 deletions src/args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't filename.string() work?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filename.string() may make errors with the pure utf-8 encoded path. Let's consider following:

#include <iostream>
#include <string>
#include <filesystem>

int main(int argc, char* argv[])
{
	try {
		std::filesystem::path fileStr = L"G:\\Tuấn Anh\\tết.txt";
		std::string test1 = fileStr.u8string();
		std::string test2 = fileStr.string(); // >>>>>>>>>>> throw exception
	}
	catch (const std::exception& ex) {
		std::cerr << ex.what() << std::endl;
		return 1;
	}

	return 0;
}

I tried again with filename.string(), the exception was thrown.
With minio::utils::Trim(...), I don't think there is a different in result with filename.string() or 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;
Expand Down Expand Up @@ -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<long>(obj_size);
return utils::CalcPartInfo(object_size, part_size, part_count);
}
Expand Down
9 changes: 5 additions & 4 deletions src/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<DownloadObjectResponse>("unable to open file "
temp_filename);
temp_filename.u8string());
}

std::string region;
Expand Down Expand Up @@ -780,7 781,7 @@ UploadObjectResponse Client::UploadObject(UploadObjectArgs args) {
file.open(args.filename);
} catch (std::system_error& err) {
return error::make<UploadObjectResponse>(
"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);
Expand Down
Loading