]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/pkg_install/add/futil.c
This commit was generated by cvs2svn to compensate for changes in r56746,
[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                 return FAIL;
50         }
51         else {
52             if (vsystem("mkdir %s", dir))
53                 return FAIL;
54             apply_perms(NULL, dir);
55         }
56         /* Put it back */
57         if (cp2) {
58             *cp2 = '/';
59             cp1 = cp2 + 1;
60         }
61     }
62     return SUCCESS;
63 }
64
65 /* Using permission defaults, apply them as necessary */
66 void
67 apply_perms(char *dir, char *arg)
68 {
69     char *cd_to;
70
71     if (!dir || *arg == '/')    /* absolute path? */
72         cd_to = "/";
73     else
74         cd_to = dir;
75
76     if (Mode)
77         if (vsystem("cd %s && chmod -R %s %s", cd_to, Mode, arg))
78             warnx("couldn't change modes of '%s' to '%s'", arg, Mode);
79     if (Owner && Group) {
80         if (vsystem("cd %s && chown -R %s.%s %s", cd_to, Owner, Group, arg))
81             warnx("couldn't change owner/group of '%s' to '%s.%s'",
82                    arg, Owner, Group);
83         return;
84     }
85     if (Owner) {
86         if (vsystem("cd %s && chown -R %s %s", cd_to, Owner, arg))
87             warnx("couldn't change owner of '%s' to '%s'", arg, Owner);
88         return;
89     } else if (Group)
90         if (vsystem("cd %s && chgrp -R %s %s", cd_to, Group, arg))
91             warnx("couldn't change group of '%s' to '%s'", arg, Group);
92 }
93