1 /* install - copy files and set attributes
2 Copyright (C) 89, 90, 91, 95, 1996 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Copy files and set their permission modes and, if possible,
19 their owner and group. Used similarly to `cp'; typically
20 used in Makefiles to copy programs into their destination
21 directories. It can also be used to create the destination
22 directories and any leading directories, and to set the final
23 directory's modes. It refuses to copy files onto themselves.
27 Set the group ownership of the installed file or directory
28 to the group ID of GROUP (default is process's current
29 group). GROUP may also be a numeric group ID.
32 Set the permission mode for the installed file or directory
33 to MODE, which is an octal number (default is 0755).
36 If run as root, set the ownership of the installed file to
37 the user ID of OWNER (default is root). OWNER may also be
40 -c No effect. For compatibility with old Unix versions of install.
43 Strip the symbol tables from installed files.
46 Create a directory and its leading directories, if they
47 do not already exist. Set the owner, group and mode
48 as given on the command line. Any leading directories
49 that are created are also given those attributes.
50 This is different from the SunOS 4.0 install, which gives
51 directories that it creates the default attributes.
53 David MacKenzie <djm@gnu.ai.mit.edu> */
58 #include <sys/types.h>
63 #include "backupfile.h"
64 #include "modechange.h"
70 # include <sys/wait.h>
78 # define BITSPERBYTE 8
81 struct passwd
*getpwnam ();
82 struct group
*getgrnam ();
84 #ifndef _POSIX_VERSION
91 # define endgrent() ((void) 0)
95 # define endpwent() ((void) 0)
98 /* True if C is an ASCII octal digit. */
99 #define isodigit(c) ((c) >= '0' && c <= '7')
101 /* Number of bytes of a file to copy at a time. */
102 #define READ_SIZE (32 * 1024)
105 # define UID_T_MAX ((uid_t)(~((uid_t)1 << (sizeof (uid_t) * BITSPERBYTE - 1))))
109 # define GID_T_MAX ((gid_t)(~((gid_t)1 << (sizeof (gid_t) * BITSPERBYTE - 1))))
118 enum backup_type
get_version ();
120 static int change_attributes
__P ((char *path
, int no_need_to_chown
));
121 static int copy_file
__P ((char *from
, char *to
, int *to_created
));
122 static int install_file_in_dir
__P ((char *from
, char *to_dir
));
123 static int install_file_in_file
__P ((char *from
, char *to
));
124 static void get_ids
__P ((void));
125 static void strip
__P ((char *path
));
126 static void usage
__P ((int status
));
128 /* The name this program was run with, for error messages. */
131 /* The user name that will own the files, or NULL to make the owner
132 the current user ID. */
133 static char *owner_name
;
135 /* The user ID corresponding to `owner_name'. */
136 static uid_t owner_id
;
138 /* The group name that will own the files, or NULL to make the group
139 the current group ID. */
140 static char *group_name
;
142 /* The group ID corresponding to `group_name'. */
143 static gid_t group_id
;
145 /* The permissions to which the files will be set. The umask has
149 /* If nonzero, strip executable files after copying them. */
150 static int strip_files
;
152 /* If nonzero, install a directory instead of a regular file. */
155 /* If nonzero, display usage information and exit. */
156 static int show_help
;
158 /* If nonzero, print the version on standard output and exit. */
159 static int show_version
;
161 static struct option
const long_options
[] =
163 {"strip", no_argument
, NULL
, 's'},
164 {"directory", no_argument
, NULL
, 'd'},
165 {"group", required_argument
, NULL
, 'g'},
166 {"mode", required_argument
, NULL
, 'm'},
167 {"owner", required_argument
, NULL
, 'o'},
168 {"backup", no_argument
, NULL
, 'b'},
169 {"version-control", required_argument
, NULL
, 'V'},
170 {"help", no_argument
, &show_help
, 1},
171 {"version", no_argument
, &show_version
, 1},
176 main (int argc
, char **argv
)
180 char *symbolic_mode
= NULL
;
181 int make_backups
= 0;
184 program_name
= argv
[0];
185 setlocale (LC_ALL
, "");
186 bindtextdomain (PACKAGE
, LOCALEDIR
);
187 textdomain (PACKAGE
);
196 version
= getenv ("SIMPLE_BACKUP_SUFFIX");
198 simple_backup_suffix
= version
;
199 version
= getenv ("VERSION_CONTROL");
201 while ((optc
= getopt_long (argc
, argv
, "bcsdg:m:o:V:S:", long_options
,
223 symbolic_mode
= optarg
;
229 simple_backup_suffix
= optarg
;
241 printf ("install - %s\n", PACKAGE_VERSION
);
248 /* Check for invalid combinations of arguments. */
249 if (dir_arg
&& strip_files
)
251 _("the strip option may not be used when installing a directory"));
254 backup_type
= get_version (version
);
256 if (optind
== argc
|| (optind
== argc
- 1 && !dir_arg
))
258 error (0, 0, _("too few arguments"));
264 struct mode_change
*change
= mode_compile (symbolic_mode
, 0);
265 if (change
== MODE_INVALID
)
266 error (1, 0, _("invalid mode `%s'"), symbolic_mode
);
267 else if (change
== MODE_MEMORY_EXHAUSTED
)
268 error (1, 0, _("virtual memory exhausted"));
269 mode
= mode_adjust (0, change
);
276 for (; optind
< argc
; ++optind
)
279 make_path (argv
[optind
], mode
, mode
, owner_id
, group_id
, 0, NULL
);
284 if (optind
== argc
- 2)
286 if (!isdir (argv
[argc
- 1]))
287 errors
= install_file_in_file (argv
[argc
- 2], argv
[argc
- 1]);
289 errors
= install_file_in_dir (argv
[argc
- 2], argv
[argc
- 1]);
293 if (!isdir (argv
[argc
- 1]))
295 for (; optind
< argc
- 1; ++optind
)
297 errors
|= install_file_in_dir (argv
[optind
], argv
[argc
- 1]);
305 /* Copy file FROM onto file TO and give TO the appropriate
307 Return 0 if successful, 1 if an error occurs. */
310 install_file_in_file (char *from
, char *to
)
313 int no_need_to_chown
;
315 if (copy_file (from
, to
, &to_created
))
319 no_need_to_chown
= (to_created
320 && owner_name
== NULL
321 && group_name
== NULL
);
322 return change_attributes (to
, no_need_to_chown
);
325 /* Copy file FROM into directory TO_DIR, keeping its same name,
326 and give the copy the appropriate attributes.
327 Return 0 if successful, 1 if not. */
330 install_file_in_dir (char *from
, char *to_dir
)
336 from_base
= basename (from
);
337 to
= xmalloc ((unsigned) (strlen (to_dir
) + strlen (from_base
) + 2));
338 stpcpy (stpcpy (stpcpy (to
, to_dir
), "/"), from_base
);
339 ret
= install_file_in_file (from
, to
);
344 /* A chunk of a file being copied. */
345 static char buffer
[READ_SIZE
];
347 /* Copy file FROM onto file TO, creating TO if necessary.
348 Return 0 if the copy is successful, 1 if not. If the copy is
349 successful, set *TO_CREATED to nonzero if TO was created (if it did
350 not exist or did, but was unlinked) and to zero otherwise. If the
351 copy fails, don't modify *TO_CREATED. */
354 copy_file (char *from
, char *to
, int *to_created
)
359 struct stat from_stats
, to_stats
;
360 int target_created
= 1;
362 if (stat (from
, &from_stats
))
364 error (0, errno
, "%s", from
);
367 if (!S_ISREG (from_stats
.st_mode
))
369 error (0, 0, _("`%s' is not a regular file"), from
);
372 if (stat (to
, &to_stats
) == 0)
374 if (!S_ISREG (to_stats
.st_mode
))
376 error (0, 0, _("`%s' is not a regular file"), to
);
379 if (from_stats
.st_dev
== to_stats
.st_dev
380 && from_stats
.st_ino
== to_stats
.st_ino
)
382 error (0, 0, _("`%s' and `%s' are the same file"), from
, to
);
386 /* The destination file exists. Try to back it up if required. */
387 if (backup_type
!= none
)
389 char *tmp_backup
= find_backup_file_name (to
);
392 if (tmp_backup
== NULL
)
393 error (1, 0, "virtual memory exhausted");
394 dst_backup
= (char *) alloca (strlen (tmp_backup
) + 1);
395 strcpy (dst_backup
, tmp_backup
);
397 if (rename (to
, dst_backup
))
401 error (0, errno
, "cannot backup `%s'", to
);
407 /* If unlink fails, try to proceed anyway. */
412 fromfd
= open (from
, O_RDONLY
, 0);
415 error (0, errno
, "%s", from
);
419 /* Make sure to open the file in a mode that allows writing. */
420 tofd
= open (to
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
423 error (0, errno
, "%s", to
);
428 while ((bytes
= safe_read (fromfd
, buffer
, READ_SIZE
)) > 0)
429 if (full_write (tofd
, buffer
, bytes
) < 0)
431 error (0, errno
, "%s", to
);
437 error (0, errno
, "%s", from
);
441 if (close (fromfd
) < 0)
443 error (0, errno
, "%s", from
);
446 if (close (tofd
) < 0)
448 error (0, errno
, "%s", to
);
452 *to_created
= target_created
;
461 /* Set the attributes of file or directory PATH.
462 If NO_NEED_TO_CHOWN is nonzero, don't call chown.
463 Return 0 if successful, 1 if not. */
466 change_attributes (char *path
, int no_need_to_chown
)
470 /* chown must precede chmod because on some systems,
471 chown clears the set[ug]id bits for non-superusers,
472 resulting in incorrect permissions.
473 On System V, users can give away files with chown and then not
474 be able to chmod them. So don't give files away.
476 We don't pass -1 to chown to mean "don't change the value"
477 because SVR3 and earlier non-BSD systems don't support that.
479 We don't normally ignore errors from chown because the idea of
480 the install command is that the file is supposed to end up with
481 precisely the attributes that the user specified (or defaulted).
482 If the file doesn't end up with the group they asked for, they'll
483 want to know. But AFS returns EPERM when you try to change a
484 file's group; thus the kludge. */
486 if (!no_need_to_chown
&& chown (path
, owner_id
, group_id
)
492 if (chmod (path
, mode
))
496 error (0, err
, "%s", path
);
502 /* Strip the symbol table from the file PATH.
503 We could dig the magic number out of the file first to
504 determine whether to strip it, but the header files and
505 magic numbers vary so much from system to system that making
506 it portable would be very difficult. Not worth the effort. */
517 error (1, errno
, _("cannot fork"));
520 execlp ("strip", "strip", path
, (char *) NULL
);
521 error (1, errno
, _("cannot run strip"));
523 default: /* Parent. */
524 /* Parent process. */
525 while (pid
!= wait (&status
)) /* Wait for kid to finish. */
531 /* Initialize the user and group ownership of the files to install. */
541 pw
= getpwnam (owner_name
);
545 if (xstrtol (owner_name
, NULL
, 0, &tmp_long
, NULL
) != LONGINT_OK
546 || tmp_long
< 0 || tmp_long
> UID_T_MAX
)
547 error (1, 0, _("invalid user `%s'"), owner_name
);
548 owner_id
= (uid_t
) tmp_long
;
551 owner_id
= pw
->pw_uid
;
555 owner_id
= getuid ();
559 gr
= getgrnam (group_name
);
563 if (xstrtol (group_name
, NULL
, 0, &tmp_long
, NULL
) != LONGINT_OK
564 || tmp_long
< 0 || tmp_long
> (long) GID_T_MAX
)
565 error (1, 0, _("invalid group `%s'"), group_name
);
566 group_id
= (gid_t
) tmp_long
;
569 group_id
= gr
->gr_gid
;
573 group_id
= getgid ();
580 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
585 Usage: %s [OPTION]... SOURCE DEST (1st format)\n\
586 or: %s [OPTION]... SOURCE... DIRECTORY (2nd format)\n\
587 or: %s -d [OPTION]... DIRECTORY... (3nd format)\n\
589 program_name
, program_name
, program_name
);
591 In first two formats, copy SOURCE to DEST or multiple SOURCE(s) to\n\
592 DIRECTORY, while setting permission modes and owner/group. In third\n\
593 format, make all components of the given DIRECTORY(ies).\n\
596 -d, --directory create [leading] directories, mandatory for 3rd format\n\
597 -g, --group=GROUP set group ownership, instead of process' current group\n\
598 -m, --mode=MODE set permission mode (as in chmod), instead of rw-r--r--\n\
599 -o, --owner=OWNER set ownership (super-user only)\n\
600 -s, --strip strip symbol tables, only for 1st and 2nd formats\n\
601 -b, --backup make backup before removal\n\
602 -S, --suffix=SUFFIX override the usual backup suffix\n\
603 -V, --version-control=WORD override the usual version control\n\
604 --help display this help and exit\n\
605 --version output version information and exit\n\
607 The backup suffix is ~, unless set with SIMPLE_BACKUP_SUFFIX. The\n\
608 version control may be set with VERSION_CONTROL, values are:\n\
610 t, numbered make numbered backups\n\
611 nil, existing numbered if numbered backups exist, simple otherwise\n\
612 never, simple always make simple backups\n"));