Solved

GetDiskFreeSpaceEx problems

Posted on 2004-08-26
11
716 Views
Last Modified: 2012-06-21
Hello;

I am trying to create a function that will absorb all the free space that is available on a pc drive.

I am using GetDiskFreeSpaceEx.

The function like the one pasted below will nearly get all of the free space on my pc C: drive except 12.8 megs.

Also I turned off the option to automaticly call Disk Cleanup. As the Cleanup utility on Windows 98 automatically calls this up. But even with this option off it will still call up Disk Cleanup. Disk Cleanup utility gives the option to remove temporary internet files etc...

The sample below is a stripped down version of the actual code. The actual function is called repeatedly by a timer and creates multiple files up to 10 meg in size depending upon remaining disk space.

It works fine until it reaches 12.8 meg of free disk space left and quits.

The example below only creates one file but the code is basically the same.

Any comments or clues are apreciated.
RJ

int MyDialog::SqeezeDisk()
{
ULARGE_INTEGER fba, tnob;
GetDiskFreeSpaceEx(Drv,&fba,&tnob,NULL);

//Assign the free space available to this thread
unsigned _int64 Size = fba.QuadPart;

unsigned _int64 Wrote=0;
unsigned _int64 Sub=0;
char *ch;

MakeNewFileName(FileName);

FILE *fptr=NULL;
fptr=fopen(FileName,"wb");
if(fptr==NULL)
{
      MessageBox("ERROR failed to create file",FileName);
      return 1;
}//endif
//

////////////////////////WRITE FILE LOOP
unsigned _int64 x=0;

while(1)
{
   //Calculate largest size to write
      
      if(Size >= (unsigned _int64)10000)
            Sub=(unsigned _int64)10000;
      if(Size <  (unsigned _int64)10000 && Size >= (unsigned _int64)1000)
            Sub=(unsigned _int64)1000;
      if(Size <  (unsigned _int64)1000  && Size >= (unsigned _int64)100)
            Sub=(unsigned _int64)100;
      if(Size <  (unsigned _int64)100   && Size >= (unsigned _int64)10)
            Sub=(unsigned _int64)10;
      if(Size <  (unsigned _int64)10    && Size >= (unsigned _int64)1)
            Sub=(unsigned _int64)1;

      if(Size <=0)
      {
            flag=0;//not bad but done
            break; /////////ALL DISK SPACE IS USED UP SO BREAK!!!!
      }

      //create mem space to write
      ch = new char[Sub];

      //fill string with junk chars
      x=0;
      while(1)
      {
            ch[x]='z';
            x++;
            if(x==Sub)break;
      }//endwhile
      
      //write the string
      if(fwrite(ch,sizeof(char)*(int)Sub,1,fptr)<1) //was==-1
      {
            fclose(fptr);
            delete [] ch;
            return 1;
      }//endif

delete [] ch;
Wrote+=Sub;

//See if Wrote is same as or greater than free space
if((unsigned _int64)Wrote>=Size)break;

//recalculate free space
Size-=(unsigned _int64)Sub;

//check if done, no more space
if(Size<=(unsigned _int64)0)break;
}//endwhile
/////////////////////////END WRITE FILE LOOP

fclose(fptr);
//
return 0; //good
}//endfunc

0
Comment
Question by:RJSoft
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 2

Expert Comment

by:carribus
ID: 11902606
I'm not sure if this is the right answer, but the third parameter to GetDiskFreeSpaceEx() is an out parameter that will return ALL the free space on the drive, as opposed to parameter 1, which will return all space available on the drive available to the current user context.

Try and compare whether the two values are equal to the difference of 12.8 Mb you mentioned earlier...
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11903070
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11905091
Writing chunks of 10000 bytes will fail if there isn't contiguous space of 10 k or bigger. You could try to decrease chunk size _AFTER_ failing and close the file if a write with the smallest chunk size fails.

Or you could try to defrag the disk before running the prog.

Regards, Alex

BTW, why are you doing that? Do you want to convince your customers to buy larger hard disks?
0
 
LVL 3

Author Comment

by:RJSoft
ID: 11907416
Hello all;

>>Writing chunks of 10000 bytes will fail if there isn't contiguous space of 10 k or bigger.

Yeah that's what the if statements are supposedly covering. The size of the write is reduced continuously until it reaches 10 bytes and drops to 1 repeatedly. It is supposed to suck up all of the hard drive space.

>>You could try to decrease chunk size _AFTER_ failing and close the file if a write with the smallest chunk size fails.

Smallest chunk is 1 and the logic is supposed to cover it. But somehow not.

>>Or you could try to defrag the disk before running the prog.

Na.

>>Regards, Alex

>>BTW, why are you doing that? Do you want to convince your customers to buy larger hard disks?

LoL! Good one. Maybe hmmm....Hey I like it!
(only joking:))

RJ
0
 
LVL 2

Expert Comment

by:carribus
ID: 11910562
Have you tried checking what the return value is for the third parameter of GetDiskFreeSpaceEx() ?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Assisted Solution

by:rhodgson
rhodgson earned 166 total points
ID: 11911250
As it looks like you are trying to build some sort of disk washer this article may be helpful to you.
http://www.sysinternals.com/ntw2k/source/sdelete.shtml
0
 
LVL 3

Author Comment

by:RJSoft
ID: 11912864
carribus.

Yes, I was hoping things would add up as you suspected. But not.

(At least not as I am doing the addition, using values reported from GetDiskFreeSpaceEx and comparing with a Dos prompt dir command.)

Also, I assume that I am only allowed to create files in the permission status of my main thread. So if I continue on in my current direction I may need to devise a way to chgmod to the super user in Windows (If there is such a thing). In short how do I change my priority level?

jamie; Thanks as usual. Good examples.

rhodgson; Exactly it!

You see I have a scraper type program that downloads any type of media file on any subject. But I have found that it can sometime download *wrong files and *very very wrong files.

BEGIN RANT>>>>>>>

The users of my application can have the best of intentions, just that there is so much trashy stuff on the internet. Good or acceptable stuff has links leads to other suff. So sometimes my app produces "good" results and sometimes bad. Stuff that is just plain wrong. If you get my drift.

Take for example if I where to start a search for jpegs of "nude women". I set the depth level (recursive searches (links leading to links) ) to 5. For each link that is found from my search engine query tool is parsed for links and files and is re-searched to the level of 5 deep. The depth level keeps the recursion from going on forever.

I am almost guaranteed to get some files that I consider hideous allong with many good ones. To think that I have spent all this time to produce a product that can even sometimes produce these kind of results sickens me to no end.

Some things are so gross and so wrong, that they should never be seen. Not even an accidental glimpse. I am sure you can understand.

But what am I to do?

I know that I am not responsible for what is on the internet. So the most I figure that I can do is add the functionally of a disk washer type utility. Which is basically what SDelete does.

This is a good solution and I thank you for it. But it does not sooth my ethical/moral delema.

I find that it is pointless to filter keywords during the search process as ordinary words lead to the un-wanted links. Un-wanted links are usualy not advertised with keywords to help specify thier content so they slip in via the norm. So negative keywords are pointless.

Truthfully, I dont know what to think. On one hand I would like to provide a file washer/ disk washer to help good people from being associated with bad stuff. But on the other hand I dont want to provide a method for bad people to do wrong stuff.

Sometimes I just want to kill the whole project. Wash my hands of it. But I have spent so much time on it. So much sacrifice.

Wondering what any of you think?

END RANT>>>>>>

Thanks
RJ
0
 
LVL 2

Accepted Solution

by:
carribus earned 167 total points
ID: 11913039
Dude :) Read your rant... I understand :)

Now for your question of changing privelages: What you can do is use security tokens to impersonate an specific privelage group or user. I personally have not written any such code, but have read enough articles to know that that is what you need to do. Have a look at ACL and Security Tokens in the Platform SDK.
0
 
LVL 3

Author Comment

by:RJSoft
ID: 11913085
carribus
Thanks!
RJ
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 167 total points
ID: 11913461
>> Smallest chunk is 1 and the logic is supposed to cover it. But somehow not.

Yes these if statements at the biginning of the loop won't work if your disk is fragmented. Look at that:


  |------------------_----------------------------------------_---------------------------_-_-_-_-_------|

Here Size is 7 (count the '_') but the biggest chunk to get is 1.

So you have to move the 'if statements' to that part of the loop where the fwrite has failed.

int Sub = MAX-CHUNK;  /// 10000
char ch[MAX_CHUNK];
while(1)
{
    //fill string with junk chars
     memset(ch, 'z', Sub);
     //write the string
     if(fwrite(ch,sizeof(char)*(int)Sub,1,fptr)<1) //was==-1
     {
          // Calculate next chunk size
         Sub /= 10;
         if (Sub < 1)     // I suppose if there are less bytes than 1 cluster size is available
                              // it will fail with all sizes
         {
            fclose(fptr);
            return 1;
         }
         continue;    // go to end of loop
     }//endif

     Wrote+=Sub;

     //See if Wrote is same as or greater than free space
     if((unsigned _int64)Wrote>=Size)break;

     //recalculate free space
     Size-=(unsigned _int64)Sub;

    //check if done, no more space
    if(Size<=(unsigned _int64)0)break;
}//endwhile
/////////////////////////END WRITE

Regards, Alex

0
 
LVL 3

Author Comment

by:RJSoft
ID: 11917066
Alex; Thanks I will give it a try.
RJ
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now