diff --git a/updater/filename.go b/updater/filename.go index 8767b4d..dfe8f1f 100644 --- a/updater/filename.go +++ b/updater/filename.go @@ -1,7 +1,7 @@ package updater import ( - "fmt" + "path" "regexp" "strings" ) @@ -13,34 +13,45 @@ var ( // GetIdentifierAndVersion splits the given file path into its identifier and version. func GetIdentifierAndVersion(versionedPath string) (identifier, version string, ok bool) { - // extract version - rawVersion := fileVersionRegex.FindString(versionedPath) + dirPath, filename := path.Split(versionedPath) + + // Extract version from filename. + rawVersion := fileVersionRegex.FindString(filename) if rawVersion == "" { + // No version present in file, making it invalid. return "", "", false } - // replace - with . and trim _ + // Trim the `_v` that gets caught by the regex and + // replace `-` with `.` to get the version string. version = strings.Replace(strings.TrimLeft(rawVersion, "_v"), "-", ".", -1) - // put together without version - i := strings.Index(versionedPath, rawVersion) + // Put the filename back together without version. + i := strings.Index(filename, rawVersion) if i < 0 { // extracted version not in string (impossible) return "", "", false } - return versionedPath[:i] + versionedPath[i+len(rawVersion):], version, true + filename = filename[:i] + filename[i+len(rawVersion):] + + // Put the full path back together and return it. + // `dirPath + filename` is guaranteed by path.Split() + return dirPath + filename, version, true } // GetVersionedPath combines the identifier and version and returns it as a file path. func GetVersionedPath(identifier, version string) (versionedPath string) { - // split in half - splittedFilePath := strings.SplitN(identifier, ".", 2) - // replace . with - + identifierPath, filename := path.Split(identifier) + + // Split the filename where the version should go. + splittedFilename := strings.SplitN(filename, ".", 2) + // Replace `.` with `-` for the filename format. transformedVersion := strings.Replace(version, ".", "-", -1) - // put together - if len(splittedFilePath) == 1 { - return fmt.Sprintf("%s_v%s", splittedFilePath[0], transformedVersion) + // Put everything back together and return it. + versionedPath = identifierPath + splittedFilename[0] + "_v" + transformedVersion + if len(splittedFilename) > 1 { + versionedPath += "." + splittedFilename[1] } - return fmt.Sprintf("%s_v%s.%s", splittedFilePath[0], transformedVersion, splittedFilePath[1]) + return versionedPath } diff --git a/updater/storage.go b/updater/storage.go index 4916501..55f78fe 100644 --- a/updater/storage.go +++ b/updater/storage.go @@ -39,12 +39,23 @@ func (reg *ResourceRegistry) ScanStorage(root string) error { // walk fs _ = filepath.Walk(root, func(path string, info os.FileInfo, err error) error { + // skip tmp dir (including errors trying to read it) + if strings.HasPrefix(path, reg.tmpDir.Path) { + return filepath.SkipDir + } + + // handle walker error if err != nil { lastError = fmt.Errorf("%s: could not read %s: %w", reg.Name, path, err) log.Warning(lastError.Error()) return nil } + // ignore directories + if info.IsDir() { + return nil + } + // get relative path to storage relativePath, err := filepath.Rel(reg.storageDir.Path, path) if err != nil { @@ -52,10 +63,6 @@ func (reg *ResourceRegistry) ScanStorage(root string) error { log.Warning(lastError.Error()) return nil } - // ignore files in tmp dir - if strings.HasPrefix(relativePath, reg.tmpDir.Path) { - return nil - } // convert to identifier and version relativePath = filepath.ToSlash(relativePath)