1327Sjkh/*
2228990Suqs * FreeBSD install - a package for the installation and maintenance
3327Sjkh * of non-core utilities.
4327Sjkh *
5327Sjkh * Redistribution and use in source and binary forms, with or without
6327Sjkh * modification, are permitted provided that the following conditions
7327Sjkh * are met:
8327Sjkh * 1. Redistributions of source code must retain the above copyright
9327Sjkh *    notice, this list of conditions and the following disclaimer.
10327Sjkh * 2. Redistributions in binary form must reproduce the above copyright
11327Sjkh *    notice, this list of conditions and the following disclaimer in the
12327Sjkh *    documentation and/or other materials provided with the distribution.
13327Sjkh *
14327Sjkh * Jordan K. Hubbard
15327Sjkh * 18 July 1993
16327Sjkh *
17327Sjkh * Miscellaneous file access utilities.
18327Sjkh *
19327Sjkh */
20327Sjkh
2193520Sobrien#include <sys/cdefs.h>
2293520Sobrien__FBSDID("$FreeBSD$");
2393520Sobrien
2430221Scharnier#include <err.h>
25222035Sflz#include "lib.h"
26327Sjkh#include "add.h"
27327Sjkh
28327Sjkh/*
29327Sjkh * Assuming dir is a desired directory name, make it and all intervening
30327Sjkh * directories necessary.
31327Sjkh */
32327Sjkh
33327Sjkhint
34240476Sjkimmake_hierarchy(char *dir, Boolean set_perm)
35327Sjkh{
36327Sjkh    char *cp1, *cp2;
378857Srgrimes
38327Sjkh    if (dir[0] == '/')
39327Sjkh	cp1 = cp2 = dir + 1;
40327Sjkh    else
41327Sjkh	cp1 = cp2 = dir;
42327Sjkh    while (cp2) {
4367429Sjkh	if ((cp2 = strchr(cp1, '/')) !=NULL )
44327Sjkh	    *cp2 = '\0';
45327Sjkh	if (fexists(dir)) {
4657330Sjkh	    if (!isdir(dir)) {
4757330Sjkh		if (cp2)
4857330Sjkh		    *cp2 = '/';
49327Sjkh		return FAIL;
5057330Sjkh	    }
51327Sjkh	}
52327Sjkh	else {
53206043Sflz	    if (mkdir(dir, 0777) < 0) {
5457330Sjkh		if (cp2)
5557330Sjkh		    *cp2 = '/';
56327Sjkh		return FAIL;
5757330Sjkh	    }
58240476Sjkim	    if (set_perm)
59240476Sjkim		apply_perms(NULL, dir);
60327Sjkh	}
61327Sjkh	/* Put it back */
62327Sjkh	if (cp2) {
63327Sjkh	    *cp2 = '/';
64327Sjkh	    cp1 = cp2 + 1;
65327Sjkh	}
66327Sjkh    }
67327Sjkh    return SUCCESS;
68327Sjkh}
69327Sjkh
70327Sjkh/* Using permission defaults, apply them as necessary */
71327Sjkhvoid
7284745Ssobomaxapply_perms(const char *dir, const char *arg)
73327Sjkh{
7484745Ssobomax    const char *cd_to;
75327Sjkh
764996Sjkh    if (!dir || *arg == '/')	/* absolute path? */
774996Sjkh	cd_to = "/";
78327Sjkh    else
794996Sjkh	cd_to = dir;
804996Sjkh
81327Sjkh    if (Mode)
82131285Seik	if (vsystem("cd %s && /bin/chmod -R %s %s", cd_to, Mode, arg))
8330221Scharnier	    warnx("couldn't change modes of '%s' to '%s'", arg, Mode);
844996Sjkh    if (Owner && Group) {
85131285Seik	if (vsystem("cd %s && /usr/sbin/chown -R %s:%s %s", cd_to, Owner, Group, arg))
8683335Sdd	    warnx("couldn't change owner/group of '%s' to '%s:%s'",
874996Sjkh		   arg, Owner, Group);
884996Sjkh	return;
894996Sjkh    }
904996Sjkh    if (Owner) {
91131285Seik	if (vsystem("cd %s && /usr/sbin/chown -R %s %s", cd_to, Owner, arg))
9230221Scharnier	    warnx("couldn't change owner of '%s' to '%s'", arg, Owner);
934996Sjkh	return;
944996Sjkh    } else if (Group)
95131285Seik	if (vsystem("cd %s && /usr/bin/chgrp -R %s %s", cd_to, Group, arg))
9630221Scharnier	    warnx("couldn't change group of '%s' to '%s'", arg, Group);
97327Sjkh}
98327Sjkh
99