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 | |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 19 | import android.system.ErrnoException; |
| 20 | import android.system.Os; |
| 21 | import android.system.StructStatVfs; |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame] | 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 |
Elliott Hughes | befd0b1 | 2013-07-09 14:46:18 -0700 | [diff] [blame] | 25 | * wrapper for Unix statvfs(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | */ |
| 27 | public class StatFs { |
Elliott Hughes | befd0b1 | 2013-07-09 14:46:18 -0700 | [diff] [blame] | 28 | private StructStatVfs 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 | |
Elliott Hughes | befd0b1 | 2013-07-09 14:46:18 -0700 | [diff] [blame] | 42 | private static StructStatVfs doStat(String path) { |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame] | 43 | try { |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 44 | return Os.statvfs(path); |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame] | 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 | /** |
Jeff Sharkey | b81440b | 2013-04-18 16:00:04 -0700 | [diff] [blame] | 60 | * @deprecated Use {@link #getBlockSizeLong()} instead. |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame] | 61 | */ |
Jeff Sharkey | b81440b | 2013-04-18 16:00:04 -0700 | [diff] [blame] | 62 | @Deprecated |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame] | 63 | public int getBlockSize() { |
| 64 | return (int) mStat.f_bsize; |
| 65 | } |
| 66 | |
| 67 | /** |
Jeff Sharkey | b65ce57 | 2013-02-13 18:31:00 -0800 | [diff] [blame] | 68 | * The size, in bytes, of a block on the file system. This corresponds to |
Elliott Hughes | befd0b1 | 2013-07-09 14:46:18 -0700 | [diff] [blame] | 69 | * the Unix {@code statvfs.f_bsize} field. |
Jeff Sharkey | b65ce57 | 2013-02-13 18:31:00 -0800 | [diff] [blame] | 70 | */ |
| 71 | public long getBlockSizeLong() { |
| 72 | return mStat.f_bsize; |
| 73 | } |
| 74 | |
| 75 | /** |
Jeff Sharkey | b81440b | 2013-04-18 16:00:04 -0700 | [diff] [blame] | 76 | * @deprecated Use {@link #getBlockCountLong()} instead. |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame] | 77 | */ |
Jeff Sharkey | b81440b | 2013-04-18 16:00:04 -0700 | [diff] [blame] | 78 | @Deprecated |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame] | 79 | public int getBlockCount() { |
| 80 | return (int) mStat.f_blocks; |
| 81 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | |
| 83 | /** |
Jeff Sharkey | b81440b | 2013-04-18 16:00:04 -0700 | [diff] [blame] | 84 | * The total number of blocks on the file system. This corresponds to the |
Elliott Hughes | befd0b1 | 2013-07-09 14:46:18 -0700 | [diff] [blame] | 85 | * Unix {@code statvfs.f_blocks} field. |
Jeff Sharkey | b65ce57 | 2013-02-13 18:31:00 -0800 | [diff] [blame] | 86 | */ |
| 87 | public long getBlockCountLong() { |
| 88 | return mStat.f_blocks; |
| 89 | } |
| 90 | |
| 91 | /** |
Jeff Sharkey | b81440b | 2013-04-18 16:00:04 -0700 | [diff] [blame] | 92 | * @deprecated Use {@link #getFreeBlocksLong()} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | */ |
Jeff Sharkey | b81440b | 2013-04-18 16:00:04 -0700 | [diff] [blame] | 94 | @Deprecated |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame] | 95 | public int getFreeBlocks() { |
| 96 | return (int) mStat.f_bfree; |
| 97 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | |
| 99 | /** |
Jeff Sharkey | b65ce57 | 2013-02-13 18:31:00 -0800 | [diff] [blame] | 100 | * The total number of blocks that are free on the file system, including |
| 101 | * reserved blocks (that are not available to normal applications). This |
Elliott Hughes | befd0b1 | 2013-07-09 14:46:18 -0700 | [diff] [blame] | 102 | * corresponds to the Unix {@code statvfs.f_bfree} field. Most applications |
Jeff Sharkey | b65ce57 | 2013-02-13 18:31:00 -0800 | [diff] [blame] | 103 | * will want to use {@link #getAvailableBlocks()} instead. |
| 104 | */ |
| 105 | public long getFreeBlocksLong() { |
| 106 | return mStat.f_bfree; |
| 107 | } |
| 108 | |
| 109 | /** |
Jeff Sharkey | b81440b | 2013-04-18 16:00:04 -0700 | [diff] [blame] | 110 | * The number of bytes that are free on the file system, including reserved |
| 111 | * blocks (that are not available to normal applications). Most applications |
| 112 | * will want to use {@link #getAvailableBytes()} instead. |
Jeff Sharkey | b65ce57 | 2013-02-13 18:31:00 -0800 | [diff] [blame] | 113 | */ |
| 114 | public long getFreeBytes() { |
| 115 | return mStat.f_bfree * mStat.f_bsize; |
| 116 | } |
| 117 | |
| 118 | /** |
Jeff Sharkey | b81440b | 2013-04-18 16:00:04 -0700 | [diff] [blame] | 119 | * @deprecated Use {@link #getAvailableBlocksLong()} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | */ |
Jeff Sharkey | b81440b | 2013-04-18 16:00:04 -0700 | [diff] [blame] | 121 | @Deprecated |
Kenny Root | bdd23ae | 2012-08-16 10:43:47 -0700 | [diff] [blame] | 122 | public int getAvailableBlocks() { |
| 123 | return (int) mStat.f_bavail; |
| 124 | } |
Jeff Sharkey | b65ce57 | 2013-02-13 18:31:00 -0800 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * The number of blocks that are free on the file system and available to |
Elliott Hughes | befd0b1 | 2013-07-09 14:46:18 -0700 | [diff] [blame] | 128 | * applications. This corresponds to the Unix {@code statvfs.f_bavail} field. |
Jeff Sharkey | b65ce57 | 2013-02-13 18:31:00 -0800 | [diff] [blame] | 129 | */ |
| 130 | public long getAvailableBlocksLong() { |
| 131 | return mStat.f_bavail; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * The number of bytes that are free on the file system and available to |
| 136 | * applications. |
| 137 | */ |
| 138 | public long getAvailableBytes() { |
| 139 | return mStat.f_bavail * mStat.f_bsize; |
| 140 | } |
Jeff Sharkey | b81440b | 2013-04-18 16:00:04 -0700 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * The total number of bytes supported by the file system. |
| 144 | */ |
| 145 | public long getTotalBytes() { |
| 146 | return mStat.f_blocks * mStat.f_bsize; |
| 147 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | } |