-
Notifications
You must be signed in to change notification settings - Fork 2
/
files.c
171 lines (168 loc) · 4.41 KB
/
files.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "files.h"
#include "dolly.h"
/*
* If "try_hard" is 1, call must be succesful.
* If try_hard is 1 and an input file can't be opened, the program terminates.
* If try_hard is not 1 and an input file can't be opened, -1 is returend.
*/
int open_infile(int try_hard,struct dollytab * mydollytab) {
char name[256+16];
/* Close old input file if there is one */
if(input != -1) {
if(close(input) == -1) {
perror("close() in open_infile()");
exit(1);
}
}
if(mydollytab->input_split != 0) {
sprintf(name, "%s_%u", mydollytab->infile, input_nr);
} else {
strcpy(name, mydollytab->infile);
}
/* Files for input/output */
if(!mydollytab->compressed_out) {
/* Input is from file */
input = open(name, O_RDONLY);
if(input == -1) {
if(try_hard == 1) {
char str[strlen(name)+18];
sprintf(str, "open inputfile '%s'", name);
perror(str);
exit(1);
} else {
return -1;
}
}
} else {
/* Input should be compressed first. */
if(access(name, R_OK) == -1) {
if(try_hard == 1) {
char str[strlen(name)+18];
sprintf(str, "open inputfile '%s'", name);
perror(str);
exit(1);
} else {
return -1;
}
}
if(pipe(id) == -1) {
perror("input pipe()");
exit(1);
}
input = id[0];
if((in_child_pid = fork()) == 0) {
int fd;
/* Here's the child. */
close(id[0]);
close(1);
(void) !dup(id[1]);
close(id[1]);
if((fd = open(name, O_RDONLY)) == -1) {
exit(1);
}
close(0);
(void) !dup(fd);
close(fd);
if(execl("/usr/bin/gzip", "gzip", "-cf", NULL) == -1) {
perror("execl for gzip in child");
exit(1);
}
} else {
/* Father */
close(id[1]);
}
} /* endif compressed_out */
input_nr++;
return 0;
}
int open_outfile(int try_hard,struct dollytab * mydollytab) {
char name[256+16];
int is_device = 0;
int is_pipe = 0;
/* Close old output file, if there is one. */
if(output != -1) {
if(close(output) == -1) {
perror("close() in open_outfile()");
exit(1);
}
}
if(mydollytab->output_split != 0) {
sprintf(name, "%s_%u", mydollytab->outfile, output_nr);
} else {
strcpy(name, mydollytab->outfile);
}
/* check if file is under /dev, if not open even if the file does not exist. */
if(strcmp("/dev/",name) > 0 ) {
is_device = 1;
}
if(strcmp("-",name) == 0) {
is_pipe = 1;
}
/* Setup the output files/pipes. */
if(!mydollytab->compressed_in) {
/* Output is to a file */
if(!mydollytab->compressed_out && (mydollytab->output_split == 0) && is_device && !is_pipe) {
/* E.g. partition-to-partition cloning */
output = open(name, O_WRONLY);
} else if(!mydollytab->compressed_out && !is_device && !is_pipe) {
/* E.g. file to file cloning */
output = open(name, O_WRONLY | O_CREAT, 0644);
} else if(is_pipe) {
output = 1;
} else {
/* E.g. partition-to-compressed-archive cloning */
output = open(name, O_WRONLY | O_CREAT | O_EXCL, 0644);
}
if(output == -1) {
char str[strlen(name)+19];
sprintf(str, "open outputfile '%s'", name);
perror(str);
exit(1);
}
} else { /* Compressed_In */
if(access(name, W_OK) == -1) {
if(try_hard == 1) {
char str[strlen(name)+19];
sprintf(str, "open outputfile '%s'", name);
perror(str);
exit(1);
} else {
return -1;
}
}
/* Pipe to gunzip */
if(pipe(pd) == -1) {
perror("output pipe");
exit(1);
}
output = pd[1];
if((out_child_pid = fork()) == 0) {
int fd;
/* Here's the child! */
close(pd[1]);
close(0); /* Close stdin */
(void) !dup(pd[0]); /* Duplicate pipe on stdin */
close(pd[0]); /* Close the unused end of the pipe */
if((fd = open(name, O_WRONLY)) == -1) {
if(errno == ENOENT) {
fprintf(stderr, "Outputfile in child does not exist.\n");
}
perror("open outfile in child");
exit(1);
}
close(1);
(void) !dup(fd);
close(fd);
/* Now stdout is redirected to our file */
if(execl("/usr/bin/gunzip", "gunzip", "-c", NULL) == -1) {
perror("execl for gunzip in child");
exit(1);
}
} else {
/* Father */
close(pd[0]);
}
}
output_nr++;
return 0;
}