]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/pkg_install/add/futil.c
1. If checking for directory-ness, check "dir" and "dir/" to catch
[FreeBSD/FreeBSD.git] / usr.sbin / pkg_install / add / futil.c
1 #ifndef lint
2 static const char rcsid[] =
3   "$FreeBSD$";
4 #endif
5
6 /*
7  * FreeBSD install - a package for the installation and maintainance
8  * of non-core utilities.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * Jordan K. Hubbard
20  * 18 July 1993
21  *
22  * Miscellaneous file access utilities.
23  *
24  */
25
26 #include <err.h>
27 #include "lib.h"
28 #include "add.h"
29
30 /*
31  * Assuming dir is a desired directory name, make it and all intervening
32  * directories necessary.
33  */
34
35 int
36 make_hierarchy(char *dir)
37 {
38     char *cp1, *cp2;
39
40     if (dir[0] == '/')
41         cp1 = cp2 = dir + 1;
42     else
43         cp1 = cp2 = dir;
44     while (cp2) {
45         if ((cp2 = index(cp1, '/')) !=NULL )
46             *cp2 = '\0';
47         if (fexists(dir)) {
48             if (!isdir(dir)) {
49                 if (cp2)
50                     *cp2 = '/';
51                 return FAIL;
52             }
53         }
54         else {
55             if (vsystem("mkdir %s", dir)) {
56                 if (cp2)
57                     *cp2 = '/';
58                 return FAIL;
59             }
60             apply_perms(NULL, dir);
61         }
62         /* Put it back */
63         if (cp2) {
64             *cp2 = '/';
65             cp1 = cp2 + 1;
66         }
67     }
68     return SUCCESS;
69 }
70
71 /* Using permission defaults, apply them as necessary */
72 void
73 apply_perms(char *dir, char *arg)
74 {
75     char *cd_to;
76
77     if (!dir || *arg == '/')    /* absolute path? */
78         cd_to = "/";
79     else
80         cd_to = dir;
81
82     if (Mode)
83         if (vsystem("cd %s && chmod -R %s %s", cd_to, Mode, arg))
84             warnx("couldn't change modes of '%s' to '%s'", arg, Mode);
85     if (Owner && Group) {
86         if (vsystem("cd %s && chown -R %s.%s %s", cd_to, Owner, Group, arg))
87             warnx("couldn't change owner/group of '%s' to '%s.%s'",
88                    arg, Owner, Group);
89         return;
90     }
91     if (Owner) {
92         if (vsystem("cd %s && chown -R %s %s", cd_to, Owner, arg))
93             warnx("couldn't change owner of '%s' to '%s'", arg, Owner);
94         return;
95     } else if (Group)
96         if (vsystem("cd %s && chgrp -R %s %s", cd_to, Group, arg))
97             warnx("couldn't change group of '%s' to '%s'", arg, Group);
98 }
99