Thread: Create File of X size

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    10

    Lightbulb Create File of X size

    I know how in Windows to create a file with an exact size x

    Using the Win32 API, CreateFile, SetFilePointerEx, SetEndOfFile, and CloseHandle in that same order but how could I do something similar in Linux to a NTFS partition (ntfs-3g driver) without having to write all zeros to the file. I see falloc.h but does this only work for ext filesystems.. not ntfs? any tricks that don't require writing all zeros first?

    another trick maybe see if there is adequate space on the disk for the file before writing so the data array of size t can be guaranteed to fit?

    free to use asm, c, c++... just looking for ideas.

    Thanks,
    John

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    To the second question - if you are using Win API - http://msdn.microsoft.com/en-us/libr...37(VS.85).aspx
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Quote Originally Posted by vart View Post
    To the second question - if you are using Win API - http://msdn.microsoft.com/en-us/libr...37(VS.85).aspx
    Thanks but I stated that above.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    Just fseek to the offset you want and close the file.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Dino View Post
    Just fseek to the offset you want and close the file.
    Just seeking won't do it -- you have to write a byte.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Quote Originally Posted by brewbuck View Post
    Just seeking won't do it -- you have to write a byte.
    yep..

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not entirely sure that even that is completely reliable - In the sense that the filesystem may still keep track of what you have written to, and "compress" all unwritten block into "nothing".

    The only absolutely sure way to guarantee that a file fits on the disk is to write the content onto the disk ([disk here means "any device that can hold a filesystem"]).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Quote Originally Posted by matsp View Post
    I'm not entirely sure that even that is completely reliable - In the sense that the filesystem may still keep track of what you have written to, and "compress" all unwritten block into "nothing".

    The only absolutely sure way to guarantee that a file fits on the disk is to write the content onto the disk ([disk here means "any device that can hold a filesystem"]).

    --
    Mats
    completely understand.. does the Linux kernel prevent you from talking to an NTFS driver directly to check drivespace? or is there a better way in C to find out the available disk space, what/how would you hook into the kernel level driver in C .. basically I need to flip to an alternate path rather than fail...

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Quote Originally Posted by Dino View Post
    Just fseek to the offset you want and close the file.
    The only problem I see with this is that it doesn't reserve space on the disk, using the code below I was able to write 10 files of 640MB each to a 1GB pen drive. This method creates sparse files correct?.. is there a way to reserve the space on the disk without writing all zeros to the file (Linux on a NTFS partition using ntfs-3g driver or mod of such)?

    Code:
    /*simple file IO test*/
    #include <stdio.h>
    
    FILE* getFile(const char*,int);
    
    int main(void){
    	
    	
    		for (int i=0;i<10;i++){
    			char str[50];
    			int res = sprintf(str,"/mnt/destination/myTest%d.txt",i);
    			FILE *testFile = getFile(str,671088640);
    			if(testFile !=NULL){
    			fputs("good test",testFile);
    			fclose(testFile);
    			}else
    			  printf("error creating file %d \n",i);
    		}
    	
    		
    	
    		
    	
    
        return 0;
    }
    FILE* getFile(const char* path,int sizeBytes){
    	FILE *pfile;
    	int result;
    	
    	if(pfile = fopen(path,"w")){
    		fseek(pfile,sizeBytes,SEEK_SET);
    		fputs("\n",pfile);
    		fclose(pfile);
    		pfile = fopen(path,"r+");
    		fseek(pfile,0,SEEK_SET);
    	}
    
      return pfile;
    }

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by jcas1411 View Post
    The only problem I see with this is that it doesn't reserve space on the disk, using the code below I was able to write 10 files of 640MB each to a 1GB pen drive. This method creates sparse files correct?.. is there a way to reserve the space on the disk without writing all zeros to the file (Linux on a NTFS partition using ntfs-3g driver or mod of such)?
    I don't know enough about NTFS to say for sure, but on most filesystems which support sparse files, the unallocated blocks are reserved in the available block count. Suppose you had 10000 blocks available, and you allocate 1000 sparse blocks. Even though the specific blocks are not allocated in the block map, the count of available blocks is reduced by 1000.

    If filesystems didn't work this way, there would never be a way to tell how much space you REALLY had available.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Quote Originally Posted by brewbuck View Post
    I don't know enough about NTFS to say for sure, but on most filesystems which support sparse files, the unallocated blocks are reserved in the available block count. Suppose you had 10000 blocks available, and you allocate 1000 sparse blocks. Even though the specific blocks are not allocated in the block map, the count of available blocks is reduced by 1000.

    If filesystems didn't work this way, there would never be a way to tell how much space you REALLY had available.
    Thanks for the suggestion but NTFS is not a block based file system

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by jcas1411 View Post
    Thanks for the suggestion but NTFS is not a block based file system
    I don't know where you're getting that information... It certainly is block based.

    A filesystem that would allow a user to completely mess things up simply by seeking and writing a byte is not a proper filesystem. I'm not sure what you're worried about.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    my bad.. ntfs in linux isnt broken.. had a fat32 drive in.. sparse files fool fat32 but not ntfs..

    in Windows the CreateFile method is actually creating a handler for direct disk IO.. that is what I really want to do in Linux..

    sorry for the double post.. didnt see that it went to page 2
    Last edited by jcas1411; 01-26-2009 at 02:27 PM. Reason: my bad

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    found my own answer.. maybe

    Windows uses unbuffered disk IO whereas Linux uses unbuffered file IO

    http://www.developer.com/net/cplus/a...0919_2119781_5

    nice help how to do on either platform via C

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jcas1411 View Post
    found my own answer.. maybe

    Windows uses unbuffered disk IO whereas Linux uses unbuffered file IO

    http://www.developer.com/net/cplus/a...0919_2119781_5

    nice help how to do on either platform via C
    Both Windows and Linux normally uses buffered I/O - although memory cards/USB sticks are "unbuffered" because people tend to pull them out at almost any time - and it helps if half the data wasn't left dangling in the computers memory [that applies for WinXP onwards, pre-XP the buffering was done for all devices whcih is why you HAVE to "eject" a drive in Win2K etc, but not in WinXP].

    Both Linux and Windows also allow you to add flags when opening a file to say "do not buffer this" (that is the basic OS level file-functions, what the upper layer C or C++ library does with this capability is a different story).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM