dd: fix bugs: always assumed conv=sync, died on write errors
[oweals/busybox.git] / coreutils / dd.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini dd implementation for busybox
4  *
5  *
6  * Copyright (C) 2000,2001  Matt Kraai
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9  */
10
11 #include "busybox.h"
12 #include <signal.h>  /* For FEATURE_DD_SIGNAL_HANDLING */
13
14 static const struct suffix_mult dd_suffixes[] = {
15         { "c", 1 },
16         { "w", 2 },
17         { "b", 512 },
18         { "kD", 1000 },
19         { "k", 1024 },
20         { "K", 1024 },  // compat with coreutils dd
21         { "MD", 1000000 },
22         { "M", 1048576 },
23         { "GD", 1000000000 },
24         { "G", 1073741824 },
25         { NULL, 0 }
26 };
27
28 static off_t out_full, out_part, in_full, in_part;
29
30 static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal)
31 {
32         fprintf(stderr, "%"OFF_FMT"+%"OFF_FMT" records in\n"
33                         "%"OFF_FMT"+%"OFF_FMT" records out\n",
34                         in_full, in_part,
35                         out_full, out_part);
36 }
37
38 static ssize_t full_write_or_warn(int fd, const void *buf, size_t len,
39                 const char* filename)
40 {
41         ssize_t n = full_write(fd, buf, len);
42         if (n < 0)
43                 bb_perror_msg("writing '%s'", filename);
44         return n;
45 }
46
47 int dd_main(int argc, char **argv)
48 {
49         enum {
50                 sync_flag    = 1 << 0,
51                 noerror      = 1 << 1,
52                 trunc_flag   = 1 << 2,
53                 twobufs_flag = 1 << 3,
54         };
55         int flags = trunc_flag;
56         size_t oc = 0, ibs = 512, obs = 512;
57         ssize_t n, w;
58         off_t seek = 0, skip = 0, count = OFF_T_MAX;
59         int oflag, ifd, ofd;
60         const char *infile = NULL, *outfile = NULL;
61         char *ibuf, *obuf;
62
63         if (ENABLE_FEATURE_DD_SIGNAL_HANDLING) {
64                 struct sigaction sa;
65
66                 memset(&sa, 0, sizeof(sa));
67                 sa.sa_handler = dd_output_status;
68                 sa.sa_flags = SA_RESTART;
69                 sigemptyset(&sa.sa_mask);
70                 sigaction(SIGUSR1, &sa, 0);
71         }
72
73         for (n = 1; n < argc; n++) {
74                 char *arg = argv[n];
75                 /* Must fit into positive ssize_t */
76                 if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("ibs=", arg, 4))
77                         ibs = xatoul_range_sfx(arg+4, 0, ((size_t)-1L)/2, dd_suffixes);
78                 else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("obs=", arg, 4))
79                         obs = xatoul_range_sfx(arg+4, 0, ((size_t)-1L)/2, dd_suffixes);
80                 else if (!strncmp("bs=", arg, 3))
81                         ibs = obs = xatoul_range_sfx(arg+3, 0, ((size_t)-1L)/2, dd_suffixes);
82                 // FIXME: make them capable of eating LARGE numbers
83                 else if (!strncmp("count=", arg, 6))
84                         count = xatoul_sfx(arg+6, dd_suffixes);
85                 else if (!strncmp("seek=", arg, 5))
86                         seek = xatoul_sfx(arg+5, dd_suffixes);
87                 else if (!strncmp("skip=", arg, 5))
88                         skip = xatoul_sfx(arg+5, dd_suffixes);
89
90                 else if (!strncmp("if=", arg, 3))
91                         infile = arg+3;
92                 else if (!strncmp("of=", arg, 3))
93                         outfile = arg+3;
94                 else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("conv=", arg, 5)) {
95                         arg += 5;
96                         while (1) {
97                                 if (!strncmp("notrunc", arg, 7)) {
98                                         flags &= ~trunc_flag;
99                                         arg += 7;
100                                 } else if (!strncmp("sync", arg, 4)) {
101                                         flags |= sync_flag;
102                                         arg += 4;
103                                 } else if (!strncmp("noerror", arg, 7)) {
104                                         flags |= noerror;
105                                         arg += 7;
106                                 } else {
107                                         bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
108                                 }
109                                 if (arg[0] == '\0') break;
110                                 if (*arg++ != ',') bb_show_usage();
111                         }
112                 } else
113                         bb_show_usage();
114         }
115
116         ibuf = obuf = xmalloc(ibs);
117         if (ibs != obs) {
118                 flags |= twobufs_flag;
119                 obuf = xmalloc(obs);
120         }
121
122         if (infile != NULL)
123                 ifd = xopen(infile, O_RDONLY);
124         else {
125                 ifd = STDIN_FILENO;
126                 infile = bb_msg_standard_input;
127         }
128
129         if (outfile != NULL) {
130                 oflag = O_WRONLY | O_CREAT;
131
132                 if (!seek && (flags & trunc_flag))
133                         oflag |= O_TRUNC;
134
135                 ofd = xopen3(outfile, oflag, 0666);
136
137                 if (seek && (flags & trunc_flag)) {
138                         if (ftruncate(ofd, seek * obs) < 0) {
139                                 struct stat st;
140
141                                 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
142                                                 S_ISDIR(st.st_mode))
143                                         goto die_outfile;
144                         }
145                 }
146         } else {
147                 ofd = STDOUT_FILENO;
148                 outfile = bb_msg_standard_output;
149         }
150
151         if (skip) {
152                 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
153                         while (skip-- > 0) {
154                                 n = safe_read(ifd, ibuf, ibs);
155                                 if (n < 0)
156                                         bb_perror_msg_and_die("%s", infile);
157                                 if (n == 0)
158                                         break;
159                         }
160                 }
161         }
162
163         if (seek) {
164                 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
165                         goto die_outfile;
166         }
167
168         while (in_full + in_part != count) {
169                 if (flags & noerror) {
170                         /* Pre-zero the buffer when doing the noerror thing */
171                         memset(ibuf, '\0', ibs);
172                 }
173
174                 n = safe_read(ifd, ibuf, ibs);
175                 if (n == 0)
176                         break;
177                 if (n < 0) {
178                         if (flags & noerror) {
179                                 n = ibs;
180                                 bb_perror_msg("%s", infile);
181                         } else
182                                 bb_perror_msg_and_die("%s", infile);
183                 }
184                 if ((size_t)n == ibs)
185                         in_full++;
186                 else {
187                         in_part++;
188                         if (flags & sync_flag) {
189                                 memset(ibuf + n, '\0', ibs - n);
190                                 n = ibs;
191                         }
192                 }
193                 if (flags & twobufs_flag) {
194                         char *tmp = ibuf;
195                         while (n) {
196                                 size_t d = obs - oc;
197
198                                 if (d > n)
199                                         d = n;
200                                 memcpy(obuf + oc, tmp, d);
201                                 n -= d;
202                                 tmp += d;
203                                 oc += d;
204                                 if (oc == obs) {
205                                         w = full_write_or_warn(ofd, obuf, obs, outfile);
206                                         if (w < 0) goto out_status;
207                                         if (w == obs)
208                                                 out_full++;
209                                         else if (w > 0)
210                                                 out_part++;
211                                         oc = 0;
212                                 }
213                         }
214                 } else {
215                         w = full_write_or_warn(ofd, ibuf, n, outfile);
216                         if (w < 0) goto out_status;
217                         if (w == obs)
218                                 out_full++;
219                         else if (w > 0)
220                                 out_part++;
221                 }
222         }
223
224         if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
225                 w = full_write_or_warn(ofd, obuf, oc, outfile);
226                 if (w < 0) goto out_status;
227                 if (w > 0)
228                         out_part++;
229         }
230         if (close(ifd) < 0) {
231                 bb_perror_msg_and_die("%s", infile);
232         }
233
234         if (close(ofd) < 0) {
235  die_outfile:
236                 bb_perror_msg_and_die("%s", outfile);
237         }
238  out_status:
239         dd_output_status(0);
240
241         return EXIT_SUCCESS;
242 }