From owner-freebsd-hackers Tue Sep 12 14:16:36 1995 Return-Path: hackers-owner Received: (from majordom@localhost) by freefall.freebsd.org (8.6.12/8.6.6) id OAA29732 for hackers-outgoing; Tue, 12 Sep 1995 14:16:36 -0700 Received: from mail1.digital.com (mail1.digital.com [204.123.2.50]) by freefall.freebsd.org (8.6.12/8.6.6) with SMTP id OAA29723 for ; Tue, 12 Sep 1995 14:16:29 -0700 Received: from inet-gw-1.pa.dec.com by mail1.digital.com; (5.65 EXP 4/12/95 for V3.2/1.0/WV) id AA25137; Tue, 12 Sep 1995 14:04:53 -0700 Received: from tempx.zuo.dec.com by mts-gw.pa.dec.com (5.65/09May94) id AA28226; Tue, 12 Sep 95 13:56:41 -0700 Received: from localhost by tempx.zuo.dec.com.zuo.dec.com; (5.65v3.0/1.1.8.2/21Jun95-1247PM) id AA28693; Tue, 12 Sep 1995 22:56:38 +0200 Message-Id: <9509122056.AA28693@tempx.zuo.dec.com.zuo.dec.com> To: hackers@freebsd.org Cc: pascal@zuo.dec.com Reply-To: pascal@zuo.dec.com Subject: Enhancement to dd (conv=sparse) Date: Tue, 12 Sep 95 22:56:37 +0200 From: "Pascal Pederiva (Digital UNIX Support Switzerland)" X-Mts: smtp Sender: hackers-owner@freebsd.org Precedence: bulk Hello here are the diffs of /usr/src/bin/dd ( 2.1.0-950726-SNAP ) to add an option to dd, which creates sparse files. It is just a half-hour midnight hack, so I am sure it needs some cleanup, but it work and I thought it might be interesting for others, too. Best regards, Pascal Pederiva ------------------------------------------------------------------------------- +---------------------------+tm Pascal Pederiva | | | | | | | | Digital UNIX Software Support | d | i | g | i | t | a | l | DEC Digital Equipment Corporation | | | | | | | | Hofwisenstrasse 50 +---------------------------+ CH-8153 Ruemlang (Switzerland) email: pascal@zuo.dec.com phone: +41-1-801-2111 fax: +41-1-801-2172 *** /usr/src/bin/dd/args.c Sat Sep 24 04:54:42 1994 --- args.c Sun Sep 10 23:43:48 1995 *************** *** 281,286 **** --- 281,287 ---- { "oldascii", C_ASCII, C_EBCDIC, e2a_32V }, { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V }, { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V }, + { "sparse", C_SPARSE, 0, NULL }, { "osync", C_OSYNC, C_BS, NULL }, { "swab", C_SWAB, 0, NULL }, { "sync", C_SYNC, 0, NULL }, *** /usr/src/bin/dd/dd.c Wed Jan 18 00:04:29 1995 --- dd.c Mon Sep 11 00:55:34 1995 *************** *** 74,79 **** --- 74,80 ---- STAT st; /* statistics */ void (*cfunc) __P((void)); /* conversion function */ u_long cpy_cnt; /* # of blocks to copy */ + u_long pending=0; /* pending seek if sparse */ u_int ddflags; /* conversion options */ u_int cbsz; /* conversion block size */ u_int files_cnt = 1; /* # of files to copy */ *************** *** 341,347 **** memset(out.dbp, 0, out.dbsz - out.dbcnt); out.dbcnt = out.dbsz; } ! if (out.dbcnt) dd_out(1); } --- 342,348 ---- memset(out.dbp, 0, out.dbsz - out.dbcnt); out.dbcnt = out.dbsz; } ! if (out.dbcnt||pending) dd_out(1); } *************** *** 350,356 **** int force; { static int warned; ! int cnt, n, nw; u_char *outp; /* --- 351,357 ---- int force; { static int warned; ! int cnt, n, nw,i,sparse; u_char *outp; /* *************** *** 369,378 **** * One special case is if we're forced to do the write -- in that case * we play games with the buffer size, and it's usually a partial write. */ outp = out.db; for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) { ! for (cnt = n;; cnt -= nw) { ! nw = write(out.fd, outp, cnt); if (nw <= 0) { if (nw == 0) errx(1, "%s: end of device", out.name); --- 370,416 ---- * One special case is if we're forced to do the write -- in that case * we play games with the buffer size, and it's usually a partial write. */ + outp = out.db; for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) { ! for (cnt = n;; cnt -= nw) { ! sparse=0; ! if (ddflags & C_SPARSE) ! { ! sparse = 1; /* Is buffer sparse ? */ ! for (i=0; i < cnt; i++) ! if (outp[i] != 0) { ! sparse = 0; ! break; ! } ! } ! if(sparse && !force) ! { ! pending+=cnt; ! nw=cnt; ! }else ! { ! if(pending) ! { ! if(force)pending--; ! if (lseek (out.fd, pending, SEEK_CUR) == -1) { ! perror("dd seek error"); ! exit(2); ! } ! if(force)write(out.fd, outp, 1); ! pending=0; ! } ! if(cnt) ! { ! nw = write(out.fd, outp, cnt); ! }else ! { ! return; ! } ! } ! ! ! if (nw <= 0) { if (nw == 0) errx(1, "%s: end of device", out.name); *** /usr/src/bin/dd/dd.h Sun Sep 10 23:07:14 1995 --- dd.h Sun Sep 10 23:10:06 1995 *************** *** 95,97 **** --- 95,98 ---- #define C_UCASE 0x40000 #define C_UNBLOCK 0x80000 #define C_OSYNC 0x100000 + #define C_SPARSE 0x200000