]>
Commit | Line | Data |
---|---|---|
23c9e63c DD |
1 | /* |
2 | * Copyright (c) 2000, Red Hat, Inc. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by DJ Delorie <dj@cygnus.com> | |
13 | * | |
14 | */ | |
15 | ||
16 | /* The purpose of this file is to hide the mess needed just to figure | |
17 | out how full a given disk is. There is an old API that can't | |
18 | handle disks bigger than 2G, and a new API that isn't always | |
19 | available. */ | |
20 | ||
b24c88b3 RC |
21 | #if 0 |
22 | static const char *cvsid = | |
23 | "\n%%% $Id$\n"; | |
24 | #endif | |
8507f105 | 25 | |
23c9e63c DD |
26 | #include "win32.h" |
27 | ||
28 | #include "diskfull.h" | |
29 | ||
b24c88b3 RC |
30 | typedef BOOL WINAPI (*GDFS) (LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, |
31 | PULARGE_INTEGER); | |
23c9e63c DD |
32 | |
33 | int | |
3c054baf | 34 | diskfull (String const &path) |
23c9e63c DD |
35 | { |
36 | WINAPI GDFS gdfs = 0; | |
37 | ||
38 | HINSTANCE k = LoadLibrary ("KERNEL32.DLL"); | |
39 | if (k) | |
40 | { | |
41 | gdfs = (GDFS) GetProcAddress (k, "GetDiskFreeSpaceExA"); | |
42 | ||
43 | if (gdfs) | |
44 | { | |
45 | ULARGE_INTEGER avail, total, free; | |
3c054baf | 46 | if (gdfs (path.cstr_oneuse(), &avail, &total, &free)) |
23c9e63c DD |
47 | { |
48 | int perc = avail.QuadPart * 100 / total.QuadPart; | |
b24c88b3 | 49 | return 100 - perc; |
23c9e63c DD |
50 | } |
51 | } | |
52 | } | |
53 | ||
54 | char root[4]; | |
3c054baf | 55 | if (path.cstr_oneuse()[1] != ':') |
23c9e63c DD |
56 | return 0; |
57 | ||
3c054baf | 58 | root[0] = path.cstr_oneuse()[0]; |
23c9e63c DD |
59 | root[1] = ':'; |
60 | root[2] = '\\'; | |
61 | root[3] = 0; | |
62 | ||
63 | DWORD junk, free_clusters, total_clusters; | |
64 | ||
65 | if (GetDiskFreeSpace (root, &junk, &junk, &free_clusters, &total_clusters)) | |
66 | { | |
67 | int perc = free_clusters * 100 / total_clusters; | |
b24c88b3 | 68 | return 100 - perc; |
23c9e63c DD |
69 | } |
70 | ||
71 | return 0; | |
72 | } |