Skip to content

Commit 521ff28

Browse files
authored
Ignore hidden files in batch (#617)
* Ignore hidden files in batch * Allow for hidden directories * Clarify comment. * Remove hidden files when batch is in transferDir * Add unit tests * Improve uni test
1 parent ec4b258 commit 521ff28

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

internal/batch/workflow.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ func (a *BatchActivity) Execute(ctx context.Context, params BatchWorkflowInput)
8989
return nil // Keep walking.
9090
}
9191

92+
if strings.HasPrefix(filepath.Base(path), ".") && !entry.IsDir() {
93+
return nil // Don't process hidden files as SIPs
94+
}
95+
9296
req := collection.ProcessingWorkflowRequest{
9397
BatchDir: filepath.Dir(path),
9498
Key: entry.Name(),

internal/workflow/activities/bundle.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"errors"
77
"fmt"
8+
"io/fs"
89
"os"
910
"path/filepath"
1011
"strings"
@@ -61,6 +62,11 @@ func (a *BundleActivity) Execute(ctx context.Context, params *BundleActivityPara
6162
res.FullPath = filepath.Join(params.BatchDir, params.Key)
6263
// This makes the workflow not to delete the original content in the transfer directory
6364
res.FullPathBeforeStrip = ""
65+
if params.ExcludeHiddenFiles {
66+
if err := removeHiddenFiles(res.FullPath); err != nil {
67+
return nil, temporal.NewNonRetryableError(fmt.Errorf("failed to remove hidden files: %w", err))
68+
}
69+
}
6470
} else {
6571
src := filepath.Join(params.BatchDir, params.Key)
6672
dst := params.TransferDir
@@ -312,3 +318,19 @@ func unbag(path string) error {
312318

313319
return nil
314320
}
321+
322+
func removeHiddenFiles(path string) error {
323+
return filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
324+
if err != nil {
325+
return err
326+
}
327+
info, err := d.Info()
328+
if err != nil {
329+
return err
330+
}
331+
if strings.HasPrefix(info.Name(), ".") {
332+
return os.Remove(path)
333+
}
334+
return nil
335+
})
336+
}

internal/workflow/activities/bundle_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,51 @@ func TestBundleActivity(t *testing.T) {
6161
),
6262
)
6363
})
64+
65+
t.Run("Remove hidden files when BatchDir is a subfolder of the TransferDir", func(t *testing.T) {
66+
activity := NewBundleActivity()
67+
ts := &temporalsdk_testsuite.WorkflowTestSuite{}
68+
env := ts.NewTestActivityEnvironment()
69+
env.RegisterActivity(activity.Execute)
70+
71+
transferDir := fs.NewDir(t, "enduro",
72+
fs.WithDir("batch-folder",
73+
fs.WithDir(
74+
"sip",
75+
fs.WithFile("foobar.txt", "Hello world!\n"),
76+
fs.WithFile(".hidden", ""),
77+
),
78+
),
79+
)
80+
batchDir := transferDir.Join("batch-folder")
81+
sipSourceDir := transferDir.Join("batch-folder", "sip")
82+
83+
fut, err := env.ExecuteActivity(activity.Execute, &BundleActivityParams{
84+
ExcludeHiddenFiles: true,
85+
IsDir: true,
86+
TransferDir: transferDir.Path(),
87+
BatchDir: batchDir,
88+
Key: "sip",
89+
})
90+
assert.NilError(t, err)
91+
92+
res := BundleActivityResult{}
93+
assert.NilError(t, fut.Get(&res))
94+
assert.Assert(t,
95+
fs.Equal(
96+
sipSourceDir,
97+
fs.Expected(t,
98+
// .hidden is not expected because ExcludeHiddenFiles is enabled.
99+
fs.WithFile("foobar.txt", "Hello world!\n"),
100+
fs.MatchAnyFileMode,
101+
),
102+
),
103+
)
104+
assert.DeepEqual(t, res.FullPath, sipSourceDir)
105+
rePath, err := filepath.Rel(transferDir.Path(), sipSourceDir)
106+
assert.NilError(t, err)
107+
assert.DeepEqual(t, res.RelPath, rePath)
108+
})
64109
}
65110

66111
func TestUnbag(t *testing.T) {

0 commit comments

Comments
 (0)