-
Notifications
You must be signed in to change notification settings - Fork 15
/
mkprefix.c
95 lines (81 loc) · 2.09 KB
/
mkprefix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Copyright (c) 2003 Regents of The University of Michigan.
* All Rights Reserved. See COPYRIGHT.
*/
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/param.h>
#include "mkprefix.h"
int extern quiet;
int extern showprogress;
/* mkprefix attempts to create intermediate directories of path.
* Intermediate directories are created with the permission of the
* mode and UID of the last pre-existing parent directory.
*/
int
mkprefix( char *path )
{
char *p, parent_path[ MAXPATHLEN * 2 ];
int saved_errno, parent_stats = 0;
uid_t e_uid;
struct stat st, parent_st;
mode_t mode = 0777;
e_uid = geteuid();
/* Move past any leading /'s */
for ( p = path; *p == '/'; p++ )
;
/* Attempt to create each intermediate directory of path */
for ( p = strchr( p, '/' ); p != NULL; p = strchr( p, '/' )) {
*p = '\0';
if ( mkdir( path, mode ) < 0 ) {
/* Only error if path exists and it's not a directory */
saved_errno = errno;
if ( stat( path, &st ) != 0 ) {
errno = saved_errno;
return( -1 );
}
if ( !S_ISDIR( st.st_mode )) {
errno = EEXIST;
return( -1 );
}
errno = 0;
*p++ = '/';
continue;
}
/* Get stats from parent of first missing directory */
if ( !parent_stats ) {
if ( snprintf( parent_path, MAXPATHLEN, "%s/..", path)
> MAXPATHLEN ) {
fprintf( stderr, "%s/..: path too long\n", path );
*p++ = '/';
return( -1 );
}
if ( stat( parent_path, &parent_st ) != 0 ) {
return( -1 );
}
parent_stats = 1;
}
/* Set mode to that of last preexisting parent */
if ( mode != parent_st.st_mode ) {
if ( chmod( path, parent_st.st_mode ) != 0 ) {
return( -1 );
}
}
/* Set uid to that of last preexisting parent */
if ( e_uid != parent_st.st_uid ) {
if ( chown( path, parent_st.st_uid, parent_st.st_gid ) != 0 ) {
return( -1 );
}
}
if ( !quiet && !showprogress ) {
printf( "%s: created missing prefix\n", path );
}
*p++ = '/';
}
return( 0 );
}