StatFs.java revision b65ce57675ecd983c85dd4d755fe0167f33ecc87
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
17package android.os;
18
19import libcore.io.ErrnoException;
20import libcore.io.Libcore;
21import libcore.io.StructStatFs;
22
23/**
24 * Retrieve overall information about the space on a filesystem. This is a
25 * wrapper for Unix statfs().
26 */
27public class StatFs {
28    private StructStatFs mStat;
29
30    /**
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.
37     */
38    public StatFs(String path) {
39        mStat = doStat(path);
40    }
41
42    private static StructStatFs doStat(String path) {
43        try {
44            return Libcore.os.statfs(path);
45        } catch (ErrnoException e) {
46            throw new IllegalArgumentException("Invalid path: " + path, e);
47        }
48    }
49
50    /**
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.
54     */
55    public void restat(String path) {
56        mStat = doStat(path);
57    }
58
59    /**
60     * The size, in bytes, of a block on the file system. This corresponds to
61     * the Unix {@code statfs.f_bsize} field.
62     */
63    public int getBlockSize() {
64        return (int) mStat.f_bsize;
65    }
66
67    /**
68     * The size, in bytes, of a block on the file system. This corresponds to
69     * the Unix {@code statfs.f_bsize} field.
70     */
71    public long getBlockSizeLong() {
72        return mStat.f_bsize;
73    }
74
75    /**
76     * The total number of blocks on the file system. This corresponds to the
77     * Unix {@code statfs.f_blocks} field.
78     */
79    public int getBlockCount() {
80        return (int) mStat.f_blocks;
81    }
82
83    /**
84     * The size, in bytes, of a block on the file system. This corresponds to
85     * the Unix {@code statfs.f_bsize} field.
86     */
87    public long getBlockCountLong() {
88        return mStat.f_blocks;
89    }
90
91    /**
92     * The total number of blocks that are free on the file system, including
93     * reserved blocks (that are not available to normal applications). This
94     * corresponds to the Unix {@code statfs.f_bfree} field. Most applications
95     * will want to use {@link #getAvailableBlocks()} instead.
96     */
97    public int getFreeBlocks() {
98        return (int) mStat.f_bfree;
99    }
100
101    /**
102     * The total number of blocks that are free on the file system, including
103     * reserved blocks (that are not available to normal applications). This
104     * corresponds to the Unix {@code statfs.f_bfree} field. Most applications
105     * will want to use {@link #getAvailableBlocks()} instead.
106     */
107    public long getFreeBlocksLong() {
108        return mStat.f_bfree;
109    }
110
111    /**
112     * The number of bytes that are free on the file system, including
113     * reserved blocks (that are not available to normal applications).
114     */
115    public long getFreeBytes() {
116        return mStat.f_bfree * mStat.f_bsize;
117    }
118
119    /**
120     * The number of blocks that are free on the file system and available to
121     * applications. This corresponds to the Unix {@code statfs.f_bavail} field.
122     */
123    public int getAvailableBlocks() {
124        return (int) mStat.f_bavail;
125    }
126
127    /**
128     * The number of blocks that are free on the file system and available to
129     * applications. This corresponds to the Unix {@code statfs.f_bavail} field.
130     */
131    public long getAvailableBlocksLong() {
132        return mStat.f_bavail;
133    }
134
135    /**
136     * The number of bytes that are free on the file system and available to
137     * applications.
138     */
139    public long getAvailableBytes() {
140        return mStat.f_bavail * mStat.f_bsize;
141    }
142}
143