Sparsify / hole punching tool

2012-11-18 Thread Remco Hosman - Yerf IT
I wrote a little tool you can use to scan a file and punch holes, so make it 
sparse.

Confirmed to work on 3.7.0-rc6
Feel free to use it in any way you like, and of course improve it. 

http://pastebin.com/8SjEsBLD

Remco Hosman--
To unsubscribe from this list: send the line unsubscribe linux-btrfs in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: Sparsify / hole punching tool

2012-11-18 Thread Hugo Mills
On Sun, Nov 18, 2012 at 10:04:29PM +0100, Remco Hosman - Yerf IT wrote:
 I wrote a little tool you can use to scan a file and punch holes, so make it 
 sparse.
 
 Confirmed to work on 3.7.0-rc6
 Feel free to use it in any way you like, and of course improve it. 
 
 http://pastebin.com/8SjEsBLD

   For archival purposes, it's probably better to put the whole thing
inline in the text of your mail. This also makes it far easier to make
comments on it, should anyone feel moved to do so.

   Hugo.

-- 
=== Hugo Mills: hugo@... carfax.org.uk | darksatanic.net | lug.org.uk ===
  PGP key: 515C238D from wwwkeys.eu.pgp.net or http://www.carfax.org.uk
 --- In theory, theory and practice are the same. In --- 
  practice,  they're different.  


signature.asc
Description: Digital signature


Re: Sparsify / hole punching tool

2012-11-18 Thread Remco Hosman - Yerf IT
as requested. like this? 


---begin
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include fcntl.h
#include sys/types.h
#include sys/stat.h
#include errno.h
#include stdio.h
#include string.h
#include linux/falloc.h
#include unistd.h

#define BLOCKSIZE 4096

int main(int argc, char** argv) {
ssize_t rsize;
int fh, result;
char buff[BLOCKSIZE], ebuff[BLOCKSIZE];
off_t curpos, pstart, psize;
int freed;

if (argc  2) {
printf(%s [file...]\n, argv[0]);
return 0;
} 

memset(ebuff, 0, BLOCKSIZE);// prepare a block of 0's

for (int i = 1; i  argc; i++) {
char* file=argv[i];
curpos = 0;
pstart = 0;
psize = 0;
freed = 0;

fh = open(file, O_RDWR, O_NOATIME);
if (fh == -1) {
perror(open());
return -1;
}

printf(sparseifying %s , file);
fflush(stdout);
while ((rsize = read(fh, buff, BLOCKSIZE))  0) {
result = memcmp(buff, ebuff,rsize);
if (result == 0) { // block is empty
if (pstart == 0) { // previous block as not empty?
pstart = curpos;
psize = rsize; // save for later punching
} else {
psize += rsize; // previous block was empty too, add size
}

freed += rsize;
} else if (pstart) { // block is not empty and we have a block that 
we still need to punch
result = fallocate(fh, FALLOC_FL_PUNCH_HOLE | 
FALLOC_FL_KEEP_SIZE, pstart, psize);
if (result == -1) {
perror(fallocate());
return -1;
}
pstart = 0;
psize = 0;
}
curpos += rsize;
}

if (pstart) { // still a block to do ?
result = fallocate(fh, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 
pstart, psize);
if (result == -1) {
perror(fallocate());
return -1;
}
}

if (rsize == 0) {
printf(done. freed %i bytes\n, freed);
continue;
}
if (rsize == -1) {
perror(read());
return -1;
}
}
return -1;
}
---end


Remco Hosman


On Nov 18, 2012, at 10:19 PM, Hugo Mills h...@carfax.org.uk wrote:

 On Sun, Nov 18, 2012 at 10:04:29PM +0100, Remco Hosman - Yerf IT wrote:
 I wrote a little tool you can use to scan a file and punch holes, so make it 
 sparse.
 
 Confirmed to work on 3.7.0-rc6
 Feel free to use it in any way you like, and of course improve it. 
 
 http://pastebin.com/8SjEsBLD
 
   For archival purposes, it's probably better to put the whole thing
 inline in the text of your mail. This also makes it far easier to make
 comments on it, should anyone feel moved to do so.
 
   Hugo.
 
 -- 
 === Hugo Mills: hugo@... carfax.org.uk | darksatanic.net | lug.org.uk ===
  PGP key: 515C238D from wwwkeys.eu.pgp.net or http://www.carfax.org.uk
 --- In theory, theory and practice are the same. In --- 
  practice,  they're different.  

--
To unsubscribe from this list: send the line unsubscribe linux-btrfs in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html