The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.os; |
| 18 | |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame^] | 19 | import libcore.io.ErrnoException; |
| 20 | import libcore.io.Libcore; |
| 21 | import libcore.io.StructStatFs; |
| 22 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | /** |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame^] | 24 | * Retrieve overall information about the space on a filesystem. This is a |
| 25 | * wrapper for Unix statfs(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | */ |
| 27 | public class StatFs { |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame^] | 28 | private StructStatFs mStat; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame^] | 31 | * Construct a new StatFs for looking at the stats of the filesystem at |
| 32 | * {@code path}. Upon construction, the stat of the file system will be |
| 33 | * performed, and the values retrieved available from the methods on this |
| 34 | * class. |
| 35 | * |
| 36 | * @param path path in the desired file system to stat. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | */ |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame^] | 38 | public StatFs(String path) { |
| 39 | mStat = doStat(path); |
| 40 | } |
| 41 | |
| 42 | private static StructStatFs doStat(String path) { |
| 43 | try { |
| 44 | return Libcore.os.statfs(path); |
| 45 | } catch (ErrnoException e) { |
| 46 | throw new IllegalArgumentException("Invalid path: " + path, e); |
| 47 | } |
| 48 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame^] | 51 | * Perform a restat of the file system referenced by this object. This is |
| 52 | * the same as re-constructing the object with the same file system path, |
| 53 | * and the new stat values are available upon return. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | */ |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame^] | 55 | public void restat(String path) { |
| 56 | mStat = doStat(path); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * The size, in bytes, of a block on the file system. This corresponds to |
| 61 | * the Unix {@code statfs.f_bsize} field. |
| 62 | */ |
| 63 | public int getBlockSize() { |
| 64 | return (int) mStat.f_bsize; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * The total number of blocks on the file system. This corresponds to the |
| 69 | * Unix {@code statfs.f_blocks} field. |
| 70 | */ |
| 71 | public int getBlockCount() { |
| 72 | return (int) mStat.f_blocks; |
| 73 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * The total number of blocks that are free on the file system, including |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame^] | 77 | * reserved blocks (that are not available to normal applications). This |
| 78 | * corresponds to the Unix {@code statfs.f_bfree} field. Most applications |
| 79 | * will want to use {@link #getAvailableBlocks()} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | */ |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame^] | 81 | public int getFreeBlocks() { |
| 82 | return (int) mStat.f_bfree; |
| 83 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * The number of blocks that are free on the file system and available to |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame^] | 87 | * applications. This corresponds to the Unix {@code statfs.f_bavail} field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | */ |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame^] | 89 | public int getAvailableBlocks() { |
| 90 | return (int) mStat.f_bavail; |
| 91 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | } |