Commit Graph

84 Commits

Author SHA1 Message Date
Nalin Dahyabhai 7aedebdccd modernize: JSON doesn't do "omitempty" structs, so stop asking
Drop the "omitempty" tag from the "json" tag on struct members that are
themselves structs, since the JSON encoder doesn't actually do that, per
golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-09-10 13:19:38 -04:00
Nalin Dahyabhai 16680a4dfd modernize: use strings.CutPrefix/SplitSeq/FieldsSeq
Use the CutPrefix(), SplitSeq(), and FieldsSeq() functions from the
strings package when chopping up or iterating over parts of strings, per
golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-09-10 13:11:00 -04:00
Jan Kaluza a0a9ac6380 Switch common, storage and image to monorepo.
Signed-off-by: Jan Kaluza <jkaluza@redhat.com>
2025-08-29 15:04:28 +02:00
Nalin Dahyabhai 473656b9dd copier.Stat(): return owner UID and GID if available
Return owner information for items that we've stat'ed.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-08-11 15:49:28 -04:00
Nalin Dahyabhai 738fa0d3c4 copier.Get(): ensure that directory entries end in "/"
Make sure that entries with Typeflag == TypeDir always end with a "/",
adding it as a suffix.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-08-11 14:10:16 -04:00
Nalin Dahyabhai 9461dd61d4 copier.Get(): strip user and group names from entries
When generating archives, clear user and group names to keep up with
recent changes to the storage library.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-08-11 14:10:16 -04:00
Nalin Dahyabhai 95013b363f copier.Ensure(): also return parent directories
Have Ensure() also return the parent directories of items that it
created, along with information about them that can be used to filter
them out of the layer at commit-time.

This modifies the signature of Ensure(), but it was added in 1.41.0, and
shouldn't (yet) have any external users.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-07-24 13:13:53 -04:00
Nalin Dahyabhai 8807a00976 copier.MkdirOptions: add ModTimeNew
Add an optional ModTimeNew field to MkdirOptions, for setting the
timestamp on newly-created directories.  Note that the timestamps on
parent directories of the topmost newly-created directories are not
something we affect, so they'll be updated as usual.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-07-24 13:13:53 -04:00
Jan Rodák 2717599f93
Ensure extendedGlob returns paths in lexical order
The `filepath.Glob` function does not provide deterministic output. In order to achieve a reproducible build, files must be copied in a deterministic manner, and `filepath.Glob` did not guarantee this. Other functions such as `filepath.Walk` and `os.ReadDir` return deterministic output. So copying files to the image is done in the same order each time.

Fixes: https://issues.redhat.com/browse/RUN-2661

Signed-off-by: Jan Rodák <hony.com@seznam.cz>
2025-05-30 10:32:11 +02:00
openshift-merge-bot[bot] 9986534eea
Merge pull request #6178 from nalind/add-timestamp
add: add a new --timestamp flag
2025-05-28 19:16:00 +00:00
Nalin Dahyabhai dda8e65e84 copier: add Ensure and ConditionalRemove
Add copier.Ensure() and copier.ConditionalRemove(), for controlling the
permissions and datestamps we set on multiple items we create with one
call (along with any parents), and selectively removing multiple items
with one call.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-05-21 09:54:21 -04:00
Nalin Dahyabhai b9a65a9d86 add: add a new --timestamp flag
Add a --timestamp flag to the "add" and "copy" CLIs, along with a
corresponding field in AddAndCopyOptions.

When a timestamp is set, we'll force the timestamp on data copied in to
be the specified value while reading it, so that the content will have
the specified datestamp in the rootfs and when the image is committed.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-05-21 09:51:51 -04:00
Sascha Brawer ada0ac0b89
Fix typo in comment (#6167)
Signed-off-by: Sascha Brawer <sascha@brawer.ch>
2025-05-13 19:04:04 -07:00
Kir Kolyshkin 0835cb4760 Use slices.Clone
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-08 15:26:09 -07:00
Kir Kolyshkin 1ede7ddce7 Use any instead of interface{}
Brought to you by

	gofmt -r 'interface{} -> any' -w .

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-08 10:19:51 -07:00
Kir Kolyshkin e8dba98314 ci: add nolintlint, fix found issues
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-07 13:02:39 -07:00
Kir Kolyshkin 2162b3c93a copier: rm nolint:unparam annotation
This function always returns nil as the first parameter, which makes
unparam linter sad.

Rather than adding //nolint:unparam, let's just move nil to actual
returns.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-07 13:02:34 -07:00
Kir Kolyshkin 52001ac4de Use switch to fix QF1003 warnings
These:

> copier/copier.go:1664:2: QF1003: could use tagged switch on hdr.Typeflag (staticcheck)
> 	if hdr.Typeflag == tar.TypeReg {
> 	^
> pkg/parse/parse.go:708:4: QF1003: could use tagged switch on arr[1] (staticcheck)
> 			if arr[1] == "local" {
> 			^

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-07 13:01:56 -07:00
Kir Kolyshkin 7b15646547 Apply De Morgan's law to fix QF1001 warnings
This fixes the following warnings:

> chroot/run_test.go:319:39: QF1001: could apply De Morgan's law (staticcheck)
> 				if limit == unix.RLIM_INFINITY && !(rlim == nil || (rlim.Soft == unix.RLIM_INFINITY && rlim.Hard == unix.RLIM_INFINITY)) {
> 				                                  ^
> copier/copier.go:1012:92: QF1001: could apply De Morgan's law (staticcheck)
> 			if target, err := os.Readlink(filepath.Join(workingPath, components[0])); err == nil && !(len(components) == 1 && !evaluateFinalComponent) {
> 			                                                                                        ^
> run_linux.go:459:118: QF1001: could apply De Morgan's law (staticcheck)
> 	if !slices.Contains(volumes, resolvconf.DefaultResolvConf) && options.ConfigureNetwork != define.NetworkDisabled && !(len(b.CommonBuildOpts.DNSServers) == 1 && strings.ToLower(b.CommonBuildOpts.DNSServers[0]) == "none") {
> 	                                                                                                                    ^

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-07 13:01:56 -07:00
David Negstad 74cfd3cfc3 Allow extendedGlob to work with Windows paths
Signed-off-by: David Negstad <David.Negstad@microsoft.com>
2025-03-24 18:15:16 -07:00
Jan Rodák 7ca9f3464b
Add --parents option for COPY in Dockerfiles
It also includes an implementation of the --parents flag for the buildah copy command.

Fixes: https://issues.redhat.com/browse/RUN-2193
Fixes: https://github.com/containers/buildah/issues/5557

Signed-off-by: Jan Rodák <hony.com@seznam.cz>
2025-03-18 21:58:27 +01:00
openshift-merge-bot[bot] c7f9925ba7
Merge pull request #5873 from nalind/minor-error-context
Add context to an error message
2024-12-13 18:55:15 +00:00
Nalin Dahyabhai cfd98d782a copier: use .PAXRecords instead of .Xattrs
When processing tar headers, use the .PAXRecords field instead of the
deprecated .Xattrs field.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-12-12 14:54:30 -05:00
Nalin Dahyabhai c41f021306 Add context to an error message
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-12-12 14:03:53 -05:00
Nalin Dahyabhai 9e374f9fd4 copier: handle globbing with "**" path components
Handle glob patterns with "**" path components by expanding "**" to the
set of subdirectories and calling filepath.Glob() on the results.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-09-03 11:46:24 -04:00
Nalin Dahyabhai 8ae99121c1 CI: enable the gofumpt linter
Turn on the gofumpt linter.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-08-15 13:17:44 -04:00
Nalin Dahyabhai fdf1c75cd3 linters: unused arguments shouldn't have names
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-08-07 10:10:35 -04:00
Nalin Dahyabhai d9191e17cd copierWithSubprocess(): try to capture stderr on io.ErrClosedPipe
When we get a tried-to-write-to-closed-pipe error while encoding
something for a coprocess, try to capture error output from the
coprocess and add it to the error message, to hopefully catch a flake
we're seeing in CI.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-05-20 09:35:48 -04:00
Paul Holzinger 292b429f6c
copier: remove libimage dependency via util package
This allows podman to import the copier package on the remote client
without needing the full libimage package.

Based on Miloslav's work: https://github.com/containers/podman/pull/19718

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2023-09-12 14:08:41 +02:00
Chris Evich 92582a75cc
Remove use of deprecated tar.TypeRegA
Fixes golangci-lint message:

`tar.TypeRegA has been deprecated since Go 1.11 and an alternative has
been available since Go 1.1: Use TypeReg instead.`

Signed-off-by: Chris Evich <cevich@redhat.com>
2023-06-12 11:25:03 -04:00
Chris Evich 46eea31588
Replace io/ioutil calls with os calls
In golang 1.19, `io/ioutil` is fully deprecated preventing Buildah from
compiling.  Replace all calls with equivalent calls from the `os`
package.

Signed-off-by: Chris Evich <cevich@redhat.com>
2022-12-06 14:29:32 -05:00
Nalin Dahyabhai 3d755b5eb7 copier.Put(): clear up os/syscall mode bit confusion
When noting that a non-symlink has setuid/setgid/sticky bits, switch
from using "syscall" package bits and syscall.Chmod() to using "os"
package bits and os.Chmod(), and if the item's a directory, record the
updated mode information in the "directoryModes" map that we'll use to
reset its permissions later.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2022-11-09 15:38:40 -05:00
Doug Rabson e867db39b6 copier: Preserve file flags when copying archives on FreeBSD
This approach should also work on darwin but I have only tested on
FreeBSD.

[NO NEW TESTS NEEDED]

Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-10-28 15:09:50 +01:00
Doug Rabson f3a73b7038 copier: work around freebsd bug for "mkdir /"
This call to os.Mkdir got missed the first time I tried to add the
workaround for the FreeBSD EISDIR bug since I was only testing with
'buildah run'. Trying to use 'buildah add' triggers the same bug whan
trying to extract an archive into a container's filesystem.

[NO NEW TESTS NEEDED]

Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-09-21 13:38:46 +01:00
Daniel J Walsh 8d5d763213
Fix stutters
Podman adds an Error: to every error message. So starting an error
message with "error" ends up being reported to the user as

Error: error ...

This patch removes the stutter.

Also ioutil.ReadFile errors report the Path, so wrapping the err message
with the path causes a stutter.

Signed-off-by: Daniel J Walsh dwalsh@redhat.com

[NO NEW TESTS NEEDED]

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2022-09-19 07:11:44 -04:00
Nalin Dahyabhai bb149ea686 Use errors.Is() instead of os.Is{Not,}Exist
If errors for which os.IsExist() or os.IsNotExist() would have returned
true have been wrapped using fmt.Errorf()'s "%w" verb, os.IsExist() and
os.IsNotExist(), not having been retrofitted to use errors.Is(), will
return false.

Use errors.Is() to check if an error is an os.ErrExist or os.ErrNotExist
error instead of calling os.IsExist() or os.IsNotExist().

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2022-07-26 15:36:58 -04:00
Sascha Grunert ce384684c0
Switch to golang native error wrapping
We now use the golang error wrapping format specifier `%w` instead of
the deprecated github.com/pkg/errors package.

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
2022-07-07 11:41:47 +02:00
Doug Rabson b23a20ee64 copier: work around freebsd bug for "mkdir /"
On FreeBSD, os.Mkdir can return EISDIR if the target is the root. As a
workaround, treat EISDIR as a non-error similarly to EEXISTS.

[NO NEW TESTS NEEDED]

Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-06-14 11:04:26 +01:00
Valentin Rothberg 7cc5f1987a copier: add `NoOverwriteNonDirDir` option
Similar to the `NoOverwriteDirNonDir` one, add an option that disables
non-directories from being overwritten by directories.

Required-for: containers/podman/issues/14420
Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
2022-06-07 16:35:44 +02:00
Giuseppe Scrivano cf2bb5a124
copier: move NSS load to new process
do not attempt to load NSS modules at init() time but do it only when
it is needed before a chroot.

[NO NEW TESTS NEEDED]

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2022-05-05 11:47:00 +02:00
OpenShift Merge Robot c45bfcc8aa
Merge pull request #3936 from nalind/copier-sticky-bit
copier.PutOptions: add StripSetuidBit/StripSetgidBit/StripStickyBit
2022-04-28 07:33:04 -04:00
OpenShift Merge Robot 9332113a9f
Merge pull request #3935 from nalind/copier-113
copier.unwrapError(): update for Go 1.16
2022-04-28 06:37:14 -04:00
Nalin Dahyabhai eb38649a25 copier.unwrapError(): update for Go 1.16
Since we started calling into the standard library's io/fs package
directly, we effectively made Go 1.16 our minimum Go version, so we
don't need to keep the workaround for compiling with Go 1.12.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2022-04-27 17:57:31 -04:00
Nalin Dahyabhai dc3a381fe2 copier.PutOptions: add StripSetuidBit/StripSetgidBit/StripStickyBit
Add StripSetuidBit/StripSetgidBit/StripStickyBit flags to
copier.PutOptions, that are interpreted similarly to their counterparts
in copier.GetOptions.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2022-04-27 17:53:53 -04:00
Nalin Dahyabhai 8f6abac5fe copier.Put(): write to read-only directories
Try to improve our ability to write to directories that aren't
writable.  If we encounter an EPERM error while attempting to create an
item, attempt to temporarily make writable the directory that we're
writing the item to, and restore its permissions on our way out.

The error usually isn't seen when run as UID 0, whether in a user
namespace or not, which is usually how we're called, but running the
unit tests as an unprivileged user will verify it.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2022-04-27 16:57:37 -04:00
Giuseppe Scrivano 9852609134
copier: attempt to open the dir before adding it
it is needed to fix a failing test.

commit 985eec5391 changed the behavior
since the directory is not opened first before adding it.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2022-03-30 14:51:10 +02:00
Daniel J Walsh 985eec5391
Switch most calls to filepath.Walk to filepath.WalkDir
Should speed up most walks escpecially if they don't need to
stat every directory entry.

[ NO NEW TESTS NEEDED]

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2022-03-28 16:02:08 -04:00
Nalin Dahyabhai 7631600e72 copier.Put: check for is-not-a-directory using lstat, not stat
When checking if something that we want to overwrite with a directory is
already a directory or not, use lstat instead of stat.  If it's a
symbolic link, it's not a directory.

This is a subtle behavior change, but it's in line with docker build.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-11-30 10:28:53 -05:00
Nalin Dahyabhai 933c8c89fb copier: RemoveAll possibly-directories
When we attempt to remove a directory to make way for a non-directory as
part of extracting content, use RemoveAll() instead of Remove().

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-11-29 17:22:07 -05:00
Nalin Dahyabhai 44a129f586 copier.Get(): try to avoid descending into directories
When processing a directory tree, only descend into a directory that is
marked for exclusion if its path is literally a prefix of an exception
pattern.

Subtly, but in a way that's compatible with docker, this means that if
we exclude directory "subdir", but we've been told to also include
"**/file" (with an exclusion pattern of "!**/file"), we won't descend
into "subdir" and find a file named "subdir/file", because "**/file"
doesn't start with "subdir/".

More generally, exclusion patterns that start with "!" which include any
wildcards before their final component technically won't be treated
correctly.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-08-30 11:04:14 -04:00