From 06f27384dd74fe8705933108274f6a6dfa8732cd Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Mon, 19 Apr 2021 16:48:51 +0100 Subject: [PATCH] b2: fix versions and .files with no extension - fixes #5244 --- lib/version/version.go | 18 ++++++++++++++---- lib/version/version_test.go | 4 ++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/version/version.go b/lib/version/version.go index 2b13f6b44..6166e2262 100644 --- a/lib/version/version.go +++ b/lib/version/version.go @@ -13,10 +13,21 @@ const versionFormat = "-v2006-01-02-150405.000" var versionRegexp = regexp.MustCompile("-v\\d{4}-\\d{2}-\\d{2}-\\d{6}-\\d{3}") +// Split fileName into base and extension so that base + ext == fileName +func splitExt(fileName string) (base, ext string) { + ext = path.Ext(fileName) + base = fileName[:len(fileName)-len(ext)] + // .file splits to base == "", ext == ".file" + // so swap ext and base in this case + if ext != "" && base == "" { + base, ext = ext, base + } + return base, ext +} + // Add returns fileName modified to include t as the version func Add(fileName string, t time.Time) string { - ext := path.Ext(fileName) - base := fileName[:len(fileName)-len(ext)] + base, ext := splitExt(fileName) s := t.Format(versionFormat) // Replace the '.' with a '-' s = strings.Replace(s, ".", "-", -1) @@ -27,8 +38,7 @@ func Add(fileName string, t time.Time) string { // If the fileName did not have a version then time.Time{} is returned along with an unmodified fileName func Remove(fileName string) (t time.Time, fileNameWithoutVersion string) { fileNameWithoutVersion = fileName - ext := path.Ext(fileName) - base := fileName[:len(fileName)-len(ext)] + base, ext := splitExt(fileName) if len(base) < len(versionFormat) { return } diff --git a/lib/version/version_test.go b/lib/version/version_test.go index 7517fb329..d514b1918 100644 --- a/lib/version/version_test.go +++ b/lib/version/version_test.go @@ -26,6 +26,8 @@ func TestVersionAdd(t *testing.T) { {t0, "potato-v2001-02-03-040506-123.txt", "potato-v2001-02-03-040506-123-v1970-01-01-010101-123.txt"}, {t0, "123.!!lipps", "123-v1970-01-01-010101-123.!!lipps"}, {t1, "potato", "potato-v2001-02-03-040506-123"}, + {t1, ".potato", ".potato-v2001-02-03-040506-123"}, + {t1, ".potato.conf", ".potato-v2001-02-03-040506-123.conf"}, {t1, "", "-v2001-02-03-040506-123"}, } { actual := version.Add(test.in, test.t) @@ -43,6 +45,8 @@ func TestVersionRemove(t *testing.T) { {"potato-v1970-01-01-010101-123.txt", t0r, "potato.txt"}, {"potato-v2001-02-03-040506-123-v1970-01-01-010101-123.txt", t0r, "potato-v2001-02-03-040506-123.txt"}, {"potato-v2001-02-03-040506-123", t1, "potato"}, + {".potato-v2001-02-03-040506-123", t1, ".potato"}, + {".potato-v2001-02-03-040506-123.conf", t1, ".potato.conf"}, {"-v2001-02-03-040506-123", t1, ""}, {"potato-v2A01-02-03-040506-123", emptyT, "potato-v2A01-02-03-040506-123"}, {"potato-v2001-02-03-040506=123", emptyT, "potato-v2001-02-03-040506=123"},