C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-03-2009, 03:22 AM   #1
Registered User
 
Join Date: May 2008
Posts: 27
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
rohan_ak1 is offline   Reply With Quote
Old 11-03-2009, 01:06 PM   #2
Registered User
 
Join Date: Nov 2008
Posts: 75
You should read more about stat, because that's the system call that you need.
MisterIO is offline   Reply With Quote
Old 11-12-2009, 10:15 AM   #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
fpmurphy is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
organize project and specify directory for object files in Makefile lehe C++ Programming 2 08-11-2009 06:45 AM
Header files and multiple definitions sjweinberg C++ Programming 16 07-17-2009 05:59 PM
*.cpp and *.h files understanding ElastoManiac C++ Programming 4 06-11-2006 04:45 AM
Linking header files, Source files and main program(Accel. C++) Daniel Primed C++ Programming 3 01-17-2006 11:46 AM
Multiple Cpp Files w4ck0z C++ Programming 5 11-14-2005 02:41 PM


All times are GMT -6. The time now is 10:47 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22