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