Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / pkg_install / add / futil.c
1 /*
2  * FreeBSD install - a package for the installation and maintainance
3  * of non-core utilities.
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  *
14  * Jordan K. Hubbard
15  * 18 July 1993
16  *
17  * Miscellaneous file access utilities.
18  *
19  * $FreeBSD: src/usr.sbin/pkg_install/add/futil.c,v 1.9.2.4 2002/08/20 06:35:07 obrien Exp $
20  * $DragonFly: src/usr.sbin/pkg_install/add/Attic/futil.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
21  */
22
23 #include <err.h>
24 #include "lib.h"
25 #include "add.h"
26
27 /*
28  * Assuming dir is a desired directory name, make it and all intervening
29  * directories necessary.
30  */
31
32 int
33 make_hierarchy(char *dir)
34 {
35     char *cp1, *cp2;
36
37     if (dir[0] == '/')
38         cp1 = cp2 = dir + 1;
39     else
40         cp1 = cp2 = dir;
41     while (cp2) {
42         if ((cp2 = strchr(cp1, '/')) !=NULL )
43             *cp2 = '\0';
44         if (fexists(dir)) {
45             if (!isdir(dir)) {
46                 if (cp2)
47                     *cp2 = '/';
48                 return FAIL;
49             }
50         }
51         else {
52             if (vsystem("mkdir %s", dir)) {
53                 if (cp2)
54                     *cp2 = '/';
55                 return FAIL;
56             }
57             apply_perms(NULL, dir);
58         }
59         /* Put it back */
60         if (cp2) {
61             *cp2 = '/';
62             cp1 = cp2 + 1;
63         }
64     }
65     return SUCCESS;
66 }
67
68 /* Using permission defaults, apply them as necessary */
69 void
70 apply_perms(const char *dir, const char *arg)
71 {
72     const char *cd_to;
73
74     if (!dir || *arg == '/')    /* absolute path? */
75         cd_to = "/";
76     else
77         cd_to = dir;
78
79     if (Mode)
80         if (vsystem("cd %s && chmod -R %s %s", cd_to, Mode, arg))
81             warnx("couldn't change modes of '%s' to '%s'", arg, Mode);
82     if (Owner && Group) {
83         if (vsystem("cd %s && chown -R %s:%s %s", cd_to, Owner, Group, arg))
84             warnx("couldn't change owner/group of '%s' to '%s:%s'",
85                    arg, Owner, Group);
86         return;
87     }
88     if (Owner) {
89         if (vsystem("cd %s && chown -R %s %s", cd_to, Owner, arg))
90             warnx("couldn't change owner of '%s' to '%s'", arg, Owner);
91         return;
92     } else if (Group)
93         if (vsystem("cd %s && chgrp -R %s %s", cd_to, Group, arg))
94             warnx("couldn't change group of '%s' to '%s'", arg, Group);
95 }
96