VirtualBox

Changeset 1627 in kBuild


Ignore:
Timestamp:
May 12, 2008 11:23:21 PM (17 years ago)
Author:
bird
Message:

Enabled recursive copying w/ protection a la rm.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/kmkbuiltin/cp.c

    r1597 r1627  
    7979#include "cp_extern.h"
    8080#include "kmkbuiltin.h"
     81#include "kbuild_protection.h"
    8182
    8283
     
    112113/* have wrappers for globals in cp_extern! */
    113114
     115static KBUILDPROTECTION g_ProtData;
    114116const char *cp_argv0;
    115117static char emptystring[] = "";
     
    141143    { "ignore-non-existing",                            no_argument, 0, CP_OPT_IGNORE_NON_EXISTING },
    142144    { "changed",                                        no_argument, 0, CP_OPT_CHANGED },
    143 /*
    144145    { "disable-protection",                             no_argument, 0, CP_OPT_DISABLE_PROTECTION },
    145146    { "enable-protection",                              no_argument, 0, CP_OPT_ENABLE_PROTECTION },
     
    147148    { "disable-full-protection",                        no_argument, 0, CP_OPT_DISABLE_FULL_PROTECTION },
    148149    { "protection-depth",                               required_argument, 0, CP_OPT_PROTECTION_DEPTH },
    149 */
    150150    { 0, 0,     0, 0 },
    151151};
     
    153153
    154154static int copy(char *[], enum op, int);
    155 static int mastercmp(const FTSENT * const *, const FTSENT * const *);
     155static int mastercmp(const FTSENT **, const FTSENT **);
    156156#ifdef SIGINFO
    157157static void siginfo(int __unused);
     
    164164        struct stat to_stat, tmp_stat;
    165165        enum op type;
    166         int Hflag, Lflag, Pflag, ch, fts_options, r, have_trailing_slash;
     166        int Hflag, Lflag, Pflag, ch, fts_options, r, have_trailing_slash, rc;
    167167        char *target;
    168168
     
    175175        info = 0;
    176176        cp_ignore_non_existing = cp_changed_only = 0;
     177        kBuildProtectionInit(&g_ProtData);
    177178
    178179        /* reset getopt and set progname. */
     
    199200                        break;
    200201                case 'R':
    201 #ifdef DO_CP_TREE
    202202                        Rflag = 1;
    203203                        break;
    204 #else
    205                         return errx(1, "recursive copy is not implemented!");
    206 #endif
    207204                case 'f':
    208205                        fflag = 1;
     
    220217                        pflag = 1;
    221218                        break;
     219#if 0 /* only one -R */
    222220                case 'r':
    223 #ifdef DO_CP_TREE
    224221                        rflag = 1;
    225222                        break;
    226 #else
    227                         return errx(1, "recursive copy is not implemented!");
    228223#endif
    229224                case 'v':
     
    232227                case CP_OPT_HELP:
    233228                        usage(stdout);
     229                        kBuildProtectionTerm(&g_ProtData);
    234230                        return 0;
    235231                case CP_OPT_VERSION:
     232                        kBuildProtectionTerm(&g_ProtData);
    236233                        return kbuild_version(argv[0]);
    237234                case CP_OPT_IGNORE_NON_EXISTING:
     
    241238                        cp_changed_only = 1;
    242239                        break;
     240                case CP_OPT_DISABLE_PROTECTION:
     241                        kBuildProtectionDisable(&g_ProtData, KBUILDPROTECTIONTYPE_RECURSIVE);
     242                        break;
     243                case CP_OPT_ENABLE_PROTECTION:
     244                        kBuildProtectionEnable(&g_ProtData, KBUILDPROTECTIONTYPE_RECURSIVE);
     245                        break;
     246                case CP_OPT_ENABLE_FULL_PROTECTION:
     247                        kBuildProtectionEnable(&g_ProtData, KBUILDPROTECTIONTYPE_FULL);
     248                        break;
     249                case CP_OPT_DISABLE_FULL_PROTECTION:
     250                        kBuildProtectionDisable(&g_ProtData, KBUILDPROTECTIONTYPE_FULL);
     251                        break;
     252                case CP_OPT_PROTECTION_DEPTH:
     253                        if (kBuildProtectionSetDepth(&g_ProtData, optarg)) {
     254                                kBuildProtectionTerm(&g_ProtData);
     255                                return 1;
     256                        }
     257                        break;
    243258                default:
     259                        kBuildProtectionTerm(&g_ProtData);
    244260                        return usage(stderr);
    245261                }
     
    247263        argv += optind;
    248264
    249         if (argc < 2)
     265        if (argc < 2) {
     266                kBuildProtectionTerm(&g_ProtData);
    250267                return usage(stderr);
     268        }
    251269
    252270        fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
    253271        if (rflag) {
    254                 if (Rflag)
     272                if (Rflag) {
     273                        kBuildProtectionTerm(&g_ProtData);
    255274                        return errx(1,
    256275                    "the -R and -r options may not be specified together.");
     276                }
    257277                if (Hflag || Lflag || Pflag)
    258278                        errx(1,
    259279        "the -H, -L, and -P options may not be specified with the -r option.");
    260 #ifdef DO_CP_TREE
    261280                fts_options &= ~FTS_PHYSICAL;
    262281                fts_options |= FTS_LOGICAL;
    263 #endif
    264         }
    265 #ifdef DO_CP_TREE
     282        }
    266283        if (Rflag) {
    267284                if (Hflag)
     
    271288                        fts_options |= FTS_LOGICAL;
    272289                }
    273         } else
    274 #endif
    275         {
     290        } else {
    276291                fts_options &= ~FTS_PHYSICAL;
    277292                fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
     
    283298        /* Save the target base in "to". */
    284299        target = argv[--argc];
    285         if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path))
     300        if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path)) {
     301                kBuildProtectionTerm(&g_ProtData);
    286302                return errx(1, "%s: name too long", target);
     303        }
    287304        to.p_end = to.p_path + strlen(to.p_path);
    288305        if (to.p_path == to.p_end) {
     
    313330         */
    314331        r = stat(to.p_path, &to_stat);
    315         if (r == -1 && errno != ENOENT)
     332        if (r == -1 && errno != ENOENT) {
     333                kBuildProtectionTerm(&g_ProtData);
    316334                return err(1, "%s", to.p_path);
     335        }
    317336        if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
    318337                /*
    319338                 * Case (1).  Target is not a directory.
    320339                 */
    321                 if (argc > 1)
     340                if (argc > 1) {
     341                        kBuildProtectionTerm(&g_ProtData);
    322342                        return usage(stderr);
     343                }
    323344                /*
    324345                 * Need to detect the case:
     
    342363
    343364                if (have_trailing_slash && type == FILE_TO_FILE) {
     365                        kBuildProtectionTerm(&g_ProtData);
    344366                        if (r == -1)
    345367                                return errx(1, "directory %s does not exist",
     
    354376                type = FILE_TO_DIR;
    355377
    356         return copy(argv, type, fts_options);
     378        /* Finally, check that the "to" directory isn't protected. */
     379        if (kBuildProtectionEnforce(&g_ProtData,
     380                                    Rflag || rflag
     381                                    ? KBUILDPROTECTIONTYPE_RECURSIVE
     382                                    : KBUILDPROTECTIONTYPE_FULL,
     383                                    to.p_path)) {
     384            kBuildProtectionTerm(&g_ProtData);
     385            return 1;
     386        }
     387
     388        rc = copy(argv, type, fts_options);
     389        kBuildProtectionTerm(&g_ProtData);
     390        return rc;
    357391}
    358392
     
    613647 */
    614648static int
    615 mastercmp(const FTSENT * const *a, const FTSENT * const *b)
     649mastercmp(const FTSENT **a, const FTSENT **b)
    616650{
    617651        int a_info, b_info;
     
    643677usage(FILE *fp)
    644678{
    645         fprintf(fp, "usage: %s [options] src target\n"
    646                     "   or: %s [options] src1 ... srcN directory\n"
    647                     "   or: %s --help\n"
    648                     "   or: %s --version\n"
    649                     "\n"
    650                     "Options:\n"
    651                     "   -R  Recursive copy.\n"
    652                     "   -H  Follow symbolic links on the commandline. Only valid with -R.\n"
    653                     "   -L  Follow all symbolic links. Only valid with -R.\n"
    654                     "   -P  Do not follow symbolic links. Default. Only valid with -R\n"
    655                     "   -f  Force. Overrides -i and -n.\n"
    656                     "   -i  Iteractive. Overrides -n and -f.\n"
    657                     "   -n  Don't overwrite any files. Overrides -i and -f.\n"
    658                     "   --ignore-non-existing\n"
    659                     "       Don't fail if the specified source file doesn't exist.\n"
    660                     "   --changed\n"
    661                     "       Only copy if changed (i.e. compare first). TODO.\n"
    662                 ,
    663                 g_progname, g_progname, g_progname, g_progname);
     679        fprintf(fp,
     680"usage: %s [options] src target\n"
     681"   or: %s [options] src1 ... srcN directory\n"
     682"   or: %s --help\n"
     683"   or: %s --version\n"
     684"\n"
     685"Options:\n"
     686"   -R  Recursive copy.\n"
     687"   -H  Follow symbolic links on the commandline. Only valid with -R.\n"
     688"   -L  Follow all symbolic links. Only valid with -R.\n"
     689"   -P  Do not follow symbolic links. Default. Only valid with -R\n"
     690"   -f  Force. Overrides -i and -n.\n"
     691"   -i  Iteractive. Overrides -n and -f.\n"
     692"   -n  Don't overwrite any files. Overrides -i and -f.\n"
     693"   --ignore-non-existing\n"
     694"       Don't fail if the specified source file doesn't exist.\n"
     695"   --changed\n"
     696"       Only copy if changed (i.e. compare first).\n"
     697"   --disable-protection\n"
     698"       Will disable the protection file protection applied with -R.\n"
     699"   --enable-protection\n"
     700"       Will enable the protection file protection applied with -R.\n"
     701"   --enable-full-protection\n"
     702"       Will enable the protection file protection for all operations.\n"
     703"   --disable-full-protection\n"
     704"       Will disable the protection file protection for all operations.\n"
     705"   --protection-depth\n"
     706"       Number or path indicating the file protection depth. Default: %d\n"
     707"\n"
     708"Environment:\n"
     709"    KMK_CP_DISABLE_PROTECTION\n"
     710"       Same as --disable-protection. Overrides command line.\n"
     711"    KMK_CP_ENABLE_PROTECTION\n"
     712"       Same as --enable-protection. Overrides everyone else.\n"
     713"    KMK_CP_ENABLE_FULL_PROTECTION\n"
     714"       Same as --enable-full-protection. Overrides everyone else.\n"
     715"    KMK_CP_DISABLE_FULL_PROTECTION\n"
     716"       Same as --disable-full-protection. Overrides command line.\n"
     717"    KMK_CP_PROTECTION_DEPTH\n"
     718"       Same as --protection-depth. Overrides command line.\n"
     719"\n"
     720"The file protection of the top %d layers of the file hierarchy is there\n"
     721"to try prevent makefiles from doing bad things to your system. This\n"
     722"protection is not bulletproof, but should help prevent you from shooting\n"
     723"yourself in the foot.\n"
     724                ,
     725                g_progname, g_progname, g_progname, g_progname,
     726                kBuildProtectionDefaultDepth(), kBuildProtectionDefaultDepth());
    664727        return 1;
    665728}
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette