Skip to content

Commit f6f12c2

Browse files
author
AbstractionFactory
authored
Fixing unnecessarily uploading unchanged files (#280)
* Fixing unnecessarily uploading unchanged files Signed-off-by: AbstractionFactory <[email protected]> * Tests for skipping uploads Signed-off-by: AbstractionFactory <[email protected]> --------- Signed-off-by: AbstractionFactory <[email protected]>
1 parent 964ac49 commit f6f12c2

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

backend/internal/indexstorage/bufferedstorage/buffered.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bufferedstorage
22

33
import (
4+
"bytes"
45
"context"
56
"fmt"
67
"io/fs"
@@ -129,6 +130,17 @@ func (b *buffered) WriteFile(ctx context.Context, filePath indexstorage.Path, co
129130
}
130131

131132
localPath := path.Join(b.localDir, string(filePath))
133+
134+
status := b.index.FileStatus(ctx, filePath)
135+
switch status {
136+
case fileStatusPresent:
137+
localContents, err := os.ReadFile(localPath)
138+
if err == nil && bytes.Equal(localContents, contents) {
139+
// Do not re-upload a file that has not changed.
140+
return nil
141+
}
142+
}
143+
132144
if err := os.MkdirAll(path.Dir(localPath), 0755); err != nil {
133145
return fmt.Errorf("failed to create directory for %s (%w)", localPath, err)
134146
}

backend/internal/indexstorage/bufferedstorage/buffered_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bufferedstorage_test
22

33
import (
4+
"bytes"
45
"context"
56
"os"
67
"path"
@@ -129,6 +130,41 @@ func TestSubdir(t *testing.T) {
129130
assertFileExists(t, ctx, buffer, "test/test.txt")
130131
}
131132

133+
func TestSameContent(t *testing.T) {
134+
const testContent = "Hello world!"
135+
backingDir := t.TempDir()
136+
backingStorage := tofutestutils.Must2(filesystemstorage.New(backingDir))
137+
138+
ctx := tofutestutils.Context(t)
139+
140+
tofutestutils.Must(backingStorage.WriteFile(ctx, "test.txt", []byte(testContent)))
141+
142+
localDir := t.TempDir()
143+
buffer := tofutestutils.Must2(bufferedstorage.New(logger.NewTestLogger(t), localDir, backingStorage, 25))
144+
if _, err := os.Stat(path.Join(localDir, "test.txt")); err == nil {
145+
t.Fatalf("Test file was present before fetched.")
146+
}
147+
readData, err := buffer.ReadFile(ctx, "test.txt")
148+
if err != nil {
149+
t.Fatalf("Could not read test file: %v", err)
150+
}
151+
if !bytes.Equal(readData, []byte(testContent)) {
152+
t.Fatalf("The test file has incorrect contents: %s", readData)
153+
}
154+
155+
if _, err = os.Stat(path.Join(localDir, "test.txt")); err != nil {
156+
t.Fatalf("Test file was not present before after fetching: %v", err)
157+
}
158+
tofutestutils.Must(buffer.WriteFile(ctx, "test.txt", []byte(testContent)))
159+
if buffer.UncommittedFiles() != 0 {
160+
t.Fatalf("Uncommitted files despite same content.")
161+
}
162+
tofutestutils.Must(buffer.WriteFile(ctx, "test.txt", []byte(testContent+"!")))
163+
if buffer.UncommittedFiles() != 1 {
164+
t.Fatalf("No or multiple uncommitted files despite different content!")
165+
}
166+
}
167+
132168
func assertFileDoesNotExist(t *testing.T, ctx context.Context, storage indexstorage.API, file indexstorage.Path) {
133169
t.Helper()
134170
_, err := storage.ReadFile(ctx, file)

0 commit comments

Comments
 (0)