Skip to content

Conversation

@julio-lopez
Copy link
Collaborator

Use os.CreateTemp when creating temporary files in tempfile.createUnixFallback function

@codecov
Copy link

codecov bot commented Apr 18, 2025

Codecov Report

Attention: Patch coverage is 71.42857% with 2 lines in your changes missing coverage. Please review.

Project coverage is 76.37%. Comparing base (cb455c6) to head (2965341).
Report is 476 commits behind head on master.

Files with missing lines Patch % Lines
internal/tempfile/tempfile_linux.go 66.66% 1 Missing ⚠️
internal/tempfile/tempfile_unix_fallback.go 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4513      +/-   ##
==========================================
+ Coverage   75.86%   76.37%   +0.50%     
==========================================
  Files         470      527      +57     
  Lines       37301    40044    +2743     
==========================================
+ Hits        28299    30584    +2285     
- Misses       7071     7444     +373     
- Partials     1931     2016      +85     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

fullPath := filepath.Join(tempDirOr(dir), uuid.NewString())

f, err := os.OpenFile(fullPath, os.O_CREATE|os.O_EXCL|os.O_RDWR, permissions) //nolint:gosec
f, err := os.CreateTemp(dir, "kt-")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason for not using os.CreateTemp here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call to tempDirOr(dir) is redundant, since os.CreateTemp will resolve the actual directory in the same way as tempDirOr was doing it, so the call is no longer necessary.

@julio-lopez julio-lopez force-pushed the cleanup/createmp branch 2 times, most recently from bc7bb72 to 8604697 Compare April 18, 2025 17:48
@julio-lopez julio-lopez requested a review from jkowalski April 18, 2025 18:23
@julio-lopez julio-lopez marked this pull request as ready for review April 18, 2025 18:23
@julio-lopez julio-lopez enabled auto-merge (squash) April 18, 2025 18:24
@julio-lopez
Copy link
Collaborator Author

@Rohit-BM18 FYI

// Create creates a temporary file that will be automatically deleted on close.
func Create(dir string) (*os.File, error) {
dir = tempDirOr(dir)
func CreateAutoDelete() (*os.File, error) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the dir parameter based on @jkowalski suggestion

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And renamed the function to CreateAutoDelete to make it more explicit.
@jkowalski @Rohit-BM18 @redgoat650 if you have a suggestion for a better name, we can change it in a separate PR.

require.NoError(t, err)
require.NoError(t, f.Sync())

defer f.Close()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f is automatically closed and removed on test cleanup

e := testenv.NewCLITest(t, testenv.RepoFormatNotImportant, runner)

e.RunAndExpectSuccess(t, "repo", "create", "filesystem", "--path", e.RepoDir)
e.RunAndExpectSuccess(t, "policy", "set", "--global", "--enable-volume-shadow-copy=when-available")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

relocated here from below, to setup the "repo" first, no functional change to the test.


oid := sources[0].Snapshots[0].ObjectID
entries := clitestutil.ListDirectory(t, e, oid)
t.Log("sources[0].Snapshots[0].ObjectID entries:", entries)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is spurious and can be removed.

t.Log("sources[0].Snapshots[0].ObjectID entries:", entries)

if isAdmin {
require.NotEmpty(t, entries)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before, there was a panic in cases where entries was empty. This explicitly checks the condition and stops the test if the condition is not met making it easier to diagnose the problem.


if errors.Is(err, syscall.EISDIR) || errors.Is(err, syscall.EOPNOTSUPP) {
return createUnixFallback(dir)
return createUnixFallback()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch is not exercised on CI because the Linux runners have recent enough kernels to succeed in the O_TMPFILE above.

@julio-lopez julio-lopez requested a review from redgoat650 April 23, 2025 00:09
@julio-lopez julio-lopez disabled auto-merge April 23, 2025 03:52
@julio-lopez julio-lopez merged commit e3fc6e0 into kopia:master Apr 23, 2025
26 of 27 checks passed
@julio-lopez julio-lopez deleted the cleanup/createmp branch April 23, 2025 03:55
@julio-lopez
Copy link
Collaborator Author

@jkowalski @Rohit-BM18 @redgoat650 happy to address feedback in a followup PR. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant