/* blklist.c - display block lists of files */
/*
* Copyright (C) 2006 Yoshinori Okuji
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include
#include
#include
#include
#include
#include
#ifdef __linux__
# include
# define FIBMAP _IO(0x00,1)
# define FIGETBSZ _IO(0x00,2)
#else /* ! __linux__ */
# error "This code is specific to Linux at the moment."
#endif /* ! __linux__ */
struct block
{
int start;
int len;
struct block *next;
};
static void
show_blklist (const char *filename)
{
int fd;
int blocksize;
struct stat st;
int i;
struct block head = { 0, 0, 0 };
struct block *current = &head;
fd = open (filename, O_RDONLY);
if (fd < 0)
{
printf ("cannot open %s\n", filename);
goto fail;
}
if (ioctl (fd, FIGETBSZ, &blocksize) < 0)
{
printf ("cannot get the block size of %s\n", filename);
goto fail;
}
if (fstat (fd, &st) < 0)
{
printf ("cannot get the size of %s\n", filename);
goto fail;
}
for (i = 0; i < (st.st_size + blocksize - 1) / blocksize; i++)
{
int block = i;
if (ioctl (fd, FIBMAP, &block) < 0)
{
printf ("cannot get the block %d of %s\n", i, filename);
goto fail;
}
if (current->start + current->len == block)
current->len++;
else
{
struct block *b;
b = malloc (sizeof (*b));
if (! b)
{
printf ("cannot allocate memory\n");
goto fail;
}
b->start = block;
b->len = 1;
b->next = 0;
current->next = b;
current = b;
}
}
printf ("%s: ", filename);
for (current = head.next; current; current = current->next)
{
printf ("%d+%d", current->start, current->len);
if (current->next)
printf (",");
}
printf (" (block size = %d)\n", blocksize);
fail:
for (current = head.next; current; current = head.next)
{
head.next = current->next;
free (current);
}
if (fd >= 0)
close (fd);
}
int
main (int argc, char *argv[])
{
if (argc < 2)
{
printf ("Display the list of blocks for given files\n");
printf ("Usage: blklist FILE...\n");
return 1;
}
while (*++argv)
show_blklist (*argv);
return 0;
}