Skip to content

Commit 0875f4b

Browse files
author
Stephen Colebourne
committed
FileSystemUtils.freeSpaceKb to normalize free space to kb
bug 38574 git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@383179 13f79535-47bb-0310-9956-ffa450edef68
1 parent 170e697 commit 0875f4b

3 files changed

Lines changed: 131 additions & 26 deletions

File tree

RELEASE-NOTES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ Enhancements from 1.1
4242
- AgeFileFilter/SizeFileFilter
4343
New file filters that compares against the age and size of the file
4444

45+
- FileSystemUtils.freeSpaceKb(drive)
46+
New method that unifies result to be in kilobytes [38574]
47+
4548
- FileUtils.contentEquals(File,File)
4649
Performance improved by adding length and file location checking
4750

src/java/org/apache/commons/io/FileSystemUtils.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005 The Apache Software Foundation.
2+
* Copyright 2005-2006 The Apache Software Foundation.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -97,7 +97,12 @@ public FileSystemUtils() {
9797

9898
//-----------------------------------------------------------------------
9999
/**
100-
* Returns the free space on a drive or volume in a cross-platform manner.
100+
* Returns the free space on a drive or volume by invoking
101+
* the command line.
102+
* This method does not normalize the result, and typically returns
103+
* bytes on Windows and Kilobytes on Unix.
104+
* See also {@link #freeSpaceKb(String)}.
105+
* <p>
101106
* Note that some OS's are NOT currently supported, including OS/390.
102107
* <pre>
103108
* FileSystemUtils.freeSpace("C:"); // Windows
@@ -113,9 +118,33 @@ public FileSystemUtils() {
113118
* @throws IOException if an error occurs when finding the free space
114119
*/
115120
public static long freeSpace(String path) throws IOException {
116-
return INSTANCE.freeSpaceOS(path, OS);
121+
return INSTANCE.freeSpaceOS(path, OS, false);
122+
}
123+
124+
//-----------------------------------------------------------------------
125+
/**
126+
* Returns the free space on a drive or volume in kilobytes by invoking
127+
* the command line.
128+
* Note that some OS's are NOT currently supported, including OS/390.
129+
* <pre>
130+
* FileSystemUtils.freeSpace("C:"); // Windows
131+
* FileSystemUtils.freeSpace("/volume"); // *nix
132+
* </pre>
133+
* The free space is calculated via the command line.
134+
* It uses 'dir /-c' on Windows and 'df -k' on *nix.
135+
*
136+
* @param path the path to get free space for, not null, not empty on Unix
137+
* @return the amount of free drive space on the drive or volume in kilobytes
138+
* @throws IllegalArgumentException if the path is invalid
139+
* @throws IllegalStateException if an error occurred in initialisation
140+
* @throws IOException if an error occurs when finding the free space
141+
* @since Commons IO 1.2
142+
*/
143+
public static long freeSpaceKb(String path) throws IOException {
144+
return INSTANCE.freeSpaceOS(path, OS, true);
117145
}
118146

147+
//-----------------------------------------------------------------------
119148
/**
120149
* Returns the free space on a drive or volume in a cross-platform manner.
121150
* Note that some OS's are NOT currently supported, including OS/390.
@@ -128,20 +157,21 @@ public static long freeSpace(String path) throws IOException {
128157
*
129158
* @param path the path to get free space for, not null, not empty on Unix
130159
* @param os the operating system code
160+
* @param kb whether to normalize to kilobytes
131161
* @return the amount of free drive space on the drive or volume
132162
* @throws IllegalArgumentException if the path is invalid
133163
* @throws IllegalStateException if an error occurred in initialisation
134164
* @throws IOException if an error occurs when finding the free space
135165
*/
136-
long freeSpaceOS(String path, int os) throws IOException {
166+
long freeSpaceOS(String path, int os, boolean kb) throws IOException {
137167
if (path == null) {
138168
throw new IllegalArgumentException("Path must not be empty");
139169
}
140170
switch (os) {
141171
case WINDOWS:
142-
return freeSpaceWindows(path);
172+
return (kb ? freeSpaceWindows(path) / 1024 : freeSpaceWindows(path));
143173
case UNIX:
144-
return freeSpaceUnix(path);
174+
return freeSpaceUnix(path, kb);
145175
case OTHER:
146176
throw new IllegalStateException("Unsupported operating system");
147177
default:
@@ -245,17 +275,19 @@ long freeSpaceWindows(String path) throws IOException {
245275
* Find free space on the *nix platform using the 'df' command.
246276
*
247277
* @param path the path to get free space for
278+
* @param kb whether to normalize to kilobytes
248279
* @return the amount of free drive space on the volume
249280
* @throws IOException if an error occurs
250281
*/
251-
long freeSpaceUnix(String path) throws IOException {
282+
long freeSpaceUnix(String path, boolean kb) throws IOException {
252283
if (path.length() == 0) {
253284
throw new IllegalArgumentException("Path must not be empty");
254285
}
255286
path = FilenameUtils.normalize(path);
256287

257288
// build and run the 'dir' command
258-
String[] cmdAttribs = new String[] {"df", path};
289+
String[] cmdAttribs =
290+
(kb ? new String[] {"df", "-k", path} : new String[] {"df", path});
259291

260292
// read the output from the command until we come to the second line
261293
long bytes = -1;

src/test/org/apache/commons/io/FileSystemUtilsTestCase.java

Lines changed: 88 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005 The Apache Software Foundation.
2+
* Copyright 2005-2006 The Apache Software Foundation.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,33 +62,50 @@ protected void tearDown() throws Exception {
6262
public void testGetFreeSpace_String() throws Exception {
6363
// test coverage, as we can't check value
6464
if (File.separatorChar == '/') {
65-
assertEquals(true, FileSystemUtils.freeSpace("/") > 0);
65+
// test assumes Unix using 1kb blocks
66+
long free = FileSystemUtils.freeSpace("/");
67+
long kb = FileSystemUtils.freeSpaceKb("/");
68+
assertEquals((double) free, (double) kb, 256d);
6669
} else {
67-
assertEquals(true, FileSystemUtils.freeSpace("") > 0);
70+
long bytes = FileSystemUtils.freeSpace("");
71+
long kb = FileSystemUtils.freeSpaceKb("");
72+
assertEquals((double) bytes / 1024, (double) kb, 256d);
6873
}
6974
}
7075

7176
//-----------------------------------------------------------------------
7277
public void testGetFreeSpaceOS_String_NullPath() throws Exception {
7378
FileSystemUtils fsu = new FileSystemUtils();
7479
try {
75-
fsu.freeSpaceOS(null, 1);
80+
fsu.freeSpaceOS(null, 1, false);
81+
fail();
82+
} catch (IllegalArgumentException ex) {}
83+
try {
84+
fsu.freeSpaceOS(null, 1, true);
7685
fail();
7786
} catch (IllegalArgumentException ex) {}
7887
}
7988

8089
public void testGetFreeSpaceOS_String_InitError() throws Exception {
8190
FileSystemUtils fsu = new FileSystemUtils();
8291
try {
83-
fsu.freeSpaceOS("", -1);
92+
fsu.freeSpaceOS("", -1, false);
93+
fail();
94+
} catch (IllegalStateException ex) {}
95+
try {
96+
fsu.freeSpaceOS("", -1, true);
8497
fail();
8598
} catch (IllegalStateException ex) {}
8699
}
87100

88101
public void testGetFreeSpaceOS_String_Other() throws Exception {
89102
FileSystemUtils fsu = new FileSystemUtils();
90103
try {
91-
fsu.freeSpaceOS("", 0);
104+
fsu.freeSpaceOS("", 0, false);
105+
fail();
106+
} catch (IllegalStateException ex) {}
107+
try {
108+
fsu.freeSpaceOS("", 0, true);
92109
fail();
93110
} catch (IllegalStateException ex) {}
94111
}
@@ -99,16 +116,18 @@ protected long freeSpaceWindows(String path) throws IOException {
99116
return 12345L;
100117
}
101118
};
102-
assertEquals(12345L, fsu.freeSpaceOS("", 1));
119+
assertEquals(12345L, fsu.freeSpaceOS("", 1, false));
120+
assertEquals(12345L / 1024, fsu.freeSpaceOS("", 1, true));
103121
}
104122

105123
public void testGetFreeSpaceOS_String_Unix() throws Exception {
106124
FileSystemUtils fsu = new FileSystemUtils() {
107-
protected long freeSpaceUnix(String path) throws IOException {
108-
return 12345L;
125+
protected long freeSpaceUnix(String path, boolean kb) throws IOException {
126+
return (kb ? 12345L : 54321);
109127
}
110128
};
111-
assertEquals(12345L, fsu.freeSpaceOS("", 2));
129+
assertEquals(54321L, fsu.freeSpaceOS("", 2, false));
130+
assertEquals(12345L, fsu.freeSpaceOS("", 2, true));
112131
}
113132

114133
//-----------------------------------------------------------------------
@@ -232,7 +251,11 @@ protected BufferedReader openProcessStream(String[] params) {
232251
}
233252
};
234253
try {
235-
fsu.freeSpaceUnix("");
254+
fsu.freeSpaceUnix("", false);
255+
fail();
256+
} catch (IllegalArgumentException ex) {}
257+
try {
258+
fsu.freeSpaceUnix("", true);
236259
fail();
237260
} catch (IllegalArgumentException ex) {}
238261
}
@@ -247,7 +270,20 @@ protected BufferedReader openProcessStream(String[] params) {
247270
return new BufferedReader(reader);
248271
}
249272
};
250-
assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s"));
273+
assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", false));
274+
}
275+
276+
public void testGetFreeSpaceUnix_String_NormalResponseKb() throws Exception {
277+
String lines =
278+
"Filesystem 1K-blocks Used Available Use% Mounted on\n" +
279+
"xxx:/home/users/s 14428928 12956424 1472504 90% /home/users/s";
280+
final StringReader reader = new StringReader(lines);
281+
FileSystemUtils fsu = new FileSystemUtils() {
282+
protected BufferedReader openProcessStream(String[] params) {
283+
return new BufferedReader(reader);
284+
}
285+
};
286+
assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", true));
251287
}
252288

253289
public void testGetFreeSpaceUnix_String_LongResponse() throws Exception {
@@ -261,7 +297,21 @@ protected BufferedReader openProcessStream(String[] params) {
261297
return new BufferedReader(reader);
262298
}
263299
};
264-
assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s"));
300+
assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", false));
301+
}
302+
303+
public void testGetFreeSpaceUnix_String_LongResponseKb() throws Exception {
304+
String lines =
305+
"Filesystem 1K-blocks Used Available Use% Mounted on\n" +
306+
"xxx-yyyyyyy-zzz:/home/users/s\n" +
307+
" 14428928 12956424 1472504 90% /home/users/s";
308+
final StringReader reader = new StringReader(lines);
309+
FileSystemUtils fsu = new FileSystemUtils() {
310+
protected BufferedReader openProcessStream(String[] params) {
311+
return new BufferedReader(reader);
312+
}
313+
};
314+
assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", true));
265315
}
266316

267317
public void testGetFreeSpaceUnix_String_EmptyResponse() throws Exception {
@@ -273,7 +323,11 @@ protected BufferedReader openProcessStream(String[] params) {
273323
}
274324
};
275325
try {
276-
fsu.freeSpaceUnix("/home/users/s");
326+
fsu.freeSpaceUnix("/home/users/s", false);
327+
fail();
328+
} catch (IOException ex) {}
329+
try {
330+
fsu.freeSpaceUnix("/home/users/s", true);
277331
fail();
278332
} catch (IOException ex) {}
279333
}
@@ -289,7 +343,11 @@ protected BufferedReader openProcessStream(String[] params) {
289343
}
290344
};
291345
try {
292-
fsu.freeSpaceUnix("/home/users/s");
346+
fsu.freeSpaceUnix("/home/users/s", false);
347+
fail();
348+
} catch (IOException ex) {}
349+
try {
350+
fsu.freeSpaceUnix("/home/users/s", true);
293351
fail();
294352
} catch (IOException ex) {}
295353
}
@@ -305,7 +363,11 @@ protected BufferedReader openProcessStream(String[] params) {
305363
}
306364
};
307365
try {
308-
fsu.freeSpaceUnix("/home/users/s");
366+
fsu.freeSpaceUnix("/home/users/s", false);
367+
fail();
368+
} catch (IOException ex) {}
369+
try {
370+
fsu.freeSpaceUnix("/home/users/s", true);
309371
fail();
310372
} catch (IOException ex) {}
311373
}
@@ -321,7 +383,11 @@ protected BufferedReader openProcessStream(String[] params) {
321383
}
322384
};
323385
try {
324-
fsu.freeSpaceUnix("/home/users/s");
386+
fsu.freeSpaceUnix("/home/users/s", false);
387+
fail();
388+
} catch (IOException ex) {}
389+
try {
390+
fsu.freeSpaceUnix("/home/users/s", true);
325391
fail();
326392
} catch (IOException ex) {}
327393
}
@@ -337,7 +403,11 @@ protected BufferedReader openProcessStream(String[] params) {
337403
}
338404
};
339405
try {
340-
fsu.freeSpaceUnix("/home/users/s");
406+
fsu.freeSpaceUnix("/home/users/s", false);
407+
fail();
408+
} catch (IOException ex) {}
409+
try {
410+
fsu.freeSpaceUnix("/home/users/s", true);
341411
fail();
342412
} catch (IOException ex) {}
343413
}

0 commit comments

Comments
 (0)