[OT] file with holes

Giorgos Keramidas keramida at ceid.upatras.gr
Mon May 20 23:54:01 EEST 2002


On 2002-05-20 15:30, Άγγελος Οικονομόπουλος wrote:
> Pws 8a mporousa na antigrapsw ena file with holes apo ena (unix)
> filesystem se ena allo, kratwntas ta holes? to dump e3aireitai :)
> Apo oso to exw psa3ei (oxi kai toso polu) kati tetoio einai adunato
> me ta uparxonta syscalls. Can somebody prove me wrong?

Xoris na exeis prosbash sto idio to filesystem layer, kai na blepeis
poia i-nodes katalambanei ena arxeio kai poia data blocks, den mporo
na skefto kapoio eukolo tropo na jereis an kapoio block apo mhdenika
einai ontws block apo mhdenika h ena hole.  Apo thn allh, mporeis
panta na kaneis kati san to ejhs:

| #include <err.h>
| #include <unistd.h>
|
| #define BUFLEN	1024
|
| int
| copyholes (int fd, int ofd, int minhole)
| {
| 	char buf[BUFLEN];	/* read buffer */
| 	int nbytes;		/* number of read bytes in buf[] */
|	int i;
|
| 	while ((nbytes = read(fd, block, BUFLEN)) > 0) {
| 		i = 0;
| 		while (i < nbytes) {
| 			if (buf[i] != 0 &&
| 			    write(ofd, &buf[i], sizeof(char)) < 0) {
| 				err("write(%d)", ofd);
| 			} else {
| 				int k = i; /* Start of zero-block. */
|
| 				while (buf[i] == 0)
| 					i++;
| 				/*
| 				 * If less than minhole consecutive zero
| 				 * bytes, write them out as normal bytes, else
| 				 * make a hole.
| 				 */
| 				if ((i - k) < minhole &&
| 				    write(&buf[k], i - k) < 0) {
| 					err("write(%d)", ofd);
| 				} else if (lseek(ofd, i - k, SEEK_CUR) < 0) {
| 					err("lseek(%d)", ofd);
| 				}
| 				i--;
| 			}
| 			i++;
| 		}
| 	}
| }

A, nai.  Den ton exo dokimasei ton kwdika.  Mporei oute compile na min
kanei.  H genikh idea einai pou thelo na grapso.  "Antigrafoume ta
panta, ektos apo ta mhdenika.  Otan brei mhdenika, kai einai pano apo
`minhole' sunexomena mhdenika, tote ftiaxnei ena hole.  Allios ta
antigrafei san kanonika mhdenika."

-- 
Giorgos Keramidas    - http://www.FreeBSD.org
keramida at FreeBSD.org - The Power to Serve



More information about the Linux-greek-users mailing list