blob: 13e9a15eedb6367a268642fbe08aeb1f66a49162 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.os;
18
Elliott Hughes34385d32014-04-28 11:11:32 -070019import android.system.ErrnoException;
20import android.system.Os;
21import android.system.StructStatVfs;
Kenny Rootbdd23ae2012-08-16 10:43:47 -070022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023/**
Kenny Rootbdd23ae2012-08-16 10:43:47 -070024 * Retrieve overall information about the space on a filesystem. This is a
Elliott Hughesbefd0b12013-07-09 14:46:18 -070025 * wrapper for Unix statvfs().
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026 */
27public class StatFs {
Elliott Hughesbefd0b12013-07-09 14:46:18 -070028 private StructStatVfs mStat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
30 /**
Kenny Rootbdd23ae2012-08-16 10:43:47 -070031 * 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 Project9066cfe2009-03-03 19:31:44 -080037 */
Kenny Rootbdd23ae2012-08-16 10:43:47 -070038 public StatFs(String path) {
39 mStat = doStat(path);
40 }
41
Elliott Hughesbefd0b12013-07-09 14:46:18 -070042 private static StructStatVfs doStat(String path) {
Kenny Rootbdd23ae2012-08-16 10:43:47 -070043 try {
Elliott Hughes34385d32014-04-28 11:11:32 -070044 return Os.statvfs(path);
Kenny Rootbdd23ae2012-08-16 10:43:47 -070045 } catch (ErrnoException e) {
46 throw new IllegalArgumentException("Invalid path: " + path, e);
47 }
48 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
50 /**
Kenny Rootbdd23ae2012-08-16 10:43:47 -070051 * 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 Project9066cfe2009-03-03 19:31:44 -080054 */
Kenny Rootbdd23ae2012-08-16 10:43:47 -070055 public void restat(String path) {
56 mStat = doStat(path);
57 }
58
59 /**
Jeff Sharkeyb81440b2013-04-18 16:00:04 -070060 * @deprecated Use {@link #getBlockSizeLong()} instead.
Kenny Rootbdd23ae2012-08-16 10:43:47 -070061 */
Jeff Sharkeyb81440b2013-04-18 16:00:04 -070062 @Deprecated
Kenny Rootbdd23ae2012-08-16 10:43:47 -070063 public int getBlockSize() {
64 return (int) mStat.f_bsize;
65 }
66
67 /**
Jeff Sharkeyb65ce572013-02-13 18:31:00 -080068 * The size, in bytes, of a block on the file system. This corresponds to
Elliott Hughesbefd0b12013-07-09 14:46:18 -070069 * the Unix {@code statvfs.f_bsize} field.
Jeff Sharkeyb65ce572013-02-13 18:31:00 -080070 */
71 public long getBlockSizeLong() {
72 return mStat.f_bsize;
73 }
74
75 /**
Jeff Sharkeyb81440b2013-04-18 16:00:04 -070076 * @deprecated Use {@link #getBlockCountLong()} instead.
Kenny Rootbdd23ae2012-08-16 10:43:47 -070077 */
Jeff Sharkeyb81440b2013-04-18 16:00:04 -070078 @Deprecated
Kenny Rootbdd23ae2012-08-16 10:43:47 -070079 public int getBlockCount() {
80 return (int) mStat.f_blocks;
81 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
83 /**
Jeff Sharkeyb81440b2013-04-18 16:00:04 -070084 * The total number of blocks on the file system. This corresponds to the
Elliott Hughesbefd0b12013-07-09 14:46:18 -070085 * Unix {@code statvfs.f_blocks} field.
Jeff Sharkeyb65ce572013-02-13 18:31:00 -080086 */
87 public long getBlockCountLong() {
88 return mStat.f_blocks;
89 }
90
91 /**
Jeff Sharkeyb81440b2013-04-18 16:00:04 -070092 * @deprecated Use {@link #getFreeBlocksLong()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 */
Jeff Sharkeyb81440b2013-04-18 16:00:04 -070094 @Deprecated
Kenny Rootbdd23ae2012-08-16 10:43:47 -070095 public int getFreeBlocks() {
96 return (int) mStat.f_bfree;
97 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098
99 /**
Jeff Sharkeyb65ce572013-02-13 18:31:00 -0800100 * 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 Hughesbefd0b12013-07-09 14:46:18 -0700102 * corresponds to the Unix {@code statvfs.f_bfree} field. Most applications
Jeff Sharkeyb65ce572013-02-13 18:31:00 -0800103 * will want to use {@link #getAvailableBlocks()} instead.
104 */
105 public long getFreeBlocksLong() {
106 return mStat.f_bfree;
107 }
108
109 /**
Jeff Sharkeyb81440b2013-04-18 16:00:04 -0700110 * 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 Sharkeyb65ce572013-02-13 18:31:00 -0800113 */
114 public long getFreeBytes() {
115 return mStat.f_bfree * mStat.f_bsize;
116 }
117
118 /**
Jeff Sharkeyb81440b2013-04-18 16:00:04 -0700119 * @deprecated Use {@link #getAvailableBlocksLong()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 */
Jeff Sharkeyb81440b2013-04-18 16:00:04 -0700121 @Deprecated
Kenny Rootbdd23ae2012-08-16 10:43:47 -0700122 public int getAvailableBlocks() {
123 return (int) mStat.f_bavail;
124 }
Jeff Sharkeyb65ce572013-02-13 18:31:00 -0800125
126 /**
127 * The number of blocks that are free on the file system and available to
Elliott Hughesbefd0b12013-07-09 14:46:18 -0700128 * applications. This corresponds to the Unix {@code statvfs.f_bavail} field.
Jeff Sharkeyb65ce572013-02-13 18:31:00 -0800129 */
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 Sharkeyb81440b2013-04-18 16:00:04 -0700141
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 Project9066cfe2009-03-03 19:31:44 -0800148}