blob: 881d0b4d9c8e2395a3fb5a5756fdef88830fb306 [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
Andrei Onea24ec3212019-03-15 17:35:05 +000019import android.annotation.UnsupportedAppUsage;
Elliott Hughes34385d32014-04-28 11:11:32 -070020import android.system.ErrnoException;
21import android.system.Os;
22import android.system.StructStatVfs;
Kenny Rootbdd23ae2012-08-16 10:43:47 -070023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024/**
Kenny Rootbdd23ae2012-08-16 10:43:47 -070025 * Retrieve overall information about the space on a filesystem. This is a
Elliott Hughesbefd0b12013-07-09 14:46:18 -070026 * wrapper for Unix statvfs().
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027 */
28public class StatFs {
Andrei Onea24ec3212019-03-15 17:35:05 +000029 @UnsupportedAppUsage
Elliott Hughesbefd0b12013-07-09 14:46:18 -070030 private StructStatVfs mStat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
32 /**
Kenny Rootbdd23ae2012-08-16 10:43:47 -070033 * 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.
Tobias Thierer0ad48dc2016-08-02 16:41:11 +010039 *
40 * @throws IllegalArgumentException if the file system access fails
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 */
Kenny Rootbdd23ae2012-08-16 10:43:47 -070042 public StatFs(String path) {
43 mStat = doStat(path);
44 }
45
Tobias Thierer0ad48dc2016-08-02 16:41:11 +010046 /**
47 * @throws IllegalArgumentException if the file system access fails
48 */
Elliott Hughesbefd0b12013-07-09 14:46:18 -070049 private static StructStatVfs doStat(String path) {
Kenny Rootbdd23ae2012-08-16 10:43:47 -070050 try {
Elliott Hughes34385d32014-04-28 11:11:32 -070051 return Os.statvfs(path);
Kenny Rootbdd23ae2012-08-16 10:43:47 -070052 } catch (ErrnoException e) {
53 throw new IllegalArgumentException("Invalid path: " + path, e);
54 }
55 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
57 /**
Kenny Rootbdd23ae2012-08-16 10:43:47 -070058 * 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.
Tobias Thierer0ad48dc2016-08-02 16:41:11 +010061 *
62 * @throws IllegalArgumentException if the file system access fails
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 */
Kenny Rootbdd23ae2012-08-16 10:43:47 -070064 public void restat(String path) {
65 mStat = doStat(path);
66 }
67
68 /**
Jeff Sharkeyb81440b2013-04-18 16:00:04 -070069 * @deprecated Use {@link #getBlockSizeLong()} instead.
Kenny Rootbdd23ae2012-08-16 10:43:47 -070070 */
Jeff Sharkeyb81440b2013-04-18 16:00:04 -070071 @Deprecated
Kenny Rootbdd23ae2012-08-16 10:43:47 -070072 public int getBlockSize() {
Jeff Sharkeydafb17e2017-04-02 23:24:40 -060073 return (int) mStat.f_frsize;
Kenny Rootbdd23ae2012-08-16 10:43:47 -070074 }
75
76 /**
Jeff Sharkeyb65ce572013-02-13 18:31:00 -080077 * The size, in bytes, of a block on the file system. This corresponds to
Jeff Sharkeydafb17e2017-04-02 23:24:40 -060078 * the Unix {@code statvfs.f_frsize} field.
Jeff Sharkeyb65ce572013-02-13 18:31:00 -080079 */
80 public long getBlockSizeLong() {
Jeff Sharkeydafb17e2017-04-02 23:24:40 -060081 return mStat.f_frsize;
Jeff Sharkeyb65ce572013-02-13 18:31:00 -080082 }
83
84 /**
Jeff Sharkeyb81440b2013-04-18 16:00:04 -070085 * @deprecated Use {@link #getBlockCountLong()} instead.
Kenny Rootbdd23ae2012-08-16 10:43:47 -070086 */
Jeff Sharkeyb81440b2013-04-18 16:00:04 -070087 @Deprecated
Kenny Rootbdd23ae2012-08-16 10:43:47 -070088 public int getBlockCount() {
89 return (int) mStat.f_blocks;
90 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92 /**
Jeff Sharkeyb81440b2013-04-18 16:00:04 -070093 * The total number of blocks on the file system. This corresponds to the
Elliott Hughesbefd0b12013-07-09 14:46:18 -070094 * Unix {@code statvfs.f_blocks} field.
Jeff Sharkeyb65ce572013-02-13 18:31:00 -080095 */
96 public long getBlockCountLong() {
97 return mStat.f_blocks;
98 }
99
100 /**
Jeff Sharkeyb81440b2013-04-18 16:00:04 -0700101 * @deprecated Use {@link #getFreeBlocksLong()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 */
Jeff Sharkeyb81440b2013-04-18 16:00:04 -0700103 @Deprecated
Kenny Rootbdd23ae2012-08-16 10:43:47 -0700104 public int getFreeBlocks() {
105 return (int) mStat.f_bfree;
106 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
108 /**
Jeff Sharkeyb65ce572013-02-13 18:31:00 -0800109 * The total number of blocks that are free on the file system, including
110 * reserved blocks (that are not available to normal applications). This
Elliott Hughesbefd0b12013-07-09 14:46:18 -0700111 * corresponds to the Unix {@code statvfs.f_bfree} field. Most applications
Jeff Sharkeyb65ce572013-02-13 18:31:00 -0800112 * will want to use {@link #getAvailableBlocks()} instead.
113 */
114 public long getFreeBlocksLong() {
115 return mStat.f_bfree;
116 }
117
118 /**
Jeff Sharkeyb81440b2013-04-18 16:00:04 -0700119 * 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.
Jeff Sharkeyb65ce572013-02-13 18:31:00 -0800122 */
123 public long getFreeBytes() {
Jeff Sharkeydafb17e2017-04-02 23:24:40 -0600124 return mStat.f_bfree * mStat.f_frsize;
Jeff Sharkeyb65ce572013-02-13 18:31:00 -0800125 }
126
127 /**
Jeff Sharkeyb81440b2013-04-18 16:00:04 -0700128 * @deprecated Use {@link #getAvailableBlocksLong()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 */
Jeff Sharkeyb81440b2013-04-18 16:00:04 -0700130 @Deprecated
Kenny Rootbdd23ae2012-08-16 10:43:47 -0700131 public int getAvailableBlocks() {
132 return (int) mStat.f_bavail;
133 }
Jeff Sharkeyb65ce572013-02-13 18:31:00 -0800134
135 /**
136 * The number of blocks that are free on the file system and available to
Elliott Hughesbefd0b12013-07-09 14:46:18 -0700137 * applications. This corresponds to the Unix {@code statvfs.f_bavail} field.
Jeff Sharkeyb65ce572013-02-13 18:31:00 -0800138 */
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() {
Jeff Sharkeydafb17e2017-04-02 23:24:40 -0600148 return mStat.f_bavail * mStat.f_frsize;
Jeff Sharkeyb65ce572013-02-13 18:31:00 -0800149 }
Jeff Sharkeyb81440b2013-04-18 16:00:04 -0700150
151 /**
152 * The total number of bytes supported by the file system.
153 */
154 public long getTotalBytes() {
Jeff Sharkeydafb17e2017-04-02 23:24:40 -0600155 return mStat.f_blocks * mStat.f_frsize;
Jeff Sharkeyb81440b2013-04-18 16:00:04 -0700156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157}