Thread: Identifying sparse files programatically in C

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    31

    Identifying sparse files programatically in C

    How would I identify sparse files through a C program ?I know that for a sparse file, its real size would be less than that returned by stat(). How would I get the real size ?

    Thanks

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    You should read more about stat, because that's the system call that you need.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    1
    Look at the stat structure. One crude heurestic is that if the size of the file (st_size) is greater than the number of blocks (st_blocks) multipied by the size of a block, then you have a sparse file.

    The GNU core utilities which support the --sparse option use this heuristic. From copy.c:
    Code:
    #if HAVE_STRUCT_STAT_ST_BLOCKS
              /* Use a heuristic to determine whether SRC_NAME contains any sparse
                 blocks.  If the file has fewer blocks than would normally be
                 needed for a file of its size, then at least one of the blocks in
                 the file is a hole.  */
              if (x->sparse_mode == SPARSE_AUTO && S_ISREG (src_open_sb.st_mode)
                  && ST_NBLOCKS (src_open_sb) < src_open_sb.st_size / ST_NBLOCKSIZE)
                make_holes = true;
    #endif

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-11-2009, 06:45 AM
  2. Header files and multiple definitions
    By sjweinberg in forum C++ Programming
    Replies: 16
    Last Post: 07-17-2009, 05:59 PM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM