Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Replace os filesystem operations with pkg/filesystem #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 20, 2024

Conversation

mafredri
Copy link
Member

@mafredri mafredri commented Aug 19, 2024

This PR replaces most os filesystem operations with vfs.FS invoked via pkg/filesystem.

Part of coder/envbuilder#230.

This allows us to run Kaniko (ish) safely with read-only or memory filesystems. It also allows us to transparently decompress cache archives to support multi-stage build cache probing.

Kaniko still calls out to a few libraries (e.g. godirwalk, otiai10/copy, etc.) that don't support swapping out os. That's not something we have to deal with for cache probing so it's not a high priority.

For simplicity, filesystem.CreateTemp and filesystem.MkdirTemp just call out to their os equivalents for simplicity but kept in the filesystem package for consistency (os write operations).

Note: Initially this PR used afero, but afero was replaced due to inconsistency with stdlib functions (e.g. afero.Walk != filepath.Walk) and convoluted API ((afero.Lstater).LstatIfPossible, among other).

@mafredri mafredri changed the title Replace os file operations with pkg/filesystem Replace os filesystem operations with pkg/filesystem Aug 19, 2024
@mafredri mafredri force-pushed the mafredri/filesystem-package-for-fs-operations branch from 1b29556 to 426b71f Compare August 19, 2024 13:43
@mafredri mafredri force-pushed the mafredri/filesystem-package-for-fs-operations branch from 3d92ec1 to 48336bc Compare August 20, 2024 09:17
@mafredri mafredri marked this pull request as ready for review August 20, 2024 09:37
@mafredri mafredri requested review from johnstcn and mtojek August 20, 2024 09:46
@mafredri mafredri self-assigned this Aug 20, 2024
Copy link
Member

@mtojek mtojek left a comment

Choose a reason for hiding this comment

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

LGTM, I skimmed through the PR, and compared umasks.

Side note: as we own the repository, could we remove the vendor dir?

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

👍 Nice work!

)

// FS is the default filesystem used by the package.
var FS vfs.FS = vfs.OSFS
Copy link
Member

Choose a reason for hiding this comment

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

Should we protect this with a mutex?

Copy link
Member Author

Choose a reason for hiding this comment

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

We could, but swapping out the filesystem after the lib has been used is a bit iffy. Would result in an inconsistent filesystem. 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm going to merge it like this for now, let's add it if we find a use-case 👍🏻. Most operations would need to change from filesystem.FS to filesystem.FS() though so it would mean another commit with many changes, unfortunately.

_, _, errno := syscall.Syscall(unix.SYS_SYNCFS, dir.Fd(), 0, 0)
d, ok := dir.(*os.File)
if !ok {
return nil, nil, errors.New("unable to convert directory to file")
Copy link
Member

Choose a reason for hiding this comment

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

Would it make more sense here to call sync on filepath.Dir(dir) instead of erroring?

Copy link
Member Author

Choose a reason for hiding this comment

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

We need the file descriptor (.Fd()) for the sycall, so there's nothing we can do if this fails.

We could do an interface test for Fd() uintptr instead though as it's more flexible.

@mafredri
Copy link
Member Author

Side note: as we own the repository, could we remove the vendor dir?

@mtojek I'll make a separate PR for that 👍🏻

@mafredri mafredri merged commit 1564449 into main Aug 20, 2024
9 checks passed
@mafredri mafredri deleted the mafredri/filesystem-package-for-fs-operations branch August 20, 2024 12:54
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.

3 participants