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.compat.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 */ StatFs(String path)42 public StatFs(String path) { 43 mStat = doStat(path); 44 } 45 46 /** 47 * @throws IllegalArgumentException if the file system access fails 48 */ doStat(String path)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 */ restat(String path)64 public void restat(String path) { 65 mStat = doStat(path); 66 } 67 68 /** 69 * @deprecated Use {@link #getBlockSizeLong()} instead. 70 */ 71 @Deprecated getBlockSize()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 */ getBlockSizeLong()80 public long getBlockSizeLong() { 81 return mStat.f_frsize; 82 } 83 84 /** 85 * @deprecated Use {@link #getBlockCountLong()} instead. 86 */ 87 @Deprecated getBlockCount()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 */ getBlockCountLong()96 public long getBlockCountLong() { 97 return mStat.f_blocks; 98 } 99 100 /** 101 * @deprecated Use {@link #getFreeBlocksLong()} instead. 102 */ 103 @Deprecated getFreeBlocks()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 */ getFreeBlocksLong()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 */ getFreeBytes()123 public long getFreeBytes() { 124 return mStat.f_bfree * mStat.f_frsize; 125 } 126 127 /** 128 * @deprecated Use {@link #getAvailableBlocksLong()} instead. 129 */ 130 @Deprecated getAvailableBlocks()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 */ getAvailableBlocksLong()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 */ getAvailableBytes()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 */ getTotalBytes()154 public long getTotalBytes() { 155 return mStat.f_blocks * mStat.f_frsize; 156 } 157 } 158