File: 0009-Replace-substdio-and-the-resulting-read-512-write-51.patch

package info (click to toggle)
safecat 1.13%2Bgit20170317.185e8bf-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 776 kB
  • sloc: ansic: 2,748; makefile: 4
file content (424 lines) | stat: -rw-r--r-- 10,757 bytes parent folder | download
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
From: =?utf-8?b?0L3QsNCx?= <nabijaczleweli@nabijaczleweli.xyz>
Date: Fri, 22 Aug 2025 00:55:56 +0200
Subject: Replace substdio and the resulting read(512)/write(512) loop with
 voreutils-derived splice/c_f_r/sendfile/[read(64k)/fwrite()] decay chain

---
 byte.h          |  9 --------
 meson.build     |  4 ----
 safecat.c       | 11 +++++++--
 stralloc_cat.c  |  1 -
 stralloc_catb.c |  4 ++--
 stralloc_copy.c |  1 -
 stralloc_opyb.c |  4 ++--
 substdi.c       | 30 ------------------------
 substdio.c      | 10 --------
 substdio.h      | 32 --------------------------
 substdio_copy.c | 16 -------------
 substdo.c       | 71 ---------------------------------------------------------
 writefile.c     | 59 +++++++++++++++++++++++++++++++----------------
 writefile.h     |  4 +++-
 14 files changed, 55 insertions(+), 201 deletions(-)
 delete mode 100644 byte.h
 delete mode 100644 substdi.c
 delete mode 100644 substdio.c
 delete mode 100644 substdio.h
 delete mode 100644 substdio_copy.c
 delete mode 100644 substdo.c

diff --git a/byte.h b/byte.h
deleted file mode 100644
index 8df08fa..0000000
--- a/byte.h
+++ /dev/null
@@ -1,9 +0,0 @@
-#ifndef BYTE_H
-#define BYTE_H
-
-#include <string.h>
-
-#define byte_copy(to, n, from) memcpy(to, from, n)
-#define byte_copyr(to, n, from) memmove(to, from, n)
-
-#endif
diff --git a/meson.build b/meson.build
index 91affc7..f5e9b04 100644
--- a/meson.build
+++ b/meson.build
@@ -14,10 +14,6 @@ executable('safecat',
 	'stralloc_pend.c',
 	'strerr_die.c',
 	'strerr_sys.c',
-	'substdi.c',
-	'substdio.c',
-	'substdo.c',
-	'substdio_copy.c',
 	'tempfile.c',
 	'writefile.c',
 	install: true)
diff --git a/safecat.c b/safecat.c
index 30e0a07..d0e15f9 100644
--- a/safecat.c
+++ b/safecat.c
@@ -39,6 +39,7 @@ int main(int argc, char *argv[]) {
   char *tempdir  = NULL;
   char *destdir  = NULL;
   int   outfd    = 0;
+  FILE *outfdf   = NULL;
   stralloc dstpath = {0};
   stralloc outfile = {0};
   stralloc outpath = {0};
@@ -98,11 +99,17 @@ int main(int argc, char *argv[]) {
     strerr_die_sys(111,"safecat: fatal: ","couldn't create output file: ");
   }
 
+  outfdf = fdopen(outfd, "w");
+  if (!outfdf) {
+    unlink(tmppath.s);
+    strerr_die_sys(111,"safecat: fatal: ","can't fdopen output file: ");
+  }
+
   /* Step 5:  Copy stdin to the temp file. */
-  writefile(outfd);
+  writefile(outfdf);
 
   /* Close the file, checking the return value. */
-  if(fsync(outfd) == -1 || close(outfd) == -1) {
+  if(fsync(outfd) == -1 || fclose(outfdf) == -1) {
     unlink(tmppath.s);
     strerr_die_sys(111,"safecat: fatal: ","can't fsync/close output file: ");
   }
diff --git a/stralloc_cat.c b/stralloc_cat.c
index ca95b9b..8891562 100644
--- a/stralloc_cat.c
+++ b/stralloc_cat.c
@@ -1,4 +1,3 @@
-#include "byte.h"
 #include "stralloc.h"
 
 int stralloc_cat(stralloc *sato, stralloc *safrom)
diff --git a/stralloc_catb.c b/stralloc_catb.c
index d9161d4..69b0839 100644
--- a/stralloc_catb.c
+++ b/stralloc_catb.c
@@ -1,11 +1,11 @@
 #include "stralloc.h"
-#include "byte.h"
+#include <string.h>
 
 int stralloc_catb(stralloc *sa, char *s, unsigned int n)
 {
   if (!sa->s) return stralloc_copyb(sa,s,n);
   if (!stralloc_readyplus(sa,n + 1)) return 0;
-  byte_copy(sa->s + sa->len,n,s);
+  memcpy(sa->s + sa->len,s,n);
   sa->len += n;
   sa->s[sa->len] = 'Z'; /* ``offensive programming'' */
   return 1;
diff --git a/stralloc_copy.c b/stralloc_copy.c
index f5d6200..dd3e16e 100644
--- a/stralloc_copy.c
+++ b/stralloc_copy.c
@@ -1,4 +1,3 @@
-#include "byte.h"
 #include "stralloc.h"
 
 int stralloc_copy(stralloc *sato, stralloc *safrom)
diff --git a/stralloc_opyb.c b/stralloc_opyb.c
index 04bf72e..6f9d687 100644
--- a/stralloc_opyb.c
+++ b/stralloc_opyb.c
@@ -1,10 +1,10 @@
 #include "stralloc.h"
-#include "byte.h"
+#include <string.h>
 
 int stralloc_copyb(stralloc *sa, char *s, unsigned int n)
 {
   if (!stralloc_ready(sa,n + 1)) return 0;
-  byte_copy(sa->s,n,s);
+  memcpy(sa->s,s,n);
   sa->len = n;
   sa->s[n] = 'Z'; /* ``offensive programming'' */
   return 1;
diff --git a/substdi.c b/substdi.c
deleted file mode 100644
index 23892af..0000000
--- a/substdi.c
+++ /dev/null
@@ -1,30 +0,0 @@
-#include "substdio.h"
-#include "byte.h"
-#include <errno.h>
-
-static int oneread(int (*op)(int fd, char *buf, int len), int fd, char *buf, int len)
-{
-  register int r;
-
-  for (;;) {
-    r = op(fd,buf,len);
-    if (r == -1) if (errno == EINTR) continue;
-    return r;
-  }
-}
-
-int substdio_feed(substdio *s)
-{
-  register int r;
-  register int q;
-
-  if (s->p) return s->p;
-  q = s->n;
-  r = oneread(s->op,s->fd,s->x,q);
-  if (r <= 0) return r;
-  s->p = r;
-  q -= r;
-  s->n = q;
-  if (q > 0) /* damn, gotta shift */ byte_copyr(s->x + q,r,s->x);
-  return r;
-}
diff --git a/substdio.c b/substdio.c
deleted file mode 100644
index a6160f3..0000000
--- a/substdio.c
+++ /dev/null
@@ -1,10 +0,0 @@
-#include "substdio.h"
-
-void substdio_fdbuf(substdio *s, int (*op)(), int fd, char *buf, int len)
-{
-  s->x = buf;
-  s->fd = fd;
-  s->op = op;
-  s->p = 0;
-  s->n = len;
-}
diff --git a/substdio.h b/substdio.h
deleted file mode 100644
index 0fdf4db..0000000
--- a/substdio.h
+++ /dev/null
@@ -1,32 +0,0 @@
-#ifndef SUBSTDIO_H
-#define SUBSTDIO_H
-
-typedef struct substdio {
-  char *x;
-  int p;
-  int n;
-  int fd;
-  int (*op)(int fd, char *buf, int len);
-} substdio;
-
-#define SUBSTDIO_FDBUF(op,fd,buf,len) { (buf), 0, (len), (fd), (op) }
-
-extern void substdio_fdbuf(substdio *s, int (*op)(int fd, char *buf, int len), int fd, char *buf, int len);
-
-extern int substdio_flush(substdio *s);
-extern int substdio_put(substdio *s, char *buf, int len);
-extern int substdio_bput(substdio *s, char *buf, int len);
-extern int substdio_puts(substdio *s, char *buf);
-
-extern int substdio_feed(substdio *s);
-
-#define substdio_fileno(s) ((s)->fd)
-
-#define SUBSTDIO_OUTSIZE 8192
-
-#define substdio_PEEK(s) ( (s)->x + (s)->n )
-#define substdio_SEEK(s,len) ( ( (s)->p -= (len) ) , ( (s)->n += (len) ) )
-
-extern int substdio_copy(substdio *ssout, substdio *ssin);
-
-#endif
diff --git a/substdio_copy.c b/substdio_copy.c
deleted file mode 100644
index 5bbfd3f..0000000
--- a/substdio_copy.c
+++ /dev/null
@@ -1,16 +0,0 @@
-#include "substdio.h"
-
-int substdio_copy(substdio *ssout, substdio *ssin)
-{
-  register int n;
-  register char *x;
-
-  for (;;) {
-    n = substdio_feed(ssin);
-    if (n < 0) return -2;
-    if (!n) return 0;
-    x = substdio_PEEK(ssin);
-    if (substdio_put(ssout,x,n) == -1) return -3;
-    substdio_SEEK(ssin,n);
-  }
-}
diff --git a/substdo.c b/substdo.c
deleted file mode 100644
index cd75e28..0000000
--- a/substdo.c
+++ /dev/null
@@ -1,71 +0,0 @@
-#include "substdio.h"
-#include "byte.h"
-#include <string.h>
-#include <errno.h>
-
-static int allwrite(int (*op)(int fd, char *buf, int len), int fd, char *buf, int len)
-{
-  register int w;
-
-  while (len) {
-    w = op(fd,buf,len);
-    if (w == -1) {
-      if (errno == EINTR) continue;
-      return -1; /* note that some data may have been written */
-    }
-    buf += w;
-    len -= w;
-  }
-  return 0;
-}
-
-int substdio_flush(substdio *s)
-{
-  register int p;
-
-  p = s->p;
-  if (!p) return 0;
-  s->p = 0;
-  return allwrite(s->op,s->fd,s->x,p);
-}
-
-int substdio_bput(substdio *s, char *buf, int len)
-{
-  register int n;
-
-  while (len > (n = s->n - s->p)) {
-    byte_copy(s->x + s->p,n,buf); s->p += n; buf += n; len -= n;
-    if (substdio_flush(s) == -1) return -1;
-  }
-  /* now len <= s->n - s->p */
-  byte_copy(s->x + s->p,len,buf);
-  s->p += len;
-  return 0;
-}
-
-int substdio_put(substdio *s, char *buf, int len)
-{
-  register int n;
-
-  n = s->n;
-  if (len > n - s->p) {
-    if (substdio_flush(s) == -1) return -1;
-    /* now s->p == 0 */
-    if (n < SUBSTDIO_OUTSIZE) n = SUBSTDIO_OUTSIZE;
-    while (len > s->n) {
-      if (n > len) n = len;
-      if (allwrite(s->op,s->fd,buf,n) == -1) return -1;
-      buf += n;
-      len -= n;
-    }
-  }
-  /* now len <= s->n - s->p */
-  byte_copy(s->x + s->p,len,buf);
-  s->p += len;
-  return 0;
-}
-
-int substdio_puts(substdio *s, char *buf)
-{
-  return substdio_put(s,buf,strlen(buf));
-}
diff --git a/writefile.c b/writefile.c
index 5ca8a37..02414b9 100644
--- a/writefile.c
+++ b/writefile.c
@@ -1,36 +1,55 @@
+/* Copyright (c) 2025, наб <nabijaczlweli@nabijaczlweli.xyz> */
 /* Copyright (c) 2017, Adrian Perez de Castro <aperez@igalia.com> */
 /* Copyright (c) 2000, Len Budney. See COPYING for details. */
+#if __linux__
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <sys/sendfile.h>
+#endif
 #include "stralloc.h"
 #include "strerr.h"
-#include "substdio.h"
 #include "writefile.h"
 #include <errno.h>
 #include <unistd.h>
 
 extern stralloc tmppath;
 
-static int false_read(int fd, char *buf, int len) {
-  return read(fd, buf, len);
-}
-
-static int false_write(int fd, char *buf, int len) {
-  return write(fd, buf, len);
-}
-
 /* ****************************************************************** */
-void writefile(int fd) {
-  char inbuf[512];
-  char outbuf[512];
-  substdio ssin;
-  substdio ssout;
-
-  /* Prepare substdio buffers for reading and writing. */
-  substdio_fdbuf(&ssin,false_read,0,inbuf,sizeof(inbuf));
-  substdio_fdbuf(&ssout,false_write,fd,outbuf,sizeof(outbuf));
+void writefile(FILE * fd) {
+  ssize_t rd;
+#if __linux__ // derived from voreutils cat
+  // We use the special state of knowing that fd was /just/ fdopen()ed and isn't dirty.
+  // All errors are better-served when detected by the read()/fwrite() loop below
+  while((rd = splice(0, NULL, fileno(fd), NULL, 128 * 1024 * 1024, SPLICE_F_MOVE | SPLICE_F_MORE)) > 0)
+    ;
+  if(rd != -1)
+    return;
+
+  while((rd = copy_file_range(0, NULL, fileno(fd), NULL, 128 * 1024 * 1024, 0)) > 0)
+    ;
+  if(rd != -1)
+    return;
+
+  while((rd = sendfile(fileno(fd), 0, NULL, 128 * 1024 * 1024)) > 0)
+    ;
+  if(rd != -1)
+    return;
+#endif
 
   /* Copy stdin to the output file, watching the return values each time. */
-  if (substdio_copy(&ssout,&ssin) < 0) goto fail;
-  if (substdio_flush(&ssout) == -1) goto fail;
+  for(char buf[64 * 1024];;) {
+    while((rd = read(0, buf, sizeof(buf))) == -1 && errno == EINTR)
+      ;
+    if(rd == -1)
+      goto fail;
+    if(!rd)
+      break;
+
+    if(fwrite(buf, 1, rd, fd) != rd)
+      goto fail;
+  }
+  if(fflush(fd))
+    goto fail;
 
   /* The file is copied. */
   return;
diff --git a/writefile.h b/writefile.h
index 2dd5218..4c471c2 100644
--- a/writefile.h
+++ b/writefile.h
@@ -1,4 +1,6 @@
 /* Copyright (c) 2000, Len Budney. See COPYING for details. */
 
+#include <stdio.h>
+
 /* Copy stdin to the given file descriptor, treating all failures as fatal. */
-void writefile(int fd);
+void writefile(FILE * fd);