📄 install.c
字号:
int i; const char *dest = file[n_files - 1]; if (!isdir (dest)) { error (0, 0, _("installing multiple files, but last argument, %s \is not a directory"), quote (dest)); usage (EXIT_FAILURE); } dest_info_init (&x); for (i = 0; i < n_files - 1; i++) { errors |= install_file_in_dir (file[i], dest, &x); } } } exit (errors);}/* Copy file FROM onto file TO, creating any missing parent directories of TO. Return 0 if successful, 1 if an error occurs */static intinstall_file_to_path (const char *from, const char *to, const struct cp_options *x){ char *dest_dir; int fail = 0; dest_dir = dir_name (to); /* check to make sure this is a path (not install a b ) */ if (!STREQ (dest_dir, ".") && !isdir (dest_dir)) { /* Someone will probably ask for a new option or three to specify owner, group, and permissions for parent directories. Remember that this option is intended mainly to help installers when the distribution doesn't provide proper install rules. */#define DIR_MODE (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) fail = make_path (dest_dir, DIR_MODE, DIR_MODE, owner_id, group_id, 0, (x->verbose ? _("creating directory %s") : NULL)); } if (fail == 0) fail = install_file_in_file (from, to, x); free (dest_dir); return fail;}/* Copy file FROM onto file TO and give TO the appropriate attributes. Return 0 if successful, 1 if an error occurs. */static intinstall_file_in_file (const char *from, const char *to, const struct cp_options *x){ if (copy_file (from, to, x)) return 1; if (strip_files) strip (to); if (change_attributes (to)) return 1; if (x->preserve_timestamps) return change_timestamps (from, to); return 0;}/* Copy file FROM into directory TO_DIR, keeping its same name, and give the copy the appropriate attributes. Return 0 if successful, 1 if not. */static intinstall_file_in_dir (const char *from, const char *to_dir, const struct cp_options *x){ const char *from_base; char *to; int ret; from_base = base_name (from); to = path_concat (to_dir, from_base, NULL); ret = install_file_in_file (from, to, x); free (to); return ret;}/* Copy file FROM onto file TO, creating TO if necessary. Return 0 if the copy is successful, 1 if not. */static intcopy_file (const char *from, const char *to, const struct cp_options *x){ int fail; int nonexistent_dst = 0; int copy_into_self; /* Allow installing from non-regular files like /dev/null. Charles Karney reported that some Sun version of install allows that and that sendmail's installation process relies on the behavior. */ if (isdir (from)) { error (0, 0, _("%s is a directory"), quote (from)); return 1; } fail = copy (from, to, nonexistent_dst, x, ©_into_self, NULL); return fail;}/* Set the attributes of file or directory PATH. Return 0 if successful, 1 if not. */static intchange_attributes (const char *path){ int err = 0; /* chown must precede chmod because on some systems, chown clears the set[ug]id bits for non-superusers, resulting in incorrect permissions. On System V, users can give away files with chown and then not be able to chmod them. So don't give files away. We don't normally ignore errors from chown because the idea of the install command is that the file is supposed to end up with precisely the attributes that the user specified (or defaulted). If the file doesn't end up with the group they asked for, they'll want to know. But AFS returns EPERM when you try to change a file's group; thus the kludge. */ if (chown (path, owner_id, group_id)#ifdef AFS && errno != EPERM#endif ) { error (0, errno, "cannot change ownership of %s", quote (path)); err = 1; } if (!err && chmod (path, mode)) { error (0, errno, "cannot change permissions of %s", quote (path)); err = 1; } return err;}/* Set the timestamps of file TO to match those of file FROM. Return 0 if successful, 1 if not. */static intchange_timestamps (const char *from, const char *to){ struct stat stb; struct utimbuf utb; if (stat (from, &stb)) { error (0, errno, _("cannot obtain time stamps for %s"), quote (from)); return 1; } /* There's currently no interface to set file timestamps with better than 1-second resolution, so discard any fractional part of the source timestamp. */ utb.actime = stb.st_atime; utb.modtime = stb.st_mtime; if (utime (to, &utb)) { error (0, errno, _("cannot set time stamps for %s"), quote (to)); return 1; } return 0;}/* Strip the symbol table from the file PATH. We could dig the magic number out of the file first to determine whether to strip it, but the header files and magic numbers vary so much from system to system that making it portable would be very difficult. Not worth the effort. */static voidstrip (const char *path){ int status; pid_t pid = fork (); switch (pid) { case -1: error (EXIT_FAILURE, errno, _("fork system call failed")); break; case 0: /* Child. */ execlp ("strip", "strip", path, NULL); error (EXIT_FAILURE, errno, _("cannot run strip")); break; default: /* Parent. */ /* Parent process. */ while (pid != wait (&status)) /* Wait for kid to finish. */ /* Do nothing. */ ; if (status) error (EXIT_FAILURE, 0, _("strip failed")); break; }}/* Initialize the user and group ownership of the files to install. */static voidget_ids (void){ struct passwd *pw; struct group *gr; if (owner_name) { pw = getpwnam (owner_name); if (pw == NULL) { unsigned long int tmp; if (xstrtoul (owner_name, NULL, 0, &tmp, NULL) != LONGINT_OK || UID_T_MAX < tmp) error (EXIT_FAILURE, 0, _("invalid user %s"), quote (owner_name)); owner_id = tmp; } else owner_id = pw->pw_uid; endpwent (); } else owner_id = (uid_t) -1; if (group_name) { gr = getgrnam (group_name); if (gr == NULL) { unsigned long int tmp; if (xstrtoul (group_name, NULL, 0, &tmp, NULL) != LONGINT_OK || GID_T_MAX < tmp) error (EXIT_FAILURE, 0, _("invalid group %s"), quote (group_name)); group_id = tmp; } else group_id = gr->gr_gid; endgrent (); } else group_id = (gid_t) -1;}voidusage (int status){ if (status != 0) fprintf (stderr, _("Try `%s --help' for more information.\n"), program_name); else { printf (_("\Usage: %s [OPTION]... SOURCE DEST (1st format)\n\ or: %s [OPTION]... SOURCE... DIRECTORY (2nd format)\n\ or: %s -d [OPTION]... DIRECTORY... (3rd format)\n\"), program_name, program_name, program_name); fputs (_("\In the first two formats, copy SOURCE to DEST or multiple SOURCE(s) to\n\the existing DIRECTORY, while setting permission modes and owner/group.\n\In the third format, create all components of the given DIRECTORY(ies).\n\\n\"), stdout); fputs (_("\Mandatory arguments to long options are mandatory for short options too.\n\"), stdout); fputs (_("\ --backup[=CONTROL] make a backup of each existing destination file\n\ -b like --backup but does not accept an argument\n\ -c (ignored)\n\ -d, --directory treat all arguments as directory names; create all\n\ components of the specified directories\n\"), stdout); fputs (_("\ -D create all leading components of DEST except the last,\n\ then copy SOURCE to DEST; useful in the 1st format\n\ -g, --group=GROUP set group ownership, instead of process' current group\n\ -m, --mode=MODE set permission mode (as in chmod), instead of rwxr-xr-x\n\ -o, --owner=OWNER set ownership (super-user only)\n\"), stdout); fputs (_("\ -p, --preserve-timestamps apply access/modification times of SOURCE files\n\ to corresponding destination files\n\ -s, --strip strip symbol tables, only for 1st and 2nd formats\n\ -S, --suffix=SUFFIX override the usual backup suffix\n\ -v, --verbose print the name of each directory as it is created\n\"), stdout); fputs (HELP_OPTION_DESCRIPTION, stdout); fputs (VERSION_OPTION_DESCRIPTION, stdout); fputs (_("\\n\The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\The version control method may be selected via the --backup option or through\n\the VERSION_CONTROL environment variable. Here are the values:\n\\n\"), stdout); fputs (_("\ none, off never make backups (even if --backup is given)\n\ numbered, t make numbered backups\n\ existing, nil numbered if numbered backups exist, simple otherwise\n\ simple, never always make simple backups\n\"), stdout); printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT); } exit (status);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -