Commit graph

89 commits

Author SHA1 Message Date
Pierre Pronchery 3d7c8f0887 cp: avoid a resource leak
In copy_file(), make sure the from_fd file descriptor is closed even
when the operation failed early.

Reported by:	Coverity Scan
CID:		1545036
Sponsored by:	The FreeBSD Foundation

Reviewed by: imp, emaste
Pull Request: https://github.com/freebsd/freebsd-src/pull/1238
2024-05-23 12:04:43 -06:00
Dag-Erling Smørgrav 9075d4cfad cp: Additional sanity check.
Once we've successfully opened the file we've been asked to copy, check
that it's of the same type as FTS told us it was.

MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	allanjude, markj
Differential Revision:	https://reviews.freebsd.org/D44806
2024-04-17 04:03:28 +02:00
Collin Funk 8b86415185 cp: Remove useless ssize_t cast
Both wcount and wresid are ssize_t so this cast is not needed. Just
remove it so the code is easier to read.

Signed-off-by:	Collin Funk <collin.funk1@gmail.com>
Reviewed by:	emaste, zlei
Pull Request:	https://github.com/freebsd/freebsd-src/pull/1116
2024-03-21 11:43:04 -03:00
Dag-Erling Smørgrav 3850927066 cp: Further simplify the core logic.
If the destination file exists but we decide unlink it, set the dne
flag.  This means we don't need to re-check the conditions that would
have caused us to delete the file when we later need to decide whether
to create or replace it.

MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D43064
2023-12-14 17:07:04 +01:00
Dag-Erling Smørgrav d002316fd7 cp: Refactor the core logic.
Rewrite `copy_file()` so the lflag and sflag are handled as early as
possible instead of constantly checking that they're not set and then
handling them at the end.  This also opens the door to changing the
failure logic at some future point (for instance, we might decide to
fall back to copying if `errno` indicates that the file system does not
support links).

MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	kevans, allanjude
Differential Revision:	https://reviews.freebsd.org/D43055
2023-12-14 00:40:55 +01:00
Warner Losh 3e7e3b5bdf cp: Don't warn for chflags() failing with EOPNOTSUPP if flags == 0
From NetBSD's utils.c 1.5 importing importing BSDI change, with light
formatting changes:
    Author: cgd <cgd@NetBSD.org>
    Date:   Wed Feb 26 14:40:51 1997 +0000

    Patch from BSDI (via Keith Bostic):
    >NFS doesn't support chflags; ignore errors unless there's reason
    >to believe we're losing bits.  (Note, this still won't be right
    >if the server supports flags and we were trying to *remove* flags
    >on a file that we copied, i.e., that we didn't create.)

    CVS Info: utils.c 1.6

Obtained from:		NetBSD
Sponsored by:		Netflix

Differential Revision:	https://reviews.freebsd.org/D42674
2023-12-07 13:36:44 -07:00
Warner Losh 5a52e3d00d cp: Add -N flag, inspired by NetBSD's similar flag
Add -N to supress copying of file flags when -p is specified (explicitly
or implicitly). Often times we don't care about the flags or wish to be
able to copy to NFS, and this comes in handy for that. FreeBSD's and
NetBSD's cp are somewhat different, so I had to reimplement all but one
of the patch hunks...

Obtained from:		NetBSD (cp.1 1.25, cp.c 1.37, utils.c 1.28 by elad)
Sponsored by:		Netflix

Differential Revision:	https://reviews.freebsd.org/D42673
2023-12-07 13:36:44 -07:00
Warner Losh e043f37205 bin: Automated cleanup of cdefs and other formatting
Apply the following automated changes to try to eliminate
no-longer-needed sys/cdefs.h includes as well as now-empty
blank lines in a row.

Remove /^#if.*\n#endif.*\n#include\s+<sys/cdefs.h>.*\n/
Remove /\n+#include\s+<sys/cdefs.h>.*\n+#if.*\n#endif.*\n+/
Remove /\n+#if.*\n#endif.*\n+/
Remove /^#if.*\n#endif.*\n/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/types.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/param.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/capsicum.h>/

Sponsored by:		Netflix
2023-11-26 22:23:59 -07:00
Warner Losh 90aea514c6 bin: Remove ancient SCCS tags.
Remove ancient SCCS tags from the tree, automated scripting, with two
minor fixup to keep things compiling. All the common forms in the tree
were removed with a perl script.

Sponsored by:		Netflix
2023-11-26 22:23:28 -07:00
Warner Losh 1d386b48a5 Remove $FreeBSD$: one-line .c pattern
Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
2023-08-16 11:54:42 -06:00
Mateusz Guzik c5b5f2d808 cp: Revert "If copy_file_range(2) fails with EXDEV, use fall-back."
This reverts commit 6433365490.

The error is not valid per api contract, it showed up as a regression
after 15f0b8c309

15f0b8c309 ("zfs: merge openzfs/zfs@9cd71c860 (master)") and was
subsequently in d012836fb6 ("zfs: fix up EXDEV handling for
clone_range").

Sponsored by:	Rubicon Communications, LLC ("Netgate")
2023-04-24 16:04:53 +00:00
Poul-Henning Kamp 6433365490 If copy_file_range(2) fails with EXDEV, use fall-back. 2023-04-04 07:39:59 +00:00
Dag-Erling Smørgrav cb96a0ef00 cp: Minor code cleanup.
* Fix includes in utils.c, cf. style(9).
* Fix type mismatch: readlink(2) returns ssize_t, not int.
* It is not necessary to set errno to 0 as fts_read(3) already does it.

MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	allanjude
Differential Revision:	https://reviews.freebsd.org/D38369
2023-02-03 16:37:37 +01:00
Dag-Erling Smørgrav 6c85042afc cp: Simplify the common case.
* The allocated buffer is only used in the fallback case, so move it
  there.  The argument for passing it in from the caller was that if
  malloc(3) were to fail, we'd want it to fail before we started
  copying anything, but firstly, it was already not in the right place
  to ensure that, and secondly, malloc(3) never fails (except in very
  contrived circumstances, such as an unreasonable RLIMIT_AS or
  RLIMIT_DATA).

* Remove the mmap(2) option.  It is almost never beneficial,
  especially when the alternative is copy_file_range(2), and it adds
  needless complexity and indentation.

MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	rmacklem, mav
Differential Revision:	https://reviews.freebsd.org/D38291
2023-02-02 15:46:04 +01:00
Alexander Motin 35b7759c05 cp: Fix build without VM_AND_BUFFER_CACHE_SYNCHRONIZED.
It allows to not use mmap() for small files, which is not helpful
in case of ZFS.  Should be no functional change.

MFC after:	1 week
2022-08-30 10:51:21 -04:00
Baptiste Daroussin 45b252fc91 cp: fix indentation
No functional changes
2021-01-27 12:18:45 +01:00
Rick Macklem c98a764c68 cp(1): fix performance issue for large non-sparse file copies
PR252358 reported a serious performance problem when
copying a large non-sparse file on a UFS file system.
This problem seems to have been caused by a large
number of SEEK_HOLE operations, with one done
for each copy_file_range(2) call.

This patch modifies cp(1) to use a large (SSIZE_MAX)
len argument, reducing the number of system calls
and resolving the performance issue.

While here, convert the type of the "rcount" from "int"
to "ssize_t" so that it is consistent with that returned
by both read(2) and copy_file_range(2).

PR:	252358
Reviewed by:	asomers
Differential Revision:	https://reviews.freebsd.org/D27937
2021-01-02 16:58:43 -08:00
Alan Somers 1ea95ba231 cp: fall back to read/write if copy_file_range fails
Even though copy_file_range has a file-system agnostic version, it still
fails on devfs (perhaps because the file descriptor is non-seekable?) In
that case, fallback to old-fashioned read/write. Fixes
"cp /dev/null /tmp/null"

PR:		249248
Reported by:	Michael Butler
Reviewed by:	mjg
MFC-With:	365549
Differential Revision:	https://reviews.freebsd.org/D26395
2020-09-11 20:49:36 +00:00
Alan Somers c01816a97f cp: use copy_file_range(2)
This has three advantages over write(2)/read(2):

* Fewer context switches and data copies
* Mostly preserves a file's sparseness
* On some file systems (currently NFS 4.2) the file system will perform the
  copy in an especially efficient way.

Reviewed by:	rmacklem
MFC after:	2 weeks
Sponsored by:	Axcient
Differential Revision:	https://reviews.freebsd.org/D26377
2020-09-10 02:48:55 +00:00
Pedro F. Giffuni 8a16b7a18f General further adoption of SPDX licensing ID tags.
Mainly focus on files that use BSD 3-Clause license.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

Special thanks to Wind River for providing access to "The Duke of
Highlander" tool: an older (2014) run over FreeBSD tree was useful as a
starting point.
2017-11-20 19:49:47 +00:00
Warner Losh fbbd9655e5 Renumber copyright clause 4
Renumber cluase 4 to 3, per what everybody else did when BSD granted
them permission to remove clause 3. My insistance on keeping the same
numbering for legal reasons is too pedantic, so give up on that point.

Submitted by:	Jan Schaumann <jschauma@stevens.edu>
Pull Request:	https://github.com/freebsd/freebsd/pull/96
2017-02-28 23:42:47 +00:00
Bryan Drewery 724fd44827 Cleanup some style(9) issues.
- Whitespace.
- Comments.
- Wrap long lines.

MFC after:	2 weeks
X-MFC-with:	r284105,r284106
Sponsored by:	EMC / Isilon Storage Division
2015-06-08 19:24:18 +00:00
Bryan Drewery 7ea2450f42 Implement '-s' to copy as symlink, similar to the current -l link(2) handling.
This is also implemented in at least GNU coreutils cp.

While here also improve the '-l' handling to not open(2) the source file as
it does not actually need the descriptor.

Sponsored by:	EMC / Isilon Storage Division
2015-06-07 06:30:25 +00:00
Bryan Drewery 14376e1f21 Cleanup some indentation issues. 2015-06-07 03:49:41 +00:00
Jilles Tjoelker 347b9f6de0 cp,mv: Use futimens() instead of utimensat() if possible. 2015-03-23 17:35:05 +00:00
Jilles Tjoelker 5faf2ae160 cp,mv,touch: Set timestamps with nanosecond precision.
This uses utimensat().
2015-01-24 13:50:13 +00:00
Mark Johnston 111a5d7c78 Return with an error from copy_link(), copy_fifo() and copy_special() if
the -n option is specified and the destination file exists.

PR:		bin/174489
Approved by:	rstone (co-mentor)
MFC after:	2 weeks
2013-01-27 05:59:28 +00:00
David E. O'Brien e4573a9a7b Return "failure" as we do for 'cp -i' and a "n" answer.
Otherwise with '-v' we print out the file name as if it was copied:
   /tmp/2gb-card/M0132.CTG not overwritten
   /mnt/DCIM/CANONMSC/M0132.CTG -> /tmp/2gb-card/M0132.CTG
2013-01-23 02:06:20 +00:00
Ed Schouten 99742a231f Change all our own code to use st_*tim instead of st_*timespec.
Also remove some local patches to diff(1) which are now unneeded.
2010-03-28 13:16:08 +00:00
Gavin Atkinson f815125fbc Implement an "-x" option to cp(1), for compatibility with Linux and
feature parity with du(1) and similar:  When set, cp(1) will not traverse
mount points.

Initial patch by:       Graham J Lee   leeg teaching.physics.ox.ac.uk

PR:		bin/88056
Initial patch by: Graham J Lee   leeg teaching.physics.ox.ac.uk
Approved by:	ed (mentor)
MFC after:	1 month
2010-01-17 09:37:31 +00:00
Edward Tomasz Napierala 2689ba2720 Add NFSv4 ACL support to cp(1) and fix a few memory leaks.
Note that this changes error reporting behaviour somewhat - before,
no error was reported if ACL couldn't be copied because the target
filesystem doesn't support ACLs.  Now, it will be reported - of course,
only if there actually is an ACL to copy.

Reviewed by:	rwatson
2009-09-02 08:08:57 +00:00
Jilles Tjoelker f2db75739c Preserve file flags on symlinks in cp -Rp.
This reported ENOSYS before.

PR:		bin/111226 (part of)
Submitted by:	Martin Kammerhofer
Approved by:	ed (mentor)
MFC after:	3 weeks
2009-05-30 10:36:14 +00:00
Ivan Voras e9cbc9a770 Teach cat(1) and cp(1) to use a larger buffer if enough memory is present
in the system. A simple heuristics is used to detect what is "enough"
memory: if number of physmem pages is greater than 32k (equalling 128 MB
on machines with 4 kB pages).

Typical immediate result of these changes is reduction in context switches
and the goal is to increase efficiency by using large buffers:
before: /usr/bin/time -hlp cat file1 > file2
...
       163  voluntary context switches
     11194  involuntary context switches
after: /usr/bin/time -hlp ./cat file1 > file2
...
       417  voluntary context switches
       272  involuntary context switches

Reviewed by:    hackers@ (no objections to earlier version of cat patch)
Approved by:    gnn (mentor)
MFC after:      4 months
2008-10-30 14:05:57 +00:00
Diomidis Spinellis cce2092c73 Add fallback when mmap fails on regular files. Some filesystems,
like smbnetfs, do not support mmap.

Reported by:	Harti Brandt
MFC after:	1 month
2008-10-27 15:21:15 +00:00
Edward Tomasz Napierala 70583fbb77 Remove redundant close(2).
Submitted by:	kensmith
Approved by:	rwatson (mentor)
2008-09-09 12:31:42 +00:00
Edward Tomasz Napierala 2aafc4d48c Fix ACL preservation, apparently broken in 1.47.
Approved by:	rwatson
MFC after:	3 weeks
2008-08-07 07:29:26 +00:00
John Baldwin d140946cf3 Add an '-a' option which is identical to specifying '-RpP' for
compatibility with other implementations.

MFC after:	1 month
2008-03-10 19:58:41 +00:00
Maxim Konovalov 9bdb7f2e54 o Style(9).
Submitted by:	bde
2006-10-07 12:14:50 +00:00
Maxim Konovalov eb82e1a1f8 o Avoid division by zero.
o Place error checking code near to the syscall.

Submitted by:	bde
2006-10-07 12:11:21 +00:00
Maxim Konovalov 6a861e9bfb o Hold copied bytes counter in off_t. This prevents statistics
displayed by SIGINFO handler from overflow.

PR:		bin/104039
Submitted by:	Geoffrey Giesemann
MFC after:	2 weeks
2006-10-06 08:30:33 +00:00
Ruslan Ermilov cbae88b901 - Fix options order.
- Touch manpage's document date.
2006-08-25 09:58:13 +00:00
Ruslan Ermilov d9c7237d04 Remove a stray -a option that probably sneaked in from julian's
attempt to enter append mode twice in vi(1).  :-)
2006-08-25 09:42:16 +00:00
Julian Elischer 6fa363774d Add an option to allow copying of a hierarchy while linking he regular files.
Bikeshedded to death on: hackers
Submitted by:andersonatcenttech.com
MFC in: 1 month
2006-08-24 20:45:38 +00:00
Christian S.J. Peron 9b4261c9b4 Attempt to complete the userspace integration of POSIX.1e extended ACLs.
This includes adding support for ACLs into cp(1) and mv(1) userspace
utilities.

For mv(1), if _PC_ACL_EXTENDED is in effect for the source AND destination
operands, the destination file's ACLs shall reflect the source.

For cp(1), if _PC_ACL_EXTENDED is in effect for both source and destination
operands, and -p has been specified, the ACLs from the source shall be
preserved on the destination.

MFC after:	1 month
2005-09-05 04:36:08 +00:00
Ruslan Ermilov a89237ae95 Sync program's usage() with manpage's SYNOPSIS. 2005-02-09 17:37:39 +00:00
Tim J. Robbins 7807cb2439 Plug file descriptor leak in implementation of -n option. 2004-06-05 02:32:21 +00:00
Mark Murray 6195fb4102 Remove clause 3 from the UCB licenses.
OK'ed by:	imp, core
2004-04-06 20:06:54 +00:00
Alan Cox b9e25f436b Don't mmap(2) and munmap(2) zero-length files.
Submitted by:	Wiktor Niesiobedzki <bsd@w.evip.pl>
2003-11-13 05:26:55 +00:00
John-Mark Gurney a6b05ab6ea support saving both user/group and permissions on symlinks (from PR)
also fix a slight bogon that assumed an fd of 0 was not valid.  Changed
it to be -1.

PR:		bin/25017
Submitted by:	Martin Kammerhofer
2003-06-22 07:02:17 +00:00
Matthew N. Dodd 00d321a2b3 Add a SIGINFO handler. 2003-04-07 11:00:56 +00:00