Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

433
Visualizações
C - Linux Sparse File: How to check if file is sparse and print 0-filled disk blocks

What I am trying to do is to write a C program on linux which should be checking in the current directory if there are sparse files, and also I would like to print the number of disk blocks that already represent gaps in the file and the number of disk blocks that are 0-filled but take up disk space.

So far I can access the current directory and print just the files with

DIR *dirp;
struct dirent *dp;

To get done the second part with sparse file I tried to use stat() but it seems not to be working because I don't get the required results as I wished.

So, could anyone show me how to do the part with the sparse file?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

If you want to look for holes in sparse files, see the manpage for lseek, specifically the bit concerning SEEK_HOLE and SEEK_DATA.

If you want to just know the allocated size on disk, then look at then manpage for stat (2):

       struct stat {
           ...
           off_t     st_size;    /* total size, in bytes */
           ...
           blksize_t st_blksize; /* blocksize for file system I/O */
           blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
       };

st_size tells you the total size in bytes, st_blksize * st_blocks gives you the allocated size. If you round st_size up to the next multiple of st_blksize and subtract the file size, that's the size of the holes.

over 4 years ago · Santiago Trujillo Relatório

0

Check size, returned by du utility, and compare with the "apparent size". If you wish you may take a look on the block counting algorithm from du

over 4 years ago · Santiago Trujillo Relatório

0

You can try the following trick with stat result:

if (st.st_blocks * st.st_blksize < st.st_size) { 
  SPARSE-FILE 
} else { 
  PROBABLY NOT SPARSE
}

Not sure if it identifies all sparse files however.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda