Repository: buchgr/bazel-remote
Pull request #641 (2023-02-16 17:54:59)

Delete .DS_Store files when migrating cache dir, ignore them when loading a current cache dir


Patch:
diff --git a/cache/disk/load.go b/cache/disk/load.go
index 0ffb465..68a39aa 100644
--- a/cache/disk/load.go
+++ b/cache/disk/load.go
@@ -28,6 +28,8 @@ import (
 	"golang.org/x/sync/semaphore"
 )
 
+const lowercaseDSStoreFile = ".ds_store"
+
 // New returns a new instance of a filesystem-based cache rooted at `dir`,
 // with a maximum size of `maxSizeBytes` bytes and `opts` Options set.
 func New(dir string, maxSizeBytes int64, opts ...Option) (Cache, error) {
@@ -259,13 +261,20 @@ func migrateV1Subdir(oldDir string, destDir string, kind cache.EntryKind) error
 	if kind == cache.CAS {
 		for _, item := range listing {
 
-			oldPath := path.Join(oldDir, item.Name())
+			name := item.Name()
+
+			oldPath := path.Join(oldDir, name)
+
+			if !validate.HashKeyRegex.MatchString(name) {
+				if strings.ToLower(name) == lowercaseDSStoreFile {
+					os.Remove(oldPath)
+					continue
+				}
 
-			if !validate.HashKeyRegex.MatchString(item.Name()) {
 				return fmt.Errorf("Unexpected file: %s", oldPath)
 			}
 
-			destPath := path.Join(destDir, item.Name()) + "-556677.v1"
+			destPath := path.Join(destDir, name) + "-556677.v1"
 			err = os.Rename(oldPath, destPath)
 			if err != nil {
 				return fmt.Errorf("Failed to migrate CAS blob %s: %w",
@@ -277,13 +286,20 @@ func migrateV1Subdir(oldDir string, destDir string, kind cache.EntryKind) error
 	}
 
 	for _, item := range listing {
-		oldPath := path.Join(oldDir, item.Name())
+		name := item.Name()
+
+		oldPath := path.Join(oldDir, name)
 
-		if !validate.HashKeyRegex.MatchString(item.Name()) {
-			return fmt.Errorf("Unexpected file: %s %s", oldPath, item.Name())
+		if !validate.HashKeyRegex.MatchString(name) {
+			if strings.ToLower(name) == lowercaseDSStoreFile {
+				os.Remove(oldPath)
+				continue
+			}
+
+			return fmt.Errorf("Unexpected file: %s %s", oldPath, name)
 		}
 
-		destPath := path.Join(destDir, item.Name()) + "-112233"
+		destPath := path.Join(destDir, name) + "-112233"
 
 		// TODO: support cross-filesystem migration.
 		err = os.Rename(oldPath, destPath)
@@ -481,6 +497,10 @@ func (c *diskCache) scanDir() (scanResult, error) {
 		name := de.Name()
 
 		if !de.IsDir() {
+			if strings.ToLower(name) == lowercaseDSStoreFile {
+				continue
+			}
+
 			return scanResult{}, fmt.Errorf("Unexpected file: %s", name)
 		}
 
@@ -504,6 +524,10 @@ func (c *diskCache) scanDir() (scanResult, error) {
 			dirPath := path.Join(name, name2)
 
 			if !de2.IsDir() {
+				if strings.ToLower(name) == lowercaseDSStoreFile {
+					continue
+				}
+
 				return scanResult{}, fmt.Errorf("Unexpected file: %s", dirPath)
 			}
 


Test patch:
diff --git a/cache/disk/disk_test.go b/cache/disk/disk_test.go
index f2f573d..e5d413d 100644
--- a/cache/disk/disk_test.go
+++ b/cache/disk/disk_test.go
@@ -706,6 +706,20 @@ func TestLoadExistingEntries(t *testing.T) {
 		t.Fatal(err)
 	}
 
+	// Create some .DS_Store files which should be ignored or deleted.
+	err = os.WriteFile(path.Join(cacheDir, "raw", rawHash[:2], ".DS_Store"), []byte{1, 2, 3}, 0644)
+	if err != nil {
+		t.Fatal(err)
+	}
+	err = os.WriteFile(path.Join(cacheDir, "raw", ".DS_Store"), []byte{}, 0644)
+	if err != nil {
+		t.Fatal(err)
+	}
+	err = os.WriteFile(path.Join(cacheDir, ".DS_Store"), []byte{4, 5, 6}, 0644)
+	if err != nil {
+		t.Fatal(err)
+	}
+
 	// Add some overhead for likely CAS blob storage expansion.
 	cacheSize := int64((blobSize + BlockSize) * numBlobs * 2)