Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 12 Sep 95 22:56:37 +0200
From:      "Pascal Pederiva (Digital UNIX Support Switzerland)" <pascal@zuo.dec.com>
To:        hackers@freebsd.org
Cc:        pascal@zuo.dec.com
Subject:   Enhancement to dd (conv=sparse)
Message-ID:  <9509122056.AA28693@tempx.zuo.dec.com.zuo.dec.com>

next in thread | raw e-mail | index | archive | help

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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?9509122056.AA28693>