Skip to content

Commit 0fded6c

Browse files
authored
Fixed the problem of pathjoin function splicing paths when basepath is the root directory. (#2118)
* fix path join
1 parent 7f8e5c1 commit 0fded6c

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

Diff for: core/common/FileSystemUtil.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,10 @@ extern const std::string PATH_SEPARATOR;
4141
// PathJoin concats base and sub (by adding necessary path separator).
4242
// NOTE: the implementation is not elegant for better performance (backward).
4343
inline std::string PathJoin(const std::string& base, const std::string& sub) {
44-
// Only Windows can collect root path, so linux do as old way.
45-
#if defined(__linux__)
46-
return base + PATH_SEPARATOR + sub;
47-
#elif defined(_MSC_VER)
48-
if (!BOOL_FLAG(enable_root_path_collection) || base.back() != PATH_SEPARATOR[0]) {
44+
if (!base.empty() && base.back() != PATH_SEPARATOR[0]) {
4945
return base + PATH_SEPARATOR + sub;
5046
}
5147
return base + sub;
52-
#endif
5348
}
5449

5550
std::string ParentPath(const std::string& path);

Diff for: core/unittest/common/FileSystemUtilUnittest.h

+23
Original file line numberDiff line numberDiff line change
@@ -453,4 +453,27 @@ TEST_F(FileSystemUtilUnittest, TestGetFdPath) {
453453
fclose(file);
454454
}
455455

456+
TEST_F(FileSystemUtilUnittest, TestPathJoin) {
457+
std::string filePath;
458+
#if defined(_MSC_VER)
459+
filePath = PathJoin("D:\\", "dataA");
460+
EXPECT_EQ(filePath, "D:\\dataA");
461+
462+
filePath = PathJoin("D:\\xx", "dataA");
463+
EXPECT_EQ(filePath, "D:\\xx\\dataA");
464+
465+
filePath = PathJoin("D:\\xx\\", "dataA");
466+
EXPECT_EQ(filePath, "D:\\xx\\dataA");
467+
#else
468+
filePath = PathJoin("/", "dataA");
469+
EXPECT_EQ(filePath, "/dataA");
470+
471+
filePath = PathJoin("/xx", "dataA");
472+
EXPECT_EQ(filePath, "/xx/dataA");
473+
474+
filePath = PathJoin("/xx/", "dataA");
475+
EXPECT_EQ(filePath, "/xx/dataA");
476+
#endif
477+
}
478+
456479
} // namespace logtail

0 commit comments

Comments
 (0)