MCPcopy Create free account
hub / github.com/coreutils/coreutils / do_copy

Function do_copy

src/cp.c:669–880  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

667 Return true if successful. */
668
669static bool
670do_copy (int n_files, char **file, char const *target_directory,
671 bool no_target_directory, struct cp_options *x)
672{
673 if (n_files <= !target_directory)
674 {
675 if (n_files <= 0)
676 error (0, 0, _("missing file operand"));
677 else
678 error (0, 0, _("missing destination file operand after %s"),
679 quoteaf (file[0]));
680 usage (EXIT_FAILURE);
681 }
682
683 struct stat sb;
684 sb.st_mode = 0;
685 int target_dirfd = AT_FDCWD;
686 bool new_dst = false;
687 bool ok = true;
688 if (no_target_directory)
689 {
690 if (target_directory)
691 error (EXIT_FAILURE, 0,
692 _("cannot combine --target-directory (-t) "
693 "and --no-target-directory (-T)"));
694 if (2 < n_files)
695 {
696 error (0, 0, _("extra operand %s"), quoteaf (file[2]));
697 usage (EXIT_FAILURE);
698 }
699 }
700 else if (target_directory)
701 {
702 target_dirfd = target_directory_operand (target_directory, &sb);
703 if (! target_dirfd_valid (target_dirfd))
704 error (EXIT_FAILURE, errno, _("target directory %s"),
705 quoteaf (target_directory));
706 }
707 else
708 {
709 char const *lastfile = file[n_files - 1];
710 int fd = target_directory_operand (lastfile, &sb);
711 if (target_dirfd_valid (fd))
712 {
713 target_dirfd = fd;
714 target_directory = lastfile;
715 n_files--;
716 }
717 else
718 {
719 int err = errno;
720 if (err == ENOENT)
721 new_dst = true;
722
723 /* The last operand LASTFILE cannot be opened as a directory.
724 If there are more than two operands, report an error.
725
726 Also, report an error if LASTFILE is known to be a directory

Callers 1

mainFunction · 0.85

Calls 9

target_directory_operandFunction · 0.85
target_dirfd_validFunction · 0.85
dest_info_initFunction · 0.85
src_info_initFunction · 0.85
make_dir_parents_privateFunction · 0.85
copyFunction · 0.85
re_protectFunction · 0.85
usageFunction · 0.70
statClass · 0.70

Tested by

no test coverage detected