-
Notifications
You must be signed in to change notification settings - Fork 15
/
installer.c
156 lines (131 loc) · 3.33 KB
/
installer.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "substdio.h"
#include "stralloc.h"
#include "getln.h"
#include "readwrite.h"
#include "exit.h"
#include "open.h"
#include "error.h"
#include "strerr.h"
#include "byte.h"
#include "scan.h"
stralloc target = {0};
char *to;
const char FATAL[] = "installer: fatal: ";
void nomem() { strerr_die2x(111,FATAL,"out of memory"); }
char inbuf[SUBSTDIO_INSIZE];
char outbuf[SUBSTDIO_OUTSIZE];
substdio ssin;
substdio ssout;
void doit(line)
stralloc *line;
{
char *x;
unsigned int xlen;
unsigned int i;
char *type;
char *uidstr;
char *gidstr;
char *modestr;
char *mid;
char *name;
unsigned long uid;
unsigned long gid;
unsigned long mode;
int fdin;
int fdout;
int opt;
x = line->s; xlen = line->len;
opt = (*x == '?');
x += opt;
xlen -= opt;
type = x;
i = byte_chr(x,xlen,':'); if (i == xlen) return;
x[i++] = 0; x += i; xlen -= i;
uidstr = x;
i = byte_chr(x,xlen,':'); if (i == xlen) return;
x[i++] = 0; x += i; xlen -= i;
gidstr = x;
i = byte_chr(x,xlen,':'); if (i == xlen) return;
x[i++] = 0; x += i; xlen -= i;
modestr = x;
i = byte_chr(x,xlen,':'); if (i == xlen) return;
x[i++] = 0; x += i; xlen -= i;
mid = x;
i = byte_chr(x,xlen,':'); if (i == xlen) return;
x[i++] = 0; x += i; xlen -= i;
name = x;
i = byte_chr(x,xlen,':'); if (i == xlen) return;
x[i++] = 0; x += i; xlen -= i;
stralloc_copys(&target,to);
stralloc_cats(&target,mid);
stralloc_cats(&target,name);
stralloc_0(&target);
if (xlen > 0) name = x;
uid = -1; if (*uidstr) scan_ulong(uidstr,&uid);
gid = -1; if (*gidstr) scan_ulong(gidstr,&gid);
scan_8long(modestr,&mode);
switch(*type) {
case 'd':
if (mkdir(target.s,0700) == -1)
if (errno != error_exist)
strerr_die3sys(111,FATAL,"unable to mkdir ",target.s);
break;
case 'c':
fdin = open_read(name);
if (fdin == -1) {
if (opt)
return;
else
strerr_die3sys(111,FATAL,"unable to read ",name);
}
substdio_fdbuf(&ssin,read,fdin,inbuf,sizeof(inbuf));
fdout = open_trunc(target.s);
if (fdout == -1)
strerr_die3sys(111,FATAL,"unable to write ",target.s);
substdio_fdbuf(&ssout,write,fdout,outbuf,sizeof(outbuf));
switch(substdio_copy(&ssout,&ssin)) {
case -2:
strerr_die3sys(111,FATAL,"unable to read ",name);
case -3:
strerr_die3sys(111,FATAL,"unable to write ",target.s);
}
close(fdin);
if (substdio_flush(&ssout) == -1)
strerr_die3sys(111,FATAL,"unable to write ",target.s);
if (fsync(fdout) == -1)
strerr_die3sys(111,FATAL,"unable to write ",target.s);
close(fdout);
break;
default:
return;
}
if (chown(target.s,uid,gid) == -1)
strerr_die3sys(111,FATAL,"unable to chown ",target.s);
if (chmod(target.s,mode) == -1)
strerr_die3sys(111,FATAL,"unable to chmod ",target.s);
}
char buf[256];
substdio in = SUBSTDIO_FDBUF(read,0,buf,sizeof(buf));
stralloc line = {0};
int main(argc,argv)
int argc;
char **argv;
{
int match;
umask(077);
to = argv[1];
if (!to) strerr_die2x(100,FATAL,"installer: usage: install dir");
for (;;) {
if (getln(&in,&line,&match,'\n') == -1)
strerr_die2sys(111,FATAL,"unable to read input");
if (line.len > 0)
line.s[--line.len] = 0;
doit(&line);
if (!match)
_exit(0);
}
(void)argc;
}