Skip to content

Commit

Permalink
[#462] when recv file, if file exist, save to foo (1).ext (was `1_f…
Browse files Browse the repository at this point in the history
…oo.ext`) (#463)

* #462 change the filename dup algorithm

* Update NEWS.md

* fix API

* fix codecov ignore files
  • Loading branch information
lidaobing authored May 3, 2021
1 parent 258b94d commit 2ed1d36
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 23 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* [#439] try to fix compile problem under Hurd.
* [#447] fix bug: use defined icon no longer works.
* [#441] use `GtkHeaderBar` under Linux.
* [#462] when recv file, if file exist, save to `foo (1).ext` (was `1_foo.ext`).
* Translation updated
* Simplified Chinese
* Russian - Thanks to @KovalevArtem
Expand Down
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ coverage:
ignore:
- "^src/iptux-core/Test.*"
- "^src/iptux-core/.*Test.cpp$"
- "^src/iptux-utils/Test.*"
- "^src/iptux-utils/.*Test.cpp$"
- "^src/iptux/Test.*"
- "^src/iptux/.*Test.cpp$"
- "^src/googletest.*"
9 changes: 4 additions & 5 deletions src/iptux-core/internal/AnalogFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,16 @@ int AnalogFS::open(const char* fn, int flags) {
*/
int AnalogFS::open(const char* fn, int flags, mode_t mode) {
char tpath[MAX_PATHLEN];
char* tfn;
int fd;

strcpy(tpath, path);
mergepath(tpath, fn);
if ((flags & O_ACCMODE) == O_WRONLY) {
tfn = assert_filename_inexist(tpath);
if ((fd = ::open(tfn, flags, mode)) == -1) {
pwarning(_("Open() file \"%s\" failed, %s"), tfn, strerror(errno));
auto tfn = assert_filename_inexist(tpath);
if ((fd = ::open(tfn.c_str(), flags, mode)) == -1) {
pwarning(_("Open() file \"%s\" failed, %s"), tfn.c_str(),
strerror(errno));
}
g_free(tfn);
} else {
if ((fd = ::open(tpath, flags, mode)) == -1) {
pwarning(_("Open() file \"%s\" failed, %s"), tpath, strerror(errno));
Expand Down
15 changes: 15 additions & 0 deletions src/iptux-utils/UtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,18 @@ TEST(Utils, utf8MakeValid) {
ASSERT_EQ(utf8MakeValid("\xe4\xe6\x96\x87"), "�文");
ASSERT_EQ(utf8MakeValid("\xe4\xb8\xad\xe6"), "中�");
}

TEST(Utils, dupPath) {
ASSERT_EQ(dupPath("/", 1), "/(1)");
ASSERT_EQ(dupPath("/a.b", 1), "/a (1).b");
ASSERT_EQ(dupPath("/a.b/.", 1), "/a.b/(1)");
ASSERT_EQ(dupPath("/a.b/c.d", 1), "/a.b/c (1).d");
ASSERT_EQ(dupPath("a.b/.", 1), "a.b/(1)");
ASSERT_EQ(dupPath("a.b/c.d", 1), "a.b/c (1).d");
ASSERT_EQ(dupPath("", 1), "(1)");
ASSERT_EQ(dupPath("a", 1), "a (1)");
ASSERT_EQ(dupPath("a.b", 1), "a (1).b");
ASSERT_EQ(dupPath("a.b.c", 1), "a.b (1).c");
ASSERT_EQ(dupPath("a.b", 2), "a (2).b");
ASSERT_EQ(dupPath("a.b", 10), "a (10).b");
}
59 changes: 42 additions & 17 deletions src/iptux-utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,26 +108,51 @@ char* convert_encode(const char* string,
* @param path 文件路径
* @return 新文件路径 *
*/
char* assert_filename_inexist(const char* path) {
const char* ptr;
uint16_t count;

string assert_filename_inexist(const char* path) {
if (access(path, F_OK) != 0)
return g_strdup(path);

ptr = strrchr(path, '/');
ptr = ptr ? ptr + 1 : path;
count = 1;
string res;
while (count) {
res =
stringFormat("%.*s%" PRIu16 "_%s", (int)(ptr - path), path, count, ptr);
if (access(res.c_str(), F_OK) != 0)
break;
count++;
return path;

int idx = 1;
while (true) {
string newPath = dupPath(path, idx);
if (access(newPath.c_str(), F_OK) != 0) {
return newPath;
}
idx++;
}
}

string dupFilename(const string& filename, int idx) {
if (filename == "." || filename == "/") {
return stringFormat("(%d)", idx);
}

return g_strdup(res.c_str());
auto pos = filename.find_last_of('.');
if (pos == string::npos) {
return stringFormat("%s (%d)", filename.c_str(), idx);
}

return stringFormat("%s (%d).%s", filename.substr(0, pos).c_str(), idx,
filename.substr(pos + 1, filename.size()).c_str());
}

string dupPath(const string& path, int idx) {
auto basename_ = g_path_get_basename(path.c_str());
auto dirname_ = g_path_get_dirname(path.c_str());

string basename = basename_;
string dirname = dirname_;
g_free(basename_);
g_free(dirname_);

if (dirname == ".") {
return dupFilename(basename, idx);
}
if (dirname == "/") {
return "/" + dupFilename(basename, idx);
}
return stringFormat("%s/%s", dirname.c_str(),
dupFilename(basename, idx).c_str());
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/iptux-utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ char* iptux_string_validate(const char* s,
char* convert_encode(const char* string,
const char* tocode,
const char* fromcode);
char* assert_filename_inexist(const char* path);
std::string assert_filename_inexist(const char* path);
std::string dupPath(const std::string& fname, int idx);
char* getformattime(gboolean date, const char* format, ...);

gboolean giter_compare_foreach(gunichar src, gunichar dst);
Expand Down

0 comments on commit 2ed1d36

Please sign in to comment.