[367] | 1 | /*
|
---|
| 2 | * Copyright (c) 1987, 1993
|
---|
| 3 | * The Regents of the University of California. All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | * 1. Redistributions of source code must retain the above copyright
|
---|
| 9 | * notice, this list of conditions and the following disclaimer.
|
---|
| 10 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 12 | * documentation and/or other materials provided with the distribution.
|
---|
| 13 | * 3. All advertising materials mentioning features or use of this software
|
---|
| 14 | * must display the following acknowledgement:
|
---|
| 15 | * This product includes software developed by the University of
|
---|
| 16 | * California, Berkeley and its contributors.
|
---|
| 17 | * 4. Neither the name of the University nor the names of its contributors
|
---|
| 18 | * may be used to endorse or promote products derived from this software
|
---|
| 19 | * without specific prior written permission.
|
---|
| 20 | *
|
---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
| 31 | * SUCH DAMAGE.
|
---|
| 32 | */
|
---|
| 33 |
|
---|
[3145] | 34 | #if 0 /*ndef lint*/
|
---|
[367] | 35 | static const char copyright[] =
|
---|
| 36 | "@(#) Copyright (c) 1987, 1993\n\
|
---|
| 37 | The Regents of the University of California. All rights reserved.\n";
|
---|
| 38 | #endif /* not lint */
|
---|
| 39 |
|
---|
| 40 | #if 0
|
---|
| 41 | #ifndef lint
|
---|
| 42 | static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93";
|
---|
| 43 | #endif /* not lint */
|
---|
| 44 |
|
---|
| 45 | #include <sys/cdefs.h>
|
---|
| 46 | __FBSDID("$FreeBSD: src/usr.bin/xinstall/xinstall.c,v 1.66 2005/01/25 14:34:57 ssouhlal Exp $");
|
---|
[370] | 47 | #endif
|
---|
[367] | 48 |
|
---|
[3214] | 49 | #define FAKES_NO_GETOPT_H
|
---|
[2113] | 50 | #include "config.h"
|
---|
[370] | 51 | #ifndef _MSC_VER
|
---|
[809] | 52 | # include <sys/param.h>
|
---|
[3109] | 53 | # if !defined(__HAIKU__) && !defined(__gnu_hurd__)
|
---|
[2546] | 54 | # include <sys/mount.h>
|
---|
| 55 | # endif
|
---|
[809] | 56 | # include <sys/wait.h>
|
---|
| 57 | # include <sys/time.h>
|
---|
| 58 | #endif /* !_MSC_VER */
|
---|
[367] | 59 | #include <sys/stat.h>
|
---|
| 60 |
|
---|
| 61 | #include <ctype.h>
|
---|
[370] | 62 | #include "err.h"
|
---|
[367] | 63 | #include <errno.h>
|
---|
| 64 | #include <fcntl.h>
|
---|
| 65 | #include <grp.h>
|
---|
| 66 | #include <paths.h>
|
---|
| 67 | #include <pwd.h>
|
---|
| 68 | #include <stdio.h>
|
---|
| 69 | #include <stdlib.h>
|
---|
| 70 | #include <string.h>
|
---|
[2546] | 71 | #ifndef __HAIKU__
|
---|
| 72 | # include <sysexits.h>
|
---|
| 73 | #endif
|
---|
[3062] | 74 | #ifdef __NetBSD__
|
---|
| 75 | # include <util.h>
|
---|
| 76 | # define strtofflags(a, b, c) string_to_flags(a, b, c)
|
---|
| 77 | #endif
|
---|
[367] | 78 | #include <unistd.h>
|
---|
[374] | 79 | #if defined(__EMX__) || defined(_MSC_VER)
|
---|
| 80 | # include <process.h>
|
---|
| 81 | #endif
|
---|
[3214] | 82 | #include "getopt_r.h"
|
---|
[809] | 83 | #ifdef __sun__
|
---|
| 84 | # include "solfakes.h"
|
---|
| 85 | #endif
|
---|
| 86 | #ifdef _MSC_VER
|
---|
| 87 | # include "mscfakes.h"
|
---|
| 88 | #endif
|
---|
[2546] | 89 | #ifdef __HAIKU__
|
---|
| 90 | # include "haikufakes.h"
|
---|
| 91 | #endif
|
---|
[1183] | 92 | #include "kmkbuiltin.h"
|
---|
[3101] | 93 | #include "k/kDefs.h" /* for K_OS */
|
---|
[3114] | 94 | #include "dos2unix.h"
|
---|
[1183] | 95 |
|
---|
| 96 |
|
---|
[1710] | 97 | extern void * bsd_setmode(const char *p);
|
---|
| 98 | extern mode_t bsd_getmode(const void *bbox, mode_t omode);
|
---|
[370] | 99 |
|
---|
| 100 | #ifndef MAXBSIZE
|
---|
[1193] | 101 | # define MAXBSIZE 0x20000
|
---|
[374] | 102 | #endif
|
---|
[370] | 103 |
|
---|
[367] | 104 | #define MAX_CMP_SIZE (16 * 1024 * 1024)
|
---|
| 105 |
|
---|
| 106 | #define DIRECTORY 0x01 /* Tell install it's a directory. */
|
---|
| 107 | #define SETFLAGS 0x02 /* Tell install to set flags. */
|
---|
| 108 | #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
|
---|
| 109 | #define BACKUP_SUFFIX ".old"
|
---|
| 110 |
|
---|
[368] | 111 | #ifndef O_BINARY
|
---|
| 112 | # define O_BINARY 0
|
---|
| 113 | #endif
|
---|
[367] | 114 |
|
---|
[371] | 115 | #ifndef EFTYPE
|
---|
| 116 | # define EFTYPE EINVAL
|
---|
[374] | 117 | #endif
|
---|
[371] | 118 |
|
---|
[1194] | 119 | #if defined(__WIN32__) || defined(__WIN64__) || defined(__OS2__)
|
---|
| 120 | # define IS_SLASH(ch) ((ch) == '/' || (ch) == '\\')
|
---|
| 121 | #else
|
---|
| 122 | # define IS_SLASH(ch) ((ch) == '/')
|
---|
| 123 | #endif
|
---|
| 124 |
|
---|
[3215] | 125 |
|
---|
[3192] | 126 | /*********************************************************************************************************************************
|
---|
| 127 | * Structures and Typedefs *
|
---|
| 128 | *********************************************************************************************************************************/
|
---|
| 129 | typedef struct INSTALLINSTANCE
|
---|
| 130 | {
|
---|
| 131 | PKMKBUILTINCTX pCtx;
|
---|
[367] | 132 |
|
---|
[3192] | 133 | gid_t gid;
|
---|
| 134 | uid_t uid;
|
---|
| 135 | int dobackup, docompare, dodir, dopreserve, dostrip, nommap, safecopy, verbose, mode_given;
|
---|
| 136 | mode_t mode;
|
---|
| 137 | const char *suffix;
|
---|
| 138 | int ignore_perm_errors;
|
---|
| 139 | int hard_link_files_when_possible;
|
---|
[3583] | 140 | int verbose_hard_link_refusal;
|
---|
| 141 | int verbose_hard_link_mode_mismatch;
|
---|
[3192] | 142 | int dos2unix;
|
---|
| 143 | } INSTALLINSTANCE;
|
---|
| 144 | typedef INSTALLINSTANCE *PINSTALLINSTANCE;
|
---|
| 145 |
|
---|
[3583] | 146 | enum
|
---|
| 147 | {
|
---|
| 148 | kInstOpt_Help = 261,
|
---|
| 149 | kInstOpt_Version,
|
---|
| 150 | kInstOpt_Verbose,
|
---|
| 151 | kInstOpt_Quiet,
|
---|
| 152 | kInstOpt_IgnorePermErrors,
|
---|
| 153 | kInstOpt_NoIgnorePermErrors,
|
---|
| 154 | kInstOpt_HardLinkFilesWhenPossible,
|
---|
| 155 | kInstOpt_NoHardLinkFilesWhenPossible,
|
---|
| 156 | kInstOpt_Dos2Unix,
|
---|
| 157 | kInstOpt_Unix2Dos,
|
---|
| 158 | kInstOpt_VerboseHardLinkRefusal,
|
---|
| 159 | kInstOpt_QuietHardLinkRefusal,
|
---|
| 160 | kInstOpt_VerboseHardLinkModeMismatch,
|
---|
| 161 | kInstOpt_QuietHardLinkModeMismatch
|
---|
| 162 | };
|
---|
[3192] | 163 |
|
---|
[3583] | 164 |
|
---|
[3192] | 165 | /*********************************************************************************************************************************
|
---|
| 166 | * Global Variables *
|
---|
| 167 | *********************************************************************************************************************************/
|
---|
[1183] | 168 | static struct option long_options[] =
|
---|
| 169 | {
|
---|
[3583] | 170 | { "help", no_argument, 0, kInstOpt_Help },
|
---|
| 171 | { "version", no_argument, 0, kInstOpt_Version },
|
---|
| 172 | { "quiet", no_argument, 0, kInstOpt_Quiet },
|
---|
| 173 | { "ignore-perm-errors", no_argument, 0, kInstOpt_IgnorePermErrors },
|
---|
| 174 | { "no-ignore-perm-errors", no_argument, 0, kInstOpt_NoIgnorePermErrors },
|
---|
| 175 | { "hard-link-files-when-possible", no_argument, 0, kInstOpt_HardLinkFilesWhenPossible },
|
---|
| 176 | { "no-hard-link-files-when-possible", no_argument, 0, kInstOpt_NoHardLinkFilesWhenPossible },
|
---|
| 177 | { "verbose-hard-link-refusal", no_argument, 0, kInstOpt_VerboseHardLinkRefusal },
|
---|
| 178 | { "quiet-hard-link-refusal", no_argument, 0, kInstOpt_QuietHardLinkRefusal },
|
---|
| 179 | { "verbose-hard-link-mode-mismatch", no_argument, 0, kInstOpt_VerboseHardLinkModeMismatch },
|
---|
| 180 | { "quiet-hard-link-mode-mismatch", no_argument, 0, kInstOpt_QuietHardLinkModeMismatch },
|
---|
| 181 | { "dos2unix", no_argument, 0, kInstOpt_Dos2Unix },
|
---|
| 182 | { "unix2dos", no_argument, 0, kInstOpt_Unix2Dos },
|
---|
| 183 | #if 1 /* GNU coreutils compatibility: */
|
---|
| 184 | { "compare", no_argument, 0, 'C' },
|
---|
| 185 | { "directory", no_argument, 0, 'd' },
|
---|
| 186 | { "group", required_argument, 0, 'g' },
|
---|
| 187 | { "mode", required_argument, 0, 'm' },
|
---|
| 188 | { "owner", required_argument, 0, 'o' },
|
---|
| 189 | { "strip", no_argument, 0, 's' },
|
---|
| 190 | { "suffix", required_argument, 0, 'S' },
|
---|
| 191 | { "verbose", no_argument, 0, 'v' },
|
---|
| 192 | #endif
|
---|
[1183] | 193 | { 0, 0, 0, 0 },
|
---|
| 194 | };
|
---|
| 195 |
|
---|
| 196 |
|
---|
[3192] | 197 | static int copy(PINSTALLINSTANCE, int, const char *, int *, const char *);
|
---|
[3115] | 198 | static int compare(int, size_t, int, size_t);
|
---|
[3192] | 199 | static int create_newfile(PINSTALLINSTANCE, const char *, int, struct stat *);
|
---|
[368] | 200 | static int create_tempfile(const char *, char *, size_t);
|
---|
[3192] | 201 | static int install(PINSTALLINSTANCE, const char *, const char *, u_long, u_int);
|
---|
| 202 | static int install_dir(PINSTALLINSTANCE, char *);
|
---|
| 203 | static u_long numeric_id(PINSTALLINSTANCE, const char *, const char *);
|
---|
| 204 | static int strip(PINSTALLINSTANCE, const char *);
|
---|
| 205 | static int usage(PKMKBUILTINCTX, int);
|
---|
[1194] | 206 | static char *last_slash(const char *);
|
---|
[3114] | 207 | static KBOOL needs_dos2unix_conversion(const char *pszFilename);
|
---|
| 208 | static KBOOL needs_unix2dos_conversion(const char *pszFilename);
|
---|
[368] | 209 |
|
---|
[367] | 210 | int
|
---|
[3192] | 211 | kmk_builtin_install(int argc, char *argv[], char ** envp, PKMKBUILTINCTX pCtx)
|
---|
[367] | 212 | {
|
---|
[3192] | 213 | INSTALLINSTANCE This;
|
---|
[3214] | 214 | struct getopt_state_r gos;
|
---|
[367] | 215 | struct stat from_sb, to_sb;
|
---|
| 216 | mode_t *set;
|
---|
[370] | 217 | u_long fset = 0;
|
---|
[367] | 218 | int ch, no_target;
|
---|
| 219 | u_int iflags;
|
---|
| 220 | char *flags;
|
---|
| 221 | const char *group, *owner, *to_name;
|
---|
[1730] | 222 | (void)envp;
|
---|
[367] | 223 |
|
---|
[3192] | 224 | /* Initialize global instance data. */
|
---|
| 225 | This.pCtx = pCtx;
|
---|
| 226 | This.mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
|
---|
| 227 | This.suffix = BACKUP_SUFFIX;
|
---|
| 228 | This.gid = 0;
|
---|
| 229 | This.uid = 0;
|
---|
| 230 | This.dobackup = 0;
|
---|
| 231 | This.docompare = 0;
|
---|
| 232 | This.dodir = 0;
|
---|
| 233 | This.dopreserve = 0;
|
---|
| 234 | This.dostrip = 0;
|
---|
| 235 | This.nommap = 0;
|
---|
| 236 | This.safecopy = 0;
|
---|
| 237 | This.verbose = 0;
|
---|
| 238 | This.mode_given = 0;
|
---|
| 239 | This.ignore_perm_errors = geteuid() != 0;
|
---|
| 240 | This.hard_link_files_when_possible = 0;
|
---|
[3583] | 241 | This.verbose_hard_link_refusal = 0;
|
---|
| 242 | This.verbose_hard_link_mode_mismatch = 0;
|
---|
[3192] | 243 | This.dos2unix = 0;
|
---|
[375] | 244 |
|
---|
[367] | 245 | iflags = 0;
|
---|
| 246 | group = owner = NULL;
|
---|
[3214] | 247 | getopt_initialize_r(&gos, argc, argv, "B:bCcdf:g:Mm:o:pSsv", long_options, envp, pCtx);
|
---|
| 248 | while ((ch = getopt_long_r(&gos, NULL)) != -1)
|
---|
[1329] | 249 | switch(ch) {
|
---|
[367] | 250 | case 'B':
|
---|
[3214] | 251 | This.suffix = gos.optarg;
|
---|
[367] | 252 | /* FALLTHROUGH */
|
---|
| 253 | case 'b':
|
---|
[3192] | 254 | This.dobackup = 1;
|
---|
[367] | 255 | break;
|
---|
| 256 | case 'C':
|
---|
[3192] | 257 | This.docompare = 1;
|
---|
[367] | 258 | break;
|
---|
| 259 | case 'c':
|
---|
| 260 | /* For backwards compatibility. */
|
---|
| 261 | break;
|
---|
| 262 | case 'd':
|
---|
[3192] | 263 | This.dodir = 1;
|
---|
[367] | 264 | break;
|
---|
[370] | 265 | case 'f':
|
---|
[3109] | 266 | #if defined(UF_IMMUTABLE) && K_OS != K_OS_GNU_KFBSD && K_OS != K_OS_GNU_HURD
|
---|
[367] | 267 | flags = optarg;
|
---|
| 268 | if (strtofflags(&flags, &fset, NULL))
|
---|
[3192] | 269 | return errx(pCtx, EX_USAGE, "%s: invalid flag", flags);
|
---|
[367] | 270 | iflags |= SETFLAGS;
|
---|
[758] | 271 | #else
|
---|
[370] | 272 | (void)flags;
|
---|
[368] | 273 | #endif
|
---|
[367] | 274 | break;
|
---|
| 275 | case 'g':
|
---|
[3214] | 276 | group = gos.optarg;
|
---|
[367] | 277 | break;
|
---|
| 278 | case 'M':
|
---|
[3192] | 279 | This.nommap = 1;
|
---|
[367] | 280 | break;
|
---|
[368] | 281 | case 'm':
|
---|
[3214] | 282 | if (!(set = bsd_setmode(gos.optarg)))
|
---|
| 283 | return errx(pCtx, EX_USAGE, "invalid file mode: %s", gos.optarg);
|
---|
[3192] | 284 | This.mode = bsd_getmode(set, 0);
|
---|
[367] | 285 | free(set);
|
---|
[3192] | 286 | This.mode_given = 1;
|
---|
[367] | 287 | break;
|
---|
| 288 | case 'o':
|
---|
[3214] | 289 | owner = gos.optarg;
|
---|
[367] | 290 | break;
|
---|
| 291 | case 'p':
|
---|
[3192] | 292 | This.docompare = This.dopreserve = 1;
|
---|
[367] | 293 | break;
|
---|
| 294 | case 'S':
|
---|
[3192] | 295 | This.safecopy = 1;
|
---|
[3583] | 296 | This.verbose_hard_link_refusal = 0;
|
---|
[367] | 297 | break;
|
---|
| 298 | case 's':
|
---|
[3192] | 299 | This.dostrip = 1;
|
---|
[3583] | 300 | This.verbose_hard_link_refusal = 0;
|
---|
[367] | 301 | break;
|
---|
| 302 | case 'v':
|
---|
[3583] | 303 | case kInstOpt_Verbose:
|
---|
[3192] | 304 | This.verbose = 1;
|
---|
[367] | 305 | break;
|
---|
[3583] | 306 | case kInstOpt_Quiet:
|
---|
| 307 | This.verbose = 0;
|
---|
| 308 | break;
|
---|
| 309 | case kInstOpt_Help:
|
---|
[3192] | 310 | usage(pCtx, 0);
|
---|
[1183] | 311 | return 0;
|
---|
[3583] | 312 | case kInstOpt_Version:
|
---|
[1183] | 313 | return kbuild_version(argv[0]);
|
---|
[3583] | 314 | case kInstOpt_IgnorePermErrors:
|
---|
[3192] | 315 | This.ignore_perm_errors = 1;
|
---|
[1730] | 316 | break;
|
---|
[3583] | 317 | case kInstOpt_NoIgnorePermErrors:
|
---|
[3192] | 318 | This.ignore_perm_errors = 0;
|
---|
[1730] | 319 | break;
|
---|
[3583] | 320 | case kInstOpt_HardLinkFilesWhenPossible:
|
---|
[3192] | 321 | This.hard_link_files_when_possible = 1;
|
---|
[2476] | 322 | break;
|
---|
[3583] | 323 | case kInstOpt_NoHardLinkFilesWhenPossible:
|
---|
[3192] | 324 | This.hard_link_files_when_possible = 0;
|
---|
[2476] | 325 | break;
|
---|
[3583] | 326 | case kInstOpt_VerboseHardLinkRefusal:
|
---|
| 327 | This.verbose_hard_link_refusal = 1;
|
---|
| 328 | break;
|
---|
| 329 | case kInstOpt_QuietHardLinkRefusal:
|
---|
| 330 | This.verbose_hard_link_refusal = 0;
|
---|
| 331 | break;
|
---|
| 332 | case kInstOpt_VerboseHardLinkModeMismatch:
|
---|
| 333 | This.verbose_hard_link_mode_mismatch = 1;
|
---|
| 334 | break;
|
---|
| 335 | case kInstOpt_QuietHardLinkModeMismatch:
|
---|
| 336 | This.verbose_hard_link_mode_mismatch = 0;
|
---|
| 337 | break;
|
---|
| 338 | case kInstOpt_Dos2Unix:
|
---|
[3192] | 339 | This.dos2unix = 1;
|
---|
[3583] | 340 | This.verbose_hard_link_refusal = 0;
|
---|
[3114] | 341 | break;
|
---|
[3583] | 342 | case kInstOpt_Unix2Dos:
|
---|
[3192] | 343 | This.dos2unix = -1;
|
---|
[3583] | 344 | This.verbose_hard_link_refusal = 0;
|
---|
[3114] | 345 | break;
|
---|
[367] | 346 | case '?':
|
---|
| 347 | default:
|
---|
[3192] | 348 | return usage(pCtx, 1);
|
---|
[367] | 349 | }
|
---|
[3214] | 350 | argc -= gos.optind;
|
---|
| 351 | argv += gos.optind;
|
---|
[367] | 352 |
|
---|
| 353 | /* some options make no sense when creating directories */
|
---|
[3192] | 354 | if (This.dostrip && This.dodir) {
|
---|
| 355 | warnx(pCtx, "-d and -s may not be specified together");
|
---|
| 356 | return usage(pCtx, 1);
|
---|
[367] | 357 | }
|
---|
| 358 |
|
---|
| 359 | /* must have at least two arguments, except when creating directories */
|
---|
[3192] | 360 | if (argc == 0 || (argc == 1 && !This.dodir))
|
---|
| 361 | return usage(pCtx, 1);
|
---|
[367] | 362 |
|
---|
[3114] | 363 | /* and unix2dos doesn't combine well with a couple of other options. */
|
---|
[3192] | 364 | if (This.dos2unix != 0) {
|
---|
| 365 | if (This.docompare) {
|
---|
| 366 | warnx(pCtx, "-C/-p and --dos2unix/unix2dos may not be specified together");
|
---|
| 367 | return usage(pCtx, 1);
|
---|
[3114] | 368 | }
|
---|
[3192] | 369 | if (This.dostrip) {
|
---|
| 370 | warnx(pCtx, "-s and --dos2unix/unix2dos may not be specified together");
|
---|
| 371 | return usage(pCtx, 1);
|
---|
[3114] | 372 | }
|
---|
| 373 | }
|
---|
| 374 |
|
---|
[367] | 375 | /* need to make a temp copy so we can compare stripped version */
|
---|
[3192] | 376 | if (This.docompare && This.dostrip)
|
---|
| 377 | This.safecopy = 1;
|
---|
[367] | 378 |
|
---|
| 379 | /* get group and owner id's */
|
---|
| 380 | if (group != NULL) {
|
---|
[370] | 381 | #ifndef _MSC_VER
|
---|
| 382 | struct group *gp;
|
---|
[367] | 383 | if ((gp = getgrnam(group)) != NULL)
|
---|
[3226] | 384 | This.gid = gp->gr_gid;
|
---|
[367] | 385 | else
|
---|
[374] | 386 | #endif
|
---|
[370] | 387 | {
|
---|
[3192] | 388 | This.gid = (gid_t)numeric_id(&This, group, "group");
|
---|
| 389 | if (This.gid == (gid_t)-1)
|
---|
[370] | 390 | return 1;
|
---|
| 391 | }
|
---|
[367] | 392 | } else
|
---|
[3192] | 393 | This.gid = (gid_t)-1;
|
---|
[367] | 394 |
|
---|
| 395 | if (owner != NULL) {
|
---|
[370] | 396 | #ifndef _MSC_VER
|
---|
| 397 | struct passwd *pp;
|
---|
[367] | 398 | if ((pp = getpwnam(owner)) != NULL)
|
---|
[3226] | 399 | This.uid = pp->pw_uid;
|
---|
[367] | 400 | else
|
---|
[374] | 401 | #endif
|
---|
[370] | 402 | {
|
---|
[3192] | 403 | This.uid = (uid_t)numeric_id(&This, owner, "user");
|
---|
| 404 | if (This.uid == (uid_t)-1)
|
---|
[370] | 405 | return 1;
|
---|
| 406 | }
|
---|
[367] | 407 | } else
|
---|
[3192] | 408 | This.uid = (uid_t)-1;
|
---|
[367] | 409 |
|
---|
[3192] | 410 | if (This.dodir) {
|
---|
[370] | 411 | for (; *argv != NULL; ++argv) {
|
---|
[3192] | 412 | int rc = install_dir(&This, *argv);
|
---|
[370] | 413 | if (rc)
|
---|
| 414 | return rc;
|
---|
| 415 | }
|
---|
| 416 | return EX_OK;
|
---|
[367] | 417 | /* NOTREACHED */
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | no_target = stat(to_name = argv[argc - 1], &to_sb);
|
---|
| 421 | if (!no_target && S_ISDIR(to_sb.st_mode)) {
|
---|
[370] | 422 | for (; *argv != to_name; ++argv) {
|
---|
[3192] | 423 | int rc = install(&This, *argv, to_name, fset, iflags | DIRECTORY);
|
---|
[370] | 424 | if (rc)
|
---|
| 425 | return rc;
|
---|
| 426 | }
|
---|
| 427 | return EX_OK;
|
---|
[367] | 428 | }
|
---|
| 429 |
|
---|
| 430 | /* can't do file1 file2 directory/file */
|
---|
| 431 | if (argc != 2) {
|
---|
[3192] | 432 | warnx(pCtx, "wrong number or types of arguments");
|
---|
| 433 | return usage(pCtx, 1);
|
---|
[367] | 434 | }
|
---|
| 435 |
|
---|
| 436 | if (!no_target) {
|
---|
| 437 | if (stat(*argv, &from_sb))
|
---|
[3192] | 438 | return err(pCtx, EX_OSERR, "%s", *argv);
|
---|
[367] | 439 | if (!S_ISREG(to_sb.st_mode)) {
|
---|
| 440 | errno = EFTYPE;
|
---|
[3192] | 441 | return err(pCtx, EX_OSERR, "%s", to_name);
|
---|
[367] | 442 | }
|
---|
| 443 | if (to_sb.st_dev == from_sb.st_dev &&
|
---|
[370] | 444 | to_sb.st_dev != 0 &&
|
---|
| 445 | to_sb.st_ino == from_sb.st_ino &&
|
---|
[2480] | 446 | to_sb.st_ino != 0 &&
|
---|
[3192] | 447 | !This.hard_link_files_when_possible)
|
---|
| 448 | return errx(pCtx, EX_USAGE,
|
---|
[370] | 449 | "%s and %s are the same file", *argv, to_name);
|
---|
[367] | 450 | }
|
---|
[3192] | 451 | return install(&This, *argv, to_name, fset, iflags);
|
---|
[367] | 452 | }
|
---|
| 453 |
|
---|
[3192] | 454 | #ifdef KMK_BUILTIN_STANDALONE
|
---|
[3389] | 455 | mode_t g_fUMask;
|
---|
[3192] | 456 | int main(int argc, char **argv, char **envp)
|
---|
| 457 | {
|
---|
| 458 | KMKBUILTINCTX Ctx = { "kmk_install", NULL };
|
---|
[3389] | 459 | umask(g_fUMask = umask(0077));
|
---|
[3192] | 460 | return kmk_builtin_install(argc, argv, envp, &Ctx);
|
---|
| 461 | }
|
---|
| 462 | #endif
|
---|
| 463 |
|
---|
[370] | 464 | static u_long
|
---|
[3192] | 465 | numeric_id(PINSTALLINSTANCE pThis, const char *name, const char *type)
|
---|
[367] | 466 | {
|
---|
| 467 | u_long val;
|
---|
| 468 | char *ep;
|
---|
| 469 |
|
---|
| 470 | /*
|
---|
| 471 | * XXX
|
---|
| 472 | * We know that uid_t's and gid_t's are unsigned longs.
|
---|
| 473 | */
|
---|
| 474 | errno = 0;
|
---|
| 475 | val = strtoul(name, &ep, 10);
|
---|
| 476 | if (errno)
|
---|
[3192] | 477 | return err(pThis->pCtx, -1, "%s", name);
|
---|
[367] | 478 | if (*ep != '\0')
|
---|
[3192] | 479 | return errx(pThis->pCtx, -1, "unknown %s %s", type, name);
|
---|
[367] | 480 | return (val);
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 | /*
|
---|
| 484 | * install --
|
---|
| 485 | * build a path name and install the file
|
---|
| 486 | */
|
---|
[370] | 487 | static int
|
---|
[3192] | 488 | install(PINSTALLINSTANCE pThis, const char *from_name, const char *to_name, u_long fset, u_int flags)
|
---|
[367] | 489 | {
|
---|
| 490 | struct stat from_sb, temp_sb, to_sb;
|
---|
| 491 | struct timeval tvb[2];
|
---|
| 492 | int devnull, files_match, from_fd, serrno, target;
|
---|
| 493 | int tempcopy, temp_fd, to_fd;
|
---|
| 494 | char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
|
---|
[370] | 495 | int rc = EX_OK;
|
---|
[367] | 496 |
|
---|
| 497 | files_match = 0;
|
---|
| 498 | from_fd = -1;
|
---|
| 499 | to_fd = -1;
|
---|
[370] | 500 | temp_fd = -1;
|
---|
[367] | 501 |
|
---|
| 502 | /* If try to install NULL file to a directory, fails. */
|
---|
[374] | 503 | if (flags & DIRECTORY
|
---|
[370] | 504 | #if defined(__EMX__) || defined(_MSC_VER)
|
---|
| 505 | || ( stricmp(from_name, _PATH_DEVNULL)
|
---|
| 506 | && stricmp(from_name, "nul")
|
---|
[2476] | 507 | # ifdef __EMX__
|
---|
[370] | 508 | && stricmp(from_name, "/dev/nul")
|
---|
[2476] | 509 | # endif
|
---|
[370] | 510 | )
|
---|
| 511 | #else
|
---|
| 512 | || strcmp(from_name, _PATH_DEVNULL)
|
---|
| 513 | #endif
|
---|
| 514 | ) {
|
---|
[367] | 515 | if (stat(from_name, &from_sb))
|
---|
[3192] | 516 | return err(pThis->pCtx, EX_OSERR, "%s", from_name);
|
---|
[367] | 517 | if (!S_ISREG(from_sb.st_mode)) {
|
---|
| 518 | errno = EFTYPE;
|
---|
[3192] | 519 | return err(pThis->pCtx, EX_OSERR, "%s", from_name);
|
---|
[367] | 520 | }
|
---|
| 521 | /* Build the target path. */
|
---|
| 522 | if (flags & DIRECTORY) {
|
---|
| 523 | (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
|
---|
| 524 | to_name,
|
---|
[1194] | 525 | (p = last_slash(from_name)) ? ++p : from_name);
|
---|
[367] | 526 | to_name = pathbuf;
|
---|
| 527 | }
|
---|
| 528 | devnull = 0;
|
---|
| 529 | } else {
|
---|
| 530 | devnull = 1;
|
---|
| 531 | }
|
---|
| 532 |
|
---|
| 533 | target = stat(to_name, &to_sb) == 0;
|
---|
| 534 |
|
---|
| 535 | /* Only install to regular files. */
|
---|
| 536 | if (target && !S_ISREG(to_sb.st_mode)) {
|
---|
| 537 | errno = EFTYPE;
|
---|
[3192] | 538 | warn(pThis->pCtx, "%s", to_name);
|
---|
[370] | 539 | return EX_OK;
|
---|
[367] | 540 | }
|
---|
| 541 |
|
---|
| 542 | /* Only copy safe if the target exists. */
|
---|
[3192] | 543 | tempcopy = pThis->safecopy && target;
|
---|
[367] | 544 |
|
---|
[2476] | 545 | /* Try hard linking if wanted and possible. */
|
---|
[3192] | 546 | if (pThis->hard_link_files_when_possible)
|
---|
[2476] | 547 | {
|
---|
[2500] | 548 | #ifdef KBUILD_OS_OS2
|
---|
| 549 | const char *why_not = "not supported on OS/2";
|
---|
| 550 | #else
|
---|
[2476] | 551 | const char *why_not = NULL;
|
---|
| 552 | if (devnull) {
|
---|
| 553 | why_not = "/dev/null";
|
---|
[3192] | 554 | } else if (pThis->dostrip) {
|
---|
[2476] | 555 | why_not = "strip (-s)";
|
---|
[3192] | 556 | } else if (pThis->docompare) {
|
---|
[2476] | 557 | why_not = "compare (-C)";
|
---|
[3192] | 558 | } else if (pThis->dobackup) {
|
---|
[2476] | 559 | why_not = "backup (-b/-B)";
|
---|
[3192] | 560 | } else if (pThis->safecopy) {
|
---|
[2476] | 561 | why_not = "safe copy (-S)";
|
---|
[2543] | 562 | } else if (lstat(from_name, &temp_sb)) {
|
---|
| 563 | why_not = "lstat on source failed";
|
---|
| 564 | } else if (S_ISLNK(temp_sb.st_mode)) {
|
---|
| 565 | why_not = "symlink";
|
---|
| 566 | } else if (!S_ISREG(temp_sb.st_mode)) {
|
---|
| 567 | why_not = "not regular file";
|
---|
[2500] | 568 | # if defined(KBUILD_OS_WINDOWS) || defined(KBUILD_OS_OS2)
|
---|
[3192] | 569 | } else if ((pThis->mode & S_IWUSR) != (from_sb.st_mode & S_IWUSR)) {
|
---|
[2500] | 570 | # else
|
---|
[3192] | 571 | } else if (pThis->mode != (from_sb.st_mode & ALLPERMS)) {
|
---|
[2500] | 572 | # endif
|
---|
[3583] | 573 | if ( pThis->verbose_hard_link_mode_mismatch
|
---|
| 574 | || pThis->verbose_hard_link_refusal)
|
---|
| 575 | kmk_builtin_ctx_printf(pThis->pCtx, 0,
|
---|
| 576 | "install: warning: Not hard linking, mode differs: 0%03o, desires 0%03o\n"
|
---|
| 577 | "install: src path '%s'\n"
|
---|
| 578 | "install: dst path '%s'\n",
|
---|
| 579 | (from_sb.st_mode & ALLPERMS), pThis->mode, from_name, to_name);
|
---|
[2482] | 580 | why_not = NULL;
|
---|
[3192] | 581 | } else if (pThis->uid != (uid_t)-1 && pThis->uid != from_sb.st_uid) {
|
---|
[2476] | 582 | why_not = "uid mismatch";
|
---|
[3192] | 583 | } else if (pThis->gid != (gid_t)-1 && pThis->gid != from_sb.st_gid) {
|
---|
[2476] | 584 | why_not = "gid mismatch";
|
---|
[3192] | 585 | } else if (pThis->dos2unix > 0 && needs_dos2unix_conversion(from_name)) {
|
---|
[3114] | 586 | why_not = "dos2unix";
|
---|
[3192] | 587 | } else if (pThis->dos2unix < 0 && needs_unix2dos_conversion(from_name)) {
|
---|
[3114] | 588 | why_not = "unix2dos";
|
---|
[2476] | 589 | } else {
|
---|
| 590 | int rcLink = link(from_name, to_name);
|
---|
| 591 | if (rcLink != 0 && errno == EEXIST) {
|
---|
| 592 | unlink(to_name);
|
---|
| 593 | rcLink = link(from_name, to_name);
|
---|
| 594 | }
|
---|
| 595 | if (rcLink == 0) {
|
---|
[3192] | 596 | if (pThis->verbose)
|
---|
| 597 | kmk_builtin_ctx_printf(pThis->pCtx, 0,
|
---|
| 598 | "install: %s -> %s (hardlinked)\n", from_name, to_name);
|
---|
[2476] | 599 | goto l_done;
|
---|
| 600 | }
|
---|
[3192] | 601 | if (pThis->verbose)
|
---|
| 602 | kmk_builtin_ctx_printf(pThis->pCtx, 0, "install: hard linking '%s' to '%s' failed: %s\n",
|
---|
[2476] | 603 | to_name, from_name, strerror(errno));
|
---|
| 604 | why_not = NULL;
|
---|
| 605 | }
|
---|
[2500] | 606 | #endif
|
---|
[3583] | 607 | if (why_not && pThis->verbose_hard_link_refusal)
|
---|
[3192] | 608 | kmk_builtin_ctx_printf(pThis->pCtx, 0, "install: not hard linking '%s' to '%s' because: %s\n",
|
---|
| 609 | to_name, from_name, why_not);
|
---|
[2476] | 610 |
|
---|
| 611 | /* Can't hard link or we failed, continue as nothing happend. */
|
---|
| 612 | }
|
---|
| 613 |
|
---|
[3219] | 614 | if (!devnull && (from_fd = open(from_name, O_RDONLY | O_BINARY | KMK_OPEN_NO_INHERIT, 0)) < 0)
|
---|
[3192] | 615 | return err(pThis->pCtx, EX_OSERR, "%s", from_name);
|
---|
[367] | 616 |
|
---|
| 617 | /* If we don't strip, we can compare first. */
|
---|
[3192] | 618 | if (pThis->docompare && !pThis->dostrip && target) {
|
---|
[3219] | 619 | if ((to_fd = open(to_name, O_RDONLY | O_BINARY | KMK_OPEN_NO_INHERIT, 0)) < 0) {
|
---|
[3192] | 620 | rc = err(pThis->pCtx, EX_OSERR, "%s", to_name);
|
---|
[370] | 621 | goto l_done;
|
---|
| 622 | }
|
---|
[367] | 623 | if (devnull)
|
---|
| 624 | files_match = to_sb.st_size == 0;
|
---|
| 625 | else
|
---|
[3115] | 626 | files_match = !compare(from_fd, (size_t)from_sb.st_size,
|
---|
| 627 | to_fd, (size_t)to_sb.st_size);
|
---|
[367] | 628 |
|
---|
| 629 | /* Close "to" file unless we match. */
|
---|
[370] | 630 | if (!files_match) {
|
---|
[367] | 631 | (void)close(to_fd);
|
---|
[370] | 632 | to_fd = -1;
|
---|
| 633 | }
|
---|
[367] | 634 | }
|
---|
| 635 |
|
---|
| 636 | if (!files_match) {
|
---|
| 637 | if (tempcopy) {
|
---|
| 638 | to_fd = create_tempfile(to_name, tempfile,
|
---|
| 639 | sizeof(tempfile));
|
---|
[370] | 640 | if (to_fd < 0) {
|
---|
[3192] | 641 | rc = err(pThis->pCtx, EX_OSERR, "%s", tempfile);
|
---|
[370] | 642 | goto l_done;
|
---|
| 643 | }
|
---|
[367] | 644 | } else {
|
---|
[3192] | 645 | if ((to_fd = create_newfile(pThis, to_name, target, &to_sb)) < 0) {
|
---|
| 646 | rc = err(pThis->pCtx, EX_OSERR, "%s", to_name);
|
---|
[370] | 647 | goto l_done;
|
---|
| 648 | }
|
---|
[3192] | 649 | if (pThis->verbose)
|
---|
| 650 | kmk_builtin_ctx_printf(pThis->pCtx, 0, "install: %s -> %s\n", from_name, to_name);
|
---|
[367] | 651 | }
|
---|
[370] | 652 | if (!devnull) {
|
---|
[3192] | 653 | rc = copy(pThis, from_fd, from_name, &to_fd, tempcopy ? tempfile : to_name);
|
---|
[370] | 654 | if (rc)
|
---|
| 655 | goto l_done;
|
---|
| 656 | }
|
---|
[367] | 657 | }
|
---|
| 658 |
|
---|
[3192] | 659 | if (pThis->dostrip) {
|
---|
[374] | 660 | #if defined(__EMX__) || defined(_MSC_VER)
|
---|
[370] | 661 | /* close before we strip. */
|
---|
| 662 | close(to_fd);
|
---|
| 663 | to_fd = -1;
|
---|
[374] | 664 | #endif
|
---|
[3192] | 665 | rc = strip(pThis, tempcopy ? tempfile : to_name);
|
---|
[370] | 666 | if (rc)
|
---|
| 667 | goto l_done;
|
---|
[367] | 668 |
|
---|
| 669 | /*
|
---|
| 670 | * Re-open our fd on the target, in case we used a strip
|
---|
| 671 | * that does not work in-place -- like GNU binutils strip.
|
---|
| 672 | */
|
---|
[374] | 673 | #if !defined(__EMX__) && !defined(_MSC_VER)
|
---|
[367] | 674 | close(to_fd);
|
---|
[374] | 675 | #endif
|
---|
[3219] | 676 | to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY | O_BINARY | KMK_OPEN_NO_INHERIT, 0);
|
---|
[370] | 677 | if (to_fd < 0) {
|
---|
[3192] | 678 | rc = err(pThis->pCtx, EX_OSERR, "stripping %s", to_name);
|
---|
[370] | 679 | goto l_done;
|
---|
| 680 | }
|
---|
[367] | 681 | }
|
---|
| 682 |
|
---|
| 683 | /*
|
---|
| 684 | * Compare the stripped temp file with the target.
|
---|
| 685 | */
|
---|
[3192] | 686 | if (pThis->docompare && pThis->dostrip && target) {
|
---|
[367] | 687 | temp_fd = to_fd;
|
---|
| 688 |
|
---|
| 689 | /* Re-open to_fd using the real target name. */
|
---|
[3219] | 690 | if ((to_fd = open(to_name, O_RDONLY | O_BINARY | KMK_OPEN_NO_INHERIT, 0)) < 0) {
|
---|
[3192] | 691 | rc = err(pThis->pCtx, EX_OSERR, "%s", to_name);
|
---|
[370] | 692 | goto l_done;
|
---|
| 693 | }
|
---|
[367] | 694 |
|
---|
| 695 | if (fstat(temp_fd, &temp_sb)) {
|
---|
| 696 | serrno = errno;
|
---|
| 697 | (void)unlink(tempfile);
|
---|
| 698 | errno = serrno;
|
---|
[3192] | 699 | rc = err(pThis->pCtx, EX_OSERR, "%s", tempfile);
|
---|
[370] | 700 | goto l_done;
|
---|
[367] | 701 | }
|
---|
| 702 |
|
---|
[3115] | 703 | if (compare(temp_fd, (size_t)temp_sb.st_size,
|
---|
| 704 | to_fd, (size_t)to_sb.st_size) == 0) {
|
---|
[367] | 705 | /*
|
---|
| 706 | * If target has more than one link we need to
|
---|
| 707 | * replace it in order to snap the extra links.
|
---|
| 708 | * Need to preserve target file times, though.
|
---|
| 709 | */
|
---|
[370] | 710 | #if !defined(_MSC_VER) && !defined(__EMX__)
|
---|
[367] | 711 | if (to_sb.st_nlink != 1) {
|
---|
| 712 | tvb[0].tv_sec = to_sb.st_atime;
|
---|
| 713 | tvb[0].tv_usec = 0;
|
---|
| 714 | tvb[1].tv_sec = to_sb.st_mtime;
|
---|
| 715 | tvb[1].tv_usec = 0;
|
---|
| 716 | (void)utimes(tempfile, tvb);
|
---|
[374] | 717 | } else
|
---|
[370] | 718 | #endif
|
---|
| 719 | {
|
---|
| 720 |
|
---|
[367] | 721 | files_match = 1;
|
---|
| 722 | (void)unlink(tempfile);
|
---|
| 723 | }
|
---|
| 724 | (void) close(temp_fd);
|
---|
[370] | 725 | temp_fd = -1;
|
---|
[367] | 726 | }
|
---|
| 727 | }
|
---|
| 728 |
|
---|
| 729 | /*
|
---|
| 730 | * Move the new file into place if doing a safe copy
|
---|
| 731 | * and the files are different (or just not compared).
|
---|
| 732 | */
|
---|
| 733 | if (tempcopy && !files_match) {
|
---|
[368] | 734 | #ifdef UF_IMMUTABLE
|
---|
[367] | 735 | /* Try to turn off the immutable bits. */
|
---|
| 736 | if (to_sb.st_flags & NOCHANGEBITS)
|
---|
| 737 | (void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
|
---|
[368] | 738 | #endif
|
---|
[3192] | 739 | if (pThis->dobackup) {
|
---|
| 740 | if ( (size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name, pThis->suffix)
|
---|
| 741 | != strlen(to_name) + strlen(pThis->suffix)) {
|
---|
[367] | 742 | unlink(tempfile);
|
---|
[3192] | 743 | rc = errx(pThis->pCtx, EX_OSERR, "%s: backup filename too long",
|
---|
[370] | 744 | to_name);
|
---|
| 745 | goto l_done;
|
---|
[367] | 746 | }
|
---|
[3192] | 747 | if (pThis->verbose)
|
---|
| 748 | kmk_builtin_ctx_printf(pThis->pCtx, 0, "install: %s -> %s\n", to_name, backup);
|
---|
[367] | 749 | if (rename(to_name, backup) < 0) {
|
---|
| 750 | serrno = errno;
|
---|
| 751 | unlink(tempfile);
|
---|
| 752 | errno = serrno;
|
---|
[3192] | 753 | rc = err(pThis->pCtx, EX_OSERR, "rename: %s to %s", to_name,
|
---|
[370] | 754 | backup);
|
---|
| 755 | goto l_done;
|
---|
[367] | 756 | }
|
---|
| 757 | }
|
---|
[3192] | 758 | if (pThis->verbose)
|
---|
| 759 | kmk_builtin_ctx_printf(pThis->pCtx, 0, "install: %s -> %s\n", from_name, to_name);
|
---|
[367] | 760 | if (rename(tempfile, to_name) < 0) {
|
---|
| 761 | serrno = errno;
|
---|
| 762 | unlink(tempfile);
|
---|
| 763 | errno = serrno;
|
---|
[3192] | 764 | rc = err(pThis->pCtx, EX_OSERR, "rename: %s to %s", tempfile, to_name);
|
---|
[370] | 765 | goto l_done;
|
---|
[367] | 766 | }
|
---|
| 767 |
|
---|
| 768 | /* Re-open to_fd so we aren't hosed by the rename(2). */
|
---|
| 769 | (void) close(to_fd);
|
---|
[3219] | 770 | if ((to_fd = open(to_name, O_RDONLY | O_BINARY | KMK_OPEN_NO_INHERIT, 0)) < 0) {
|
---|
[3192] | 771 | rc = err(pThis->pCtx, EX_OSERR, "%s", to_name);
|
---|
[370] | 772 | goto l_done;
|
---|
| 773 | }
|
---|
[367] | 774 | }
|
---|
| 775 |
|
---|
| 776 | /*
|
---|
| 777 | * Preserve the timestamp of the source file if necessary.
|
---|
| 778 | */
|
---|
[3192] | 779 | if (pThis->dopreserve && !files_match && !devnull) {
|
---|
[367] | 780 | tvb[0].tv_sec = from_sb.st_atime;
|
---|
| 781 | tvb[0].tv_usec = 0;
|
---|
| 782 | tvb[1].tv_sec = from_sb.st_mtime;
|
---|
| 783 | tvb[1].tv_usec = 0;
|
---|
| 784 | (void)utimes(to_name, tvb);
|
---|
| 785 | }
|
---|
| 786 |
|
---|
| 787 | if (fstat(to_fd, &to_sb) == -1) {
|
---|
| 788 | serrno = errno;
|
---|
| 789 | (void)unlink(to_name);
|
---|
| 790 | errno = serrno;
|
---|
[3192] | 791 | rc = err(pThis->pCtx, EX_OSERR, "%s", to_name);
|
---|
[370] | 792 | goto l_done;
|
---|
[367] | 793 | }
|
---|
| 794 |
|
---|
| 795 | /*
|
---|
| 796 | * Set owner, group, mode for target; do the chown first,
|
---|
| 797 | * chown may lose the setuid bits.
|
---|
| 798 | */
|
---|
[368] | 799 | #ifdef UF_IMMUTABLE
|
---|
[3227] | 800 | if ((pThis->gid != (gid_t)-1 && pThis->gid != to_sb.st_gid) ||
|
---|
| 801 | (pThis->uid != (uid_t)-1 && pThis->uid != to_sb.st_uid) ||
|
---|
[3226] | 802 | (pThis->mode != (to_sb.st_mode & ALLPERMS))) {
|
---|
[367] | 803 | /* Try to turn off the immutable bits. */
|
---|
| 804 | if (to_sb.st_flags & NOCHANGEBITS)
|
---|
| 805 | (void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
|
---|
| 806 | }
|
---|
[368] | 807 | #endif
|
---|
[367] | 808 |
|
---|
[3192] | 809 | if ((pThis->gid != (gid_t)-1 && pThis->gid != to_sb.st_gid) ||
|
---|
| 810 | (pThis->uid != (uid_t)-1 && pThis->uid != to_sb.st_uid))
|
---|
| 811 | if (fchown(to_fd, pThis->uid, pThis->gid) == -1) {
|
---|
| 812 | if (errno == EPERM && pThis->ignore_perm_errors) {
|
---|
| 813 | warn(pThis->pCtx, "%s: ignoring chown uid=%d gid=%d failure",
|
---|
| 814 | to_name, (int)pThis->uid, (int)pThis->gid);
|
---|
[1730] | 815 | } else {
|
---|
| 816 | serrno = errno;
|
---|
| 817 | (void)unlink(to_name);
|
---|
| 818 | errno = serrno;
|
---|
[3192] | 819 | rc = err(pThis->pCtx, EX_OSERR,"%s: chown/chgrp", to_name);
|
---|
[1730] | 820 | goto l_done;
|
---|
| 821 | }
|
---|
[367] | 822 | }
|
---|
| 823 |
|
---|
[3192] | 824 | if (pThis->mode != (to_sb.st_mode & ALLPERMS))
|
---|
| 825 | if (fchmod(to_fd, pThis->mode)) {
|
---|
[367] | 826 | serrno = errno;
|
---|
[3192] | 827 | if (serrno == EPERM && pThis->ignore_perm_errors) {
|
---|
| 828 | fchmod(to_fd, pThis->mode & (ALLPERMS & ~0007000));
|
---|
[1730] | 829 | errno = errno;
|
---|
[3192] | 830 | warn(pThis->pCtx, "%s: ignoring chmod 0%o failure", to_name, (int)(pThis->mode & ALLPERMS));
|
---|
[1730] | 831 | } else {
|
---|
| 832 | serrno = errno;
|
---|
| 833 | (void)unlink(to_name);
|
---|
| 834 | errno = serrno;
|
---|
[3192] | 835 | rc = err(pThis->pCtx, EX_OSERR, "%s: chmod", to_name);
|
---|
[1730] | 836 | goto l_done;
|
---|
| 837 | }
|
---|
[367] | 838 | }
|
---|
| 839 |
|
---|
| 840 | /*
|
---|
| 841 | * If provided a set of flags, set them, otherwise, preserve the
|
---|
| 842 | * flags, except for the dump flag.
|
---|
| 843 | * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
|
---|
| 844 | * trying to turn off UF_NODUMP. If we're trying to set real flags,
|
---|
| 845 | * then warn if the the fs doesn't support it, otherwise fail.
|
---|
| 846 | */
|
---|
[368] | 847 | #ifdef UF_IMMUTABLE
|
---|
[3192] | 848 | if ( !devnull
|
---|
| 849 | && (flags & SETFLAGS || (from_sb.st_flags & ~UF_NODUMP) != to_sb.st_flags)
|
---|
| 850 | && fchflags(to_fd, flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
|
---|
[367] | 851 | if (flags & SETFLAGS) {
|
---|
| 852 | if (errno == EOPNOTSUPP)
|
---|
[3192] | 853 | warn(pThis->pCtx, "%s: chflags", to_name);
|
---|
[367] | 854 | else {
|
---|
| 855 | serrno = errno;
|
---|
| 856 | (void)unlink(to_name);
|
---|
| 857 | errno = serrno;
|
---|
[3192] | 858 | rc = err(pThis->pCtx, EX_OSERR, "%s: chflags", to_name);
|
---|
[370] | 859 | goto l_done;
|
---|
[367] | 860 | }
|
---|
| 861 | }
|
---|
| 862 | }
|
---|
[368] | 863 | #endif
|
---|
[367] | 864 |
|
---|
[370] | 865 | l_done:
|
---|
| 866 | if (to_fd >= 0)
|
---|
| 867 | (void)close(to_fd);
|
---|
| 868 | if (temp_fd >= 0)
|
---|
| 869 | (void)close(temp_fd);
|
---|
[2486] | 870 | if (from_fd >= 0 && !devnull)
|
---|
[367] | 871 | (void)close(from_fd);
|
---|
[370] | 872 | return rc;
|
---|
[367] | 873 | }
|
---|
| 874 |
|
---|
| 875 | /*
|
---|
| 876 | * compare --
|
---|
| 877 | * compare two files; non-zero means files differ
|
---|
| 878 | */
|
---|
[370] | 879 | static int
|
---|
[3115] | 880 | compare(int from_fd, size_t from_len, int to_fd, size_t to_len)
|
---|
[367] | 881 | {
|
---|
[3112] | 882 | char buf1[MAXBSIZE];
|
---|
| 883 | char buf2[MAXBSIZE];
|
---|
| 884 | int n1, n2;
|
---|
[367] | 885 | int rv;
|
---|
| 886 |
|
---|
| 887 | if (from_len != to_len)
|
---|
| 888 | return 1;
|
---|
| 889 |
|
---|
| 890 | if (from_len <= MAX_CMP_SIZE) {
|
---|
[3112] | 891 | rv = 0;
|
---|
| 892 | lseek(from_fd, 0, SEEK_SET);
|
---|
| 893 | lseek(to_fd, 0, SEEK_SET);
|
---|
| 894 | while (rv == 0) {
|
---|
| 895 | n1 = read(from_fd, buf1, sizeof(buf1));
|
---|
| 896 | if (n1 == 0)
|
---|
| 897 | break; /* EOF */
|
---|
| 898 | else if (n1 > 0) {
|
---|
| 899 | n2 = read(to_fd, buf2, n1);
|
---|
| 900 | if (n2 == n1)
|
---|
| 901 | rv = memcmp(buf1, buf2, n1);
|
---|
| 902 | else
|
---|
| 903 | rv = 1; /* out of sync */
|
---|
| 904 | } else
|
---|
| 905 | rv = 1; /* read failure */
|
---|
[367] | 906 | }
|
---|
[3112] | 907 | lseek(from_fd, 0, SEEK_SET);
|
---|
| 908 | lseek(to_fd, 0, SEEK_SET);
|
---|
[367] | 909 | } else
|
---|
| 910 | rv = 1; /* don't bother in this case */
|
---|
| 911 |
|
---|
| 912 | return rv;
|
---|
| 913 | }
|
---|
| 914 |
|
---|
| 915 | /*
|
---|
| 916 | * create_tempfile --
|
---|
| 917 | * create a temporary file based on path and open it
|
---|
| 918 | */
|
---|
| 919 | int
|
---|
| 920 | create_tempfile(const char *path, char *temp, size_t tsize)
|
---|
| 921 | {
|
---|
[3241] | 922 | static char s_szTemplate[] = "INS@XXXXXX";
|
---|
| 923 | const char *p = last_slash(path);
|
---|
| 924 | if (p) {
|
---|
| 925 | size_t cchDir = ++p - path;
|
---|
| 926 | if (cchDir + sizeof(s_szTemplate) <= tsize) {
|
---|
| 927 | memcpy(temp, path, cchDir);
|
---|
| 928 | memcpy(&temp[cchDir], s_szTemplate, sizeof(s_szTemplate));
|
---|
| 929 | } else
|
---|
| 930 | return EOVERFLOW;
|
---|
| 931 | } else if (tsize >= sizeof(s_szTemplate))
|
---|
| 932 | memcpy(temp, s_szTemplate, sizeof(s_szTemplate));
|
---|
| 933 | else
|
---|
| 934 | return EOVERFLOW;
|
---|
[367] | 935 |
|
---|
| 936 | return (mkstemp(temp));
|
---|
| 937 | }
|
---|
| 938 |
|
---|
| 939 | /*
|
---|
| 940 | * create_newfile --
|
---|
| 941 | * create a new file, overwriting an existing one if necessary
|
---|
| 942 | */
|
---|
| 943 | int
|
---|
[3192] | 944 | create_newfile(PINSTALLINSTANCE pThis, const char *path, int target, struct stat *sbp)
|
---|
[367] | 945 | {
|
---|
| 946 | char backup[MAXPATHLEN];
|
---|
| 947 | int saved_errno = 0;
|
---|
| 948 | int newfd;
|
---|
| 949 |
|
---|
| 950 | if (target) {
|
---|
| 951 | /*
|
---|
| 952 | * Unlink now... avoid ETXTBSY errors later. Try to turn
|
---|
| 953 | * off the append/immutable bits -- if we fail, go ahead,
|
---|
| 954 | * it might work.
|
---|
| 955 | */
|
---|
[368] | 956 | #ifdef UF_IMMUTABLE
|
---|
[367] | 957 | if (sbp->st_flags & NOCHANGEBITS)
|
---|
| 958 | (void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
|
---|
[368] | 959 | #endif
|
---|
[367] | 960 |
|
---|
[3192] | 961 | if (pThis->dobackup) {
|
---|
| 962 | if ( (size_t)snprintf(backup, MAXPATHLEN, "%s%s", path, pThis->suffix)
|
---|
| 963 | != strlen(path) + strlen(pThis->suffix)) {
|
---|
| 964 | errx(pThis->pCtx, EX_OSERR, "%s: backup filename too long", path);
|
---|
[370] | 965 | errno = ENAMETOOLONG;
|
---|
| 966 | return -1;
|
---|
| 967 | }
|
---|
[3192] | 968 | (void)snprintf(backup, MAXPATHLEN, "%s%s", path, pThis->suffix);
|
---|
| 969 | if (pThis->verbose)
|
---|
| 970 | kmk_builtin_ctx_printf(pThis->pCtx, 0, "install: %s -> %s\n", path, backup);
|
---|
[370] | 971 | if (rename(path, backup) < 0) {
|
---|
[3192] | 972 | err(pThis->pCtx, EX_OSERR, "rename: %s to %s", path, backup);
|
---|
[370] | 973 | return -1;
|
---|
| 974 | }
|
---|
[367] | 975 | } else
|
---|
| 976 | if (unlink(path) < 0)
|
---|
| 977 | saved_errno = errno;
|
---|
| 978 | }
|
---|
| 979 |
|
---|
[3219] | 980 | newfd = open(path, O_CREAT | O_RDWR | O_TRUNC | O_BINARY | KMK_OPEN_NO_INHERIT, S_IRUSR | S_IWUSR);
|
---|
[367] | 981 | if (newfd < 0 && saved_errno != 0)
|
---|
| 982 | errno = saved_errno;
|
---|
| 983 | return newfd;
|
---|
| 984 | }
|
---|
| 985 |
|
---|
| 986 | /*
|
---|
[3114] | 987 | * Write error handler.
|
---|
| 988 | */
|
---|
[3192] | 989 | static int write_error(PINSTALLINSTANCE pThis, int *ptr_to_fd, const char *to_name, int nw)
|
---|
[3114] | 990 | {
|
---|
| 991 | int serrno = errno;
|
---|
| 992 | (void)close(*ptr_to_fd);
|
---|
| 993 | *ptr_to_fd = -1;
|
---|
| 994 | (void)unlink(to_name);
|
---|
| 995 | errno = nw > 0 ? EIO : serrno;
|
---|
[3192] | 996 | return err(pThis->pCtx, EX_OSERR, "%s", to_name);
|
---|
[3114] | 997 | }
|
---|
| 998 |
|
---|
| 999 | /*
|
---|
| 1000 | * Read error handler.
|
---|
| 1001 | */
|
---|
[3192] | 1002 | static int read_error(PINSTALLINSTANCE pThis, const char *from_name, int *ptr_to_fd, const char *to_name)
|
---|
[3114] | 1003 | {
|
---|
| 1004 | int serrno = errno;
|
---|
| 1005 | (void)close(*ptr_to_fd);
|
---|
| 1006 | *ptr_to_fd = -1;
|
---|
| 1007 | (void)unlink(to_name);
|
---|
| 1008 | errno = serrno;
|
---|
[3192] | 1009 | return err(pThis->pCtx, EX_OSERR, "%s", from_name);
|
---|
[3114] | 1010 | }
|
---|
| 1011 |
|
---|
| 1012 | /*
|
---|
[367] | 1013 | * copy --
|
---|
| 1014 | * copy from one file to another
|
---|
| 1015 | */
|
---|
[370] | 1016 | static int
|
---|
[3192] | 1017 | copy(PINSTALLINSTANCE pThis, int from_fd, const char *from_name, int *ptr_to_fd, const char *to_name)
|
---|
[367] | 1018 | {
|
---|
[3114] | 1019 | KBOOL fPendingCr = K_FALSE;
|
---|
| 1020 | KSIZE cchDst;
|
---|
[367] | 1021 | int nr, nw;
|
---|
[3112] | 1022 | char buf[MAXBSIZE];
|
---|
[3114] | 1023 | int to_fd = *ptr_to_fd;
|
---|
[367] | 1024 |
|
---|
| 1025 | /* Rewind file descriptors. */
|
---|
| 1026 | if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
|
---|
[3192] | 1027 | return err(pThis->pCtx, EX_OSERR, "lseek: %s", from_name);
|
---|
[367] | 1028 | if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
|
---|
[3192] | 1029 | return err(pThis->pCtx, EX_OSERR, "lseek: %s", to_name);
|
---|
[367] | 1030 |
|
---|
[3192] | 1031 | if (pThis->dos2unix == 0) {
|
---|
[3114] | 1032 | /*
|
---|
| 1033 | * Copy bytes, no conversion.
|
---|
| 1034 | */
|
---|
| 1035 | while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
|
---|
| 1036 | if ((nw = write(to_fd, buf, nr)) != nr)
|
---|
[3192] | 1037 | return write_error(pThis, ptr_to_fd, to_name, nw);
|
---|
| 1038 | } else if (pThis->dos2unix > 0) {
|
---|
[3114] | 1039 | /*
|
---|
| 1040 | * CRLF -> LF is a reduction, so we can work with full buffers.
|
---|
| 1041 | */
|
---|
| 1042 | while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
|
---|
| 1043 | if ( fPendingCr
|
---|
| 1044 | && buf[0] != '\n'
|
---|
| 1045 | && (nw = write(to_fd, "\r", 1)) != 1)
|
---|
[3192] | 1046 | return write_error(pThis, ptr_to_fd, to_name, nw);
|
---|
[3114] | 1047 |
|
---|
| 1048 | fPendingCr = dos2unix_convert_to_unix(buf, nr, buf, &cchDst);
|
---|
| 1049 |
|
---|
| 1050 | nw = write(to_fd, buf, cchDst);
|
---|
| 1051 | if (nw != (int)cchDst)
|
---|
[3192] | 1052 | return write_error(pThis, ptr_to_fd, to_name, nw);
|
---|
[367] | 1053 | }
|
---|
[3114] | 1054 | } else {
|
---|
| 1055 | /*
|
---|
| 1056 | * LF -> CRLF is an expansion, so we work with half buffers, reading
|
---|
| 1057 | * into the upper half of the buffer and expanding into the full buffer.
|
---|
| 1058 | * The conversion will never expand to more than the double size.
|
---|
| 1059 | *
|
---|
| 1060 | * Note! We do not convert valid CRLF line endings. This gives us
|
---|
| 1061 | * valid DOS text, but no round-trip conversion.
|
---|
| 1062 | */
|
---|
| 1063 | char * const pchSrc = &buf[sizeof(buf) / 2];
|
---|
| 1064 | while ((nr = read(from_fd, pchSrc, sizeof(buf) / 2)) > 0) {
|
---|
| 1065 | if ( fPendingCr
|
---|
| 1066 | && pchSrc[0] != '\n'
|
---|
| 1067 | && (nw = write(to_fd, "\r", 1))!= 1)
|
---|
[3192] | 1068 | return write_error(pThis, ptr_to_fd, to_name, nw);
|
---|
[3114] | 1069 |
|
---|
| 1070 | fPendingCr = dos2unix_convert_to_dos(pchSrc, nr, buf, &cchDst);
|
---|
| 1071 |
|
---|
| 1072 | nw = write(to_fd, buf, cchDst);
|
---|
| 1073 | if (nw != (int)cchDst)
|
---|
[3192] | 1074 | return write_error(pThis, ptr_to_fd, to_name, nw);
|
---|
[3114] | 1075 | }
|
---|
[367] | 1076 | }
|
---|
[3114] | 1077 |
|
---|
| 1078 | /* Check for read error. */
|
---|
| 1079 | if (nr != 0)
|
---|
[3192] | 1080 | return read_error(pThis, from_name, ptr_to_fd, to_name);
|
---|
[3114] | 1081 |
|
---|
| 1082 | /* When converting, we might have a pending final CR to write. */
|
---|
[3192] | 1083 | if ( fPendingCr
|
---|
| 1084 | && (nw = write(to_fd, "\r", 1))!= 1)
|
---|
| 1085 | return write_error(pThis, ptr_to_fd, to_name, nw);
|
---|
[3114] | 1086 |
|
---|
| 1087 | return EX_OK;
|
---|
[367] | 1088 | }
|
---|
| 1089 |
|
---|
| 1090 | /*
|
---|
| 1091 | * strip --
|
---|
| 1092 | * use strip(1) to strip the target file
|
---|
| 1093 | */
|
---|
[370] | 1094 | static int
|
---|
[3192] | 1095 | strip(PINSTALLINSTANCE pThis, const char *to_name)
|
---|
[367] | 1096 | {
|
---|
[370] | 1097 | #if defined(__EMX__) || defined(_MSC_VER)
|
---|
| 1098 | const char *stripbin = getenv("STRIPBIN");
|
---|
| 1099 | if (stripbin == NULL)
|
---|
| 1100 | stripbin = "strip";
|
---|
[3192] | 1101 | (void)pThis;
|
---|
[370] | 1102 | return spawnlp(P_WAIT, stripbin, stripbin, to_name, NULL);
|
---|
| 1103 | #else
|
---|
[367] | 1104 | const char *stripbin;
|
---|
| 1105 | int serrno, status;
|
---|
[758] | 1106 | pid_t pid;
|
---|
[367] | 1107 |
|
---|
[758] | 1108 | pid = fork();
|
---|
| 1109 | switch (pid) {
|
---|
[367] | 1110 | case -1:
|
---|
| 1111 | serrno = errno;
|
---|
| 1112 | (void)unlink(to_name);
|
---|
| 1113 | errno = serrno;
|
---|
[3192] | 1114 | return err(pThis->pCtx, EX_TEMPFAIL, "fork");
|
---|
[367] | 1115 | case 0:
|
---|
| 1116 | stripbin = getenv("STRIPBIN");
|
---|
| 1117 | if (stripbin == NULL)
|
---|
| 1118 | stripbin = "strip";
|
---|
| 1119 | execlp(stripbin, stripbin, to_name, (char *)NULL);
|
---|
[3192] | 1120 | err(pThis->pCtx, EX_OSERR, "exec(%s)", stripbin);
|
---|
[1539] | 1121 | exit(EX_OSERR);
|
---|
[367] | 1122 | default:
|
---|
[758] | 1123 | if (waitpid(pid, &status, 0) == -1 || status) {
|
---|
[367] | 1124 | serrno = errno;
|
---|
| 1125 | (void)unlink(to_name);
|
---|
[371] | 1126 | errno = serrno;
|
---|
[3192] | 1127 | return err(pThis->pCtx, EX_SOFTWARE, "waitpid");
|
---|
[367] | 1128 | /* NOTREACHED */
|
---|
| 1129 | }
|
---|
| 1130 | }
|
---|
[1538] | 1131 | return 0;
|
---|
[374] | 1132 | #endif
|
---|
[367] | 1133 | }
|
---|
| 1134 |
|
---|
| 1135 | /*
|
---|
| 1136 | * install_dir --
|
---|
| 1137 | * build directory heirarchy
|
---|
| 1138 | */
|
---|
[370] | 1139 | static int
|
---|
[3192] | 1140 | install_dir(PINSTALLINSTANCE pThis, char *path)
|
---|
[367] | 1141 | {
|
---|
| 1142 | char *p;
|
---|
| 1143 | struct stat sb;
|
---|
| 1144 | int ch;
|
---|
| 1145 |
|
---|
| 1146 | for (p = path;; ++p)
|
---|
[2678] | 1147 | if ( !*p
|
---|
| 1148 | || ( p != path
|
---|
| 1149 | && IS_SLASH(*p)
|
---|
| 1150 | #if defined(_MSC_VER) /* stat("C:") fails (VC++ v10). Just skip it since it's unnecessary. */
|
---|
| 1151 | && (p - path != 2 || p[-1] != ':')
|
---|
| 1152 | #endif
|
---|
| 1153 | )) {
|
---|
[367] | 1154 | ch = *p;
|
---|
| 1155 | *p = '\0';
|
---|
| 1156 | if (stat(path, &sb)) {
|
---|
| 1157 | if (errno != ENOENT || mkdir(path, 0755) < 0) {
|
---|
[3192] | 1158 | return err(pThis->pCtx, EX_OSERR, "mkdir %s", path);
|
---|
[367] | 1159 | /* NOTREACHED */
|
---|
[3192] | 1160 | } else if (pThis->verbose)
|
---|
| 1161 | kmk_builtin_ctx_printf(pThis->pCtx, 0, "install: mkdir %s\n", path);
|
---|
[367] | 1162 | } else if (!S_ISDIR(sb.st_mode))
|
---|
[3192] | 1163 | return errx(pThis->pCtx, EX_OSERR, "%s exists but is not a directory", path);
|
---|
[367] | 1164 | if (!(*p = ch))
|
---|
| 1165 | break;
|
---|
| 1166 | }
|
---|
| 1167 |
|
---|
[3192] | 1168 | if ((pThis->gid != (gid_t)-1 || pThis->uid != (uid_t)-1) && chown(path, pThis->uid, pThis->gid))
|
---|
| 1169 | warn(pThis->pCtx, "chown %u:%u %s", pThis->uid, pThis->gid, path);
|
---|
| 1170 | if (chmod(path, pThis->mode))
|
---|
| 1171 | warn(pThis->pCtx, "chmod %o %s", pThis->mode, path);
|
---|
[370] | 1172 | return EX_OK;
|
---|
[367] | 1173 | }
|
---|
| 1174 |
|
---|
| 1175 | /*
|
---|
| 1176 | * usage --
|
---|
| 1177 | * print a usage message and die
|
---|
| 1178 | */
|
---|
[374] | 1179 | static int
|
---|
[3192] | 1180 | usage(PKMKBUILTINCTX pCtx, int fIsErr)
|
---|
[367] | 1181 | {
|
---|
[3192] | 1182 | kmk_builtin_ctx_printf(pCtx, fIsErr,
|
---|
[2476] | 1183 | "usage: %s [-bCcpSsv] [--[no-]hard-link-files-when-possible]\n"
|
---|
[3583] | 1184 | " [--verbose-hard-link-refusal] [--verbose-hard-link-mode-mismatch]\n"
|
---|
| 1185 | " [--[no-]ignore-perm-errors] [-B suffix] [-f flags] [-g group]\n"
|
---|
| 1186 | " [-m mode] [-o owner] [--dos2unix|--unix2dos] file1 file2\n"
|
---|
[1730] | 1187 | " or: %s [-bCcpSsv] [--[no-]ignore-perm-errors] [-B suffix] [-f flags]\n"
|
---|
[3583] | 1188 | " [-g group] [-m mode] [-o owner] file1 ... fileN directory\n"
|
---|
[1183] | 1189 | " or: %s -d [-v] [-g group] [-m mode] [-o owner] directory ...\n"
|
---|
| 1190 | " or: %s --help\n"
|
---|
| 1191 | " or: %s --version\n",
|
---|
[3192] | 1192 | pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName,
|
---|
| 1193 | pCtx->pszProgName, pCtx->pszProgName);
|
---|
[370] | 1194 | return EX_USAGE;
|
---|
[367] | 1195 | }
|
---|
| 1196 |
|
---|
[1194] | 1197 | /* figures out where the last slash or colon is. */
|
---|
| 1198 | static char *
|
---|
| 1199 | last_slash(const char *path)
|
---|
| 1200 | {
|
---|
| 1201 | #if defined(__WIN32__) || defined(__WIN64__) || defined(__OS2__)
|
---|
| 1202 | char *p = (char *)strrchr(path, '/');
|
---|
| 1203 | if (p)
|
---|
| 1204 | {
|
---|
| 1205 | char *p2 = strrchr(p, '\\');
|
---|
| 1206 | if (p2)
|
---|
| 1207 | p = p2;
|
---|
| 1208 | }
|
---|
| 1209 | else
|
---|
| 1210 | {
|
---|
| 1211 | p = (char *)strrchr(path, '\\');
|
---|
| 1212 | if (!p && isalpha(path[0]) && path[1] == ':')
|
---|
| 1213 | p = (char *)&path[1];
|
---|
| 1214 | }
|
---|
| 1215 | return p;
|
---|
| 1216 | #else
|
---|
| 1217 | return strrchr(path, '/');
|
---|
| 1218 | #endif
|
---|
| 1219 | }
|
---|
| 1220 |
|
---|
[3114] | 1221 | /**
|
---|
| 1222 | * Checks if @a pszFilename actually needs dos2unix conversion.
|
---|
| 1223 | *
|
---|
| 1224 | * @returns boolean.
|
---|
| 1225 | * @param pszFilename The name of the file to check.
|
---|
| 1226 | */
|
---|
| 1227 | static KBOOL needs_dos2unix_conversion(const char *pszFilename)
|
---|
| 1228 | {
|
---|
| 1229 | KU32 fStyle = 0;
|
---|
| 1230 | int iErr = dos2unix_analyze_file(pszFilename, &fStyle, NULL, NULL);
|
---|
| 1231 | return iErr != 0
|
---|
| 1232 | || (fStyle & (DOS2UNIX_STYLE_MASK | DOS2UNIX_F_BINARY)) != DOS2UNIX_STYLE_UNIX;
|
---|
| 1233 | }
|
---|
| 1234 |
|
---|
| 1235 | /**
|
---|
| 1236 | * Checks if @a pszFilename actually needs unix2dos conversion.
|
---|
| 1237 | *
|
---|
| 1238 | * @returns boolean.
|
---|
| 1239 | * @param pszFilename The name of the file to check.
|
---|
| 1240 | */
|
---|
| 1241 | static KBOOL needs_unix2dos_conversion(const char *pszFilename)
|
---|
| 1242 | {
|
---|
| 1243 | KU32 fStyle = 0;
|
---|
| 1244 | int iErr = dos2unix_analyze_file(pszFilename, &fStyle, NULL, NULL);
|
---|
| 1245 | return iErr != 0
|
---|
| 1246 | || (fStyle & (DOS2UNIX_STYLE_MASK | DOS2UNIX_F_BINARY)) != DOS2UNIX_STYLE_DOS;
|
---|
| 1247 | }
|
---|
| 1248 |
|
---|