Commit Graph

75 Commits

Author SHA1 Message Date
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
Nalin Dahyabhai 854b38c745 copier.Put(): set xattrs after ownership
Set extended attributes on files _after_ setting their ownership, so
that security-sensitive attributes ("security.capability" among them)
won't get quietly cleared from under us if we set them before calling
chown().

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-06-18 16:03:53 -04:00
Chris Evich 470cf23fbd
Fix copy race while walking paths
During a copy operation which descends through a directory tree,
It's possible for a referenced file to become inaccessible (by unlink
or permission change or whatever).  During the walk of paths to copy,
an `Lstat()` is run on each item, and any error passed into the handler
function to deal with.  Subsequently, if there is no error, the file
is examined for inclusion/exclusion by the handler.

Unfortunately, this introduces a TOCTOU race condition for files which
become inaccessible even if they would otherwise be excluded.  For
example a file or directory under /proc or /sys (which frequently and
unpredictably change).  This was the original cause encountered during
podman integration testing.

It's impractical to actually fix this race at the file-level, without
introducing negative effects to any source-container operations.  It's
also questionably useful to offer a command-line option to offload the
choice to the user.  Instead, follow the behavior of the `tar` command
for this situation: Issue a warning to the user, and ignore the
problematic item (don't copy it).

Also add a test resembling the podman test which originally caught this
race.  While not reliable, it does introduce a non-zero chance of
hitting the race condition - and handling the new warning properly.

Signed-off-by: Chris Evich <cevich@redhat.com>
2021-05-07 12:58:47 -04:00
Daniel J Walsh 37e9d254cc
Fix copier when using globs
In Docker if you are copying more then one object, and
one of them is successful, then the command is successful. Currently in
buildah each glob has to be successful. This PR matches Buildah to
Docker.

Fixes: https://github.com/containers/podman/issues/9594

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-04-22 10:39:23 -04:00
Nalin Dahyabhai 745cee8aa5 copier: add Remove()
Add copier.Remove().

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-04-20 14:09:50 -04:00
Nalin Dahyabhai 1dbf430563 copier: add GetOptions.NoCrossDevice
Add a NoCrossDevice flag to GetOptions, telling it to ignore
subdirectories on devices different than the top reference directory
that we start from, i.e., ignore the contents of mounted filesystems.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-04-14 10:19:19 -04:00
Giuseppe Scrivano 1b3d250899
copier: ignore sockets
sockets are not supported by tarsplit, ignore them.

Closes: https://github.com/containers/buildah/issues/3074

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2021-03-12 17:06:22 +01:00
Nalin Dahyabhai 34ae47a226 copier: add GetOptions.IgnoreUnreadable
Add an IgnoreUnreadable flag to copier.GetOptions to suppress errors
from copier.Get() that would pass the os.IsPermission() test, if they're
encountered while attempting to read files or descend into directories.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-03-04 15:32:32 -05:00
Nalin Dahyabhai effb375b5a ADD/COPY: create the destination directory first, chroot to it
Always create the destination directory first when ADDing or COPYing
content into a container, then extract contents into it using the
destination directory as the chroot instead of the container's root
directory.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-03-02 16:29:43 -05:00
Nalin Dahyabhai 51ef0a47da copier.GetOptions: add NoDerefSymLinks
Add a NoDerefSymlinks flag to force items that are matched to the Globs
we're given to be treated as symlinks, rather than dereferencing them as
we would need to do for sources for ADD or COPY.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-03-02 16:29:43 -05:00