Skip to content

Commit af94ad7

Browse files
committed
Added allow_dir_exist to MakeDirectory + clarified comments
1 parent 8511943 commit af94ad7

File tree

7 files changed

+38
-17
lines changed

7 files changed

+38
-17
lines changed

src/filesystem/api.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,11 +619,12 @@ ReadBinaryProto(const std::string& path, google::protobuf::MessageLite* msg)
619619
}
620620

621621
Status
622-
MakeDirectory(const std::string& dir, const bool recursive)
622+
MakeDirectory(
623+
const std::string& dir, const bool recursive, const bool allow_dir_exist)
623624
{
624625
std::shared_ptr<FileSystem> fs;
625626
RETURN_IF_ERROR(fsm_.GetFileSystem(dir, fs));
626-
return fs->MakeDirectory(dir, recursive);
627+
return fs->MakeDirectory(dir, recursive, allow_dir_exist);
627628
}
628629

629630
Status

src/filesystem/api.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Status LocalizePath(
158158
/// \param path The path of the directory or file.
159159
/// \param recursive If true, will fetch all sub-directories in
160160
/// the provided path.
161-
/// \param mount_dir If specified, will use provided local directory
161+
/// \param mount_dir If not empty, will use provided local directory
162162
/// for localization.
163163
/// \param localized Returns the LocalizedPath object
164164
/// representing the local copy of the path.
@@ -205,8 +205,14 @@ Status ReadBinaryProto(
205205
/// \param dir The path to the directory.
206206
/// \param recursive Whether the parent directories will be created
207207
/// if not exist.
208+
/// \param allow_dir_exist Controls the behavior on condition,
209+
/// when `dir` already exists. If true and `dir` exists, returns success.
210+
/// If false and `dir` exists, fails with `Status::Code::INTERNAL`
211+
/// and reports errno EEXIST. Default value: false.
208212
/// \return Error status if the directory can't be created
209-
Status MakeDirectory(const std::string& dir, const bool recursive);
213+
Status MakeDirectory(
214+
const std::string& dir, const bool recursive,
215+
const bool allow_dir_exist = false);
210216

211217
/// Create a temporary directory of the specified filesystem type.
212218
/// \param type The type of the filesystem.

src/filesystem/implementations/as.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ class ASFileSystem : public FileSystem {
9494
Status WriteBinaryFile(
9595
const std::string& path, const char* contents,
9696
const size_t content_len) override;
97-
Status MakeDirectory(const std::string& dir, const bool recursive) override;
97+
Status MakeDirectory(
98+
const std::string& dir, const bool recursive,
99+
const bool allow_dir_exist) override;
98100
Status MakeTemporaryDirectory(std::string* temp_dir) override;
99101
Status DeletePath(const std::string& path) override;
100102

@@ -489,7 +491,8 @@ ASFileSystem::WriteBinaryFile(
489491
}
490492

491493
Status
492-
ASFileSystem::MakeDirectory(const std::string& dir, const bool recursive)
494+
ASFileSystem::MakeDirectory(
495+
const std::string& dir, const bool recursive, const bool allow_dir_exist)
493496
{
494497
return Status(
495498
Status::Code::UNSUPPORTED,

src/filesystem/implementations/common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ class FileSystem {
9292
const std::string& path, const char* contents,
9393
const size_t content_len) = 0;
9494
virtual Status MakeDirectory(
95-
const std::string& dir, const bool recursive) = 0;
95+
const std::string& dir, const bool recursive,
96+
const bool allow_dir_exist) = 0;
9697
virtual Status MakeTemporaryDirectory(std::string* temp_dir) = 0;
9798
virtual Status DeletePath(const std::string& path) = 0;
9899
};

src/filesystem/implementations/gcs.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ class GCSFileSystem : public FileSystem {
8383
Status WriteBinaryFile(
8484
const std::string& path, const char* contents,
8585
const size_t content_len) override;
86-
Status MakeDirectory(const std::string& dir, const bool recursive) override;
86+
Status MakeDirectory(
87+
const std::string& dir, const bool recursive,
88+
const bool allow_dir_exist) override;
8789
Status MakeTemporaryDirectory(std::string* temp_dir) override;
8890
Status DeletePath(const std::string& path) override;
8991

@@ -474,7 +476,8 @@ GCSFileSystem::WriteBinaryFile(
474476
}
475477

476478
Status
477-
GCSFileSystem::MakeDirectory(const std::string& dir, const bool recursive)
479+
GCSFileSystem::MakeDirectory(
480+
const std::string& dir, const bool recursive, const bool allow_dir_exist)
478481
{
479482
return Status(
480483
Status::Code::UNSUPPORTED,

src/filesystem/implementations/local.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ class LocalFileSystem : public FileSystem {
5757
Status WriteBinaryFile(
5858
const std::string& path, const char* contents,
5959
const size_t content_len) override;
60-
Status MakeDirectory(const std::string& dir, const bool recursive) override;
60+
Status MakeDirectory(
61+
const std::string& dir, const bool recursive,
62+
const bool allow_dir_exist) override;
6163
Status MakeTemporaryDirectory(std::string* temp_dir) override;
6264
Status DeletePath(const std::string& path) override;
6365
};
@@ -248,22 +250,23 @@ LocalFileSystem::WriteBinaryFile(
248250
}
249251

250252
Status
251-
LocalFileSystem::MakeDirectory(const std::string& dir, const bool recursive)
253+
LocalFileSystem::MakeDirectory(
254+
const std::string& dir, const bool recursive, const bool allow_dir_exist)
252255
{
253256
#ifdef _WIN32
254257
if (mkdir(dir.c_str()) == -1)
255258
#else
256259
if (mkdir(dir.c_str(), S_IRWXU) == -1)
257260
#endif
258261
{
259-
// Return success if directory already exists
260-
if (errno == EEXIST) {
262+
// Return success if directory already exists and it is permitted
263+
if (allow_dir_exist && errno == EEXIST) {
261264
return Status::Success;
262265
}
263266
// In all other cases only allow the error due to parent directory
264267
// does not exist, if 'recursive' is requested
265268
if ((errno == ENOENT) && (!dir.empty()) && recursive) {
266-
RETURN_IF_ERROR(MakeDirectory(DirName(dir), recursive));
269+
RETURN_IF_ERROR(MakeDirectory(DirName(dir), recursive, allow_dir_exist));
267270
// Retry the creation
268271
#ifdef _WIN32
269272
if (mkdir(dir.c_str()) == -1)

src/filesystem/implementations/s3.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ class S3FileSystem : public FileSystem {
159159
Status WriteBinaryFile(
160160
const std::string& path, const char* contents,
161161
const size_t content_len) override;
162-
Status MakeDirectory(const std::string& dir, const bool recursive) override;
162+
Status MakeDirectory(
163+
const std::string& dir, const bool recursive,
164+
const bool allow_dir_exist) override;
163165
Status MakeTemporaryDirectory(std::string* temp_dir) override;
164166
Status DeletePath(const std::string& path) override;
165167

@@ -668,7 +670,8 @@ S3FileSystem::LocalizePath(
668670
tmp_folder = mount_dir.empty() ? std::string(env_mount_dir) : mount_dir;
669671
tmp_folder =
670672
JoinPath({tmp_folder, path.substr(path.find_last_of('/') + 1)});
671-
RETURN_IF_ERROR(triton::core::MakeDirectory(tmp_folder, true));
673+
RETURN_IF_ERROR(triton::core::MakeDirectory(
674+
tmp_folder, true /*recursive*/, true /*allow_dir_exist*/));
672675
}
673676

674677
// Specify contents to be downloaded
@@ -780,7 +783,8 @@ S3FileSystem::WriteBinaryFile(
780783
}
781784

782785
Status
783-
S3FileSystem::MakeDirectory(const std::string& dir, const bool recursive)
786+
S3FileSystem::MakeDirectory(
787+
const std::string& dir, const bool recursive, const bool allow_dir_exist)
784788
{
785789
return Status(
786790
Status::Code::UNSUPPORTED,

0 commit comments

Comments
 (0)