Rahul Ravikumar | 0533600 | 2019-10-14 15:04:32 -0700 | [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 | |
| 19 | import android.annotation.UnsupportedAppUsage; |
| 20 | import android.system.ErrnoException; |
| 21 | import android.system.Os; |
| 22 | import android.system.StructStatVfs; |
| 23 | |
| 24 | /** |
| 25 | * Retrieve overall information about the space on a filesystem. This is a |
| 26 | * wrapper for Unix statvfs(). |
| 27 | */ |
| 28 | public class StatFs { |
| 29 | @UnsupportedAppUsage |
| 30 | private StructStatVfs mStat; |
| 31 | |
| 32 | /** |
| 33 | * Construct a new StatFs for looking at the stats of the filesystem at |
| 34 | * {@code path}. Upon construction, the stat of the file system will be |
| 35 | * performed, and the values retrieved available from the methods on this |
| 36 | * class. |
| 37 | * |
| 38 | * @param path path in the desired file system to stat. |
| 39 | * |
| 40 | * @throws IllegalArgumentException if the file system access fails |
| 41 | */ |
| 42 | public StatFs(String path) { |
| 43 | mStat = doStat(path); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @throws IllegalArgumentException if the file system access fails |
| 48 | */ |
| 49 | private static StructStatVfs doStat(String path) { |
| 50 | try { |
| 51 | return Os.statvfs(path); |
| 52 | } catch (ErrnoException e) { |
| 53 | throw new IllegalArgumentException("Invalid path: " + path, e); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Perform a restat of the file system referenced by this object. This is |
| 59 | * the same as re-constructing the object with the same file system path, |
| 60 | * and the new stat values are available upon return. |
| 61 | * |
| 62 | * @throws IllegalArgumentException if the file system access fails |
| 63 | */ |
| 64 | public void restat(String path) { |
| 65 | mStat = doStat(path); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @deprecated Use {@link #getBlockSizeLong()} instead. |
| 70 | */ |
| 71 | @Deprecated |
| 72 | public int getBlockSize() { |
| 73 | return (int) mStat.f_frsize; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * The size, in bytes, of a block on the file system. This corresponds to |
| 78 | * the Unix {@code statvfs.f_frsize} field. |
| 79 | */ |
| 80 | public long getBlockSizeLong() { |
| 81 | return mStat.f_frsize; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @deprecated Use {@link #getBlockCountLong()} instead. |
| 86 | */ |
| 87 | @Deprecated |
| 88 | public int getBlockCount() { |
| 89 | return (int) mStat.f_blocks; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * The total number of blocks on the file system. This corresponds to the |
| 94 | * Unix {@code statvfs.f_blocks} field. |
| 95 | */ |
| 96 | public long getBlockCountLong() { |
| 97 | return mStat.f_blocks; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @deprecated Use {@link #getFreeBlocksLong()} instead. |
| 102 | */ |
| 103 | @Deprecated |
| 104 | public int getFreeBlocks() { |
| 105 | return (int) mStat.f_bfree; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * The total number of blocks that are free on the file system, including |
| 110 | * reserved blocks (that are not available to normal applications). This |
| 111 | * corresponds to the Unix {@code statvfs.f_bfree} field. Most applications |
| 112 | * will want to use {@link #getAvailableBlocks()} instead. |
| 113 | */ |
| 114 | public long getFreeBlocksLong() { |
| 115 | return mStat.f_bfree; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * The number of bytes that are free on the file system, including reserved |
| 120 | * blocks (that are not available to normal applications). Most applications |
| 121 | * will want to use {@link #getAvailableBytes()} instead. |
| 122 | */ |
| 123 | public long getFreeBytes() { |
| 124 | return mStat.f_bfree * mStat.f_frsize; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @deprecated Use {@link #getAvailableBlocksLong()} instead. |
| 129 | */ |
| 130 | @Deprecated |
| 131 | public int getAvailableBlocks() { |
| 132 | return (int) mStat.f_bavail; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * The number of blocks that are free on the file system and available to |
| 137 | * applications. This corresponds to the Unix {@code statvfs.f_bavail} field. |
| 138 | */ |
| 139 | public long getAvailableBlocksLong() { |
| 140 | return mStat.f_bavail; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * The number of bytes that are free on the file system and available to |
| 145 | * applications. |
| 146 | */ |
| 147 | public long getAvailableBytes() { |
| 148 | return mStat.f_bavail * mStat.f_frsize; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * The total number of bytes supported by the file system. |
| 153 | */ |
| 154 | public long getTotalBytes() { |
| 155 | return mStat.f_blocks * mStat.f_frsize; |
| 156 | } |
| 157 | } |