19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.os;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1934385d352da19805ae948215e2edbeedd16b7941Elliott Hughesimport android.system.ErrnoException;
2034385d352da19805ae948215e2edbeedd16b7941Elliott Hughesimport android.system.Os;
2134385d352da19805ae948215e2edbeedd16b7941Elliott Hughesimport android.system.StructStatVfs;
22bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
24bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root * Retrieve overall information about the space on a filesystem. This is a
25befd0b1bdfb4492425944a1b7fcf6d10eaed9b4cElliott Hughes * wrapper for Unix statvfs().
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class StatFs {
28befd0b1bdfb4492425944a1b7fcf6d10eaed9b4cElliott Hughes    private StructStatVfs mStat;
29bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
31bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root     * Construct a new StatFs for looking at the stats of the filesystem at
32bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root     * {@code path}. Upon construction, the stat of the file system will be
33bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root     * performed, and the values retrieved available from the methods on this
34bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root     * class.
35bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root     *
36bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root     * @param path path in the desired file system to stat.
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
38bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root    public StatFs(String path) {
39bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root        mStat = doStat(path);
40bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root    }
41bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root
42befd0b1bdfb4492425944a1b7fcf6d10eaed9b4cElliott Hughes    private static StructStatVfs doStat(String path) {
43bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root        try {
4434385d352da19805ae948215e2edbeedd16b7941Elliott Hughes            return Os.statvfs(path);
45bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root        } catch (ErrnoException e) {
46bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root            throw new IllegalArgumentException("Invalid path: " + path, e);
47bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root        }
48bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root    }
49bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
51bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root     * Perform a restat of the file system referenced by this object. This is
52bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root     * the same as re-constructing the object with the same file system path,
53bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root     * and the new stat values are available upon return.
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
55bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root    public void restat(String path) {
56bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root        mStat = doStat(path);
57bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root    }
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
60b81440bd9bcb22ba93c2bfec4e3c2da39a57c95dJeff Sharkey     * @deprecated Use {@link #getBlockSizeLong()} instead.
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
62b81440bd9bcb22ba93c2bfec4e3c2da39a57c95dJeff Sharkey    @Deprecated
63bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root    public int getBlockSize() {
64bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root        return (int) mStat.f_bsize;
65bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root    }
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
68b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey     * The size, in bytes, of a block on the file system. This corresponds to
69e16a21c6b4f108813e5f5e00f30e0ff075de504cElliott Hughes     * the Unix {@code statvfs.f_bsize} field.
70b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey     */
71b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey    public long getBlockSizeLong() {
72b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey        return mStat.f_bsize;
73b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey    }
74b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey
75b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey    /**
76b81440bd9bcb22ba93c2bfec4e3c2da39a57c95dJeff Sharkey     * @deprecated Use {@link #getBlockCountLong()} instead.
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
78b81440bd9bcb22ba93c2bfec4e3c2da39a57c95dJeff Sharkey    @Deprecated
79bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root    public int getBlockCount() {
80bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root        return (int) mStat.f_blocks;
81bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root    }
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
84b81440bd9bcb22ba93c2bfec4e3c2da39a57c95dJeff Sharkey     * The total number of blocks on the file system. This corresponds to the
85e16a21c6b4f108813e5f5e00f30e0ff075de504cElliott Hughes     * Unix {@code statvfs.f_blocks} field.
86b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey     */
87b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey    public long getBlockCountLong() {
88b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey        return mStat.f_blocks;
89b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey    }
90b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey
91b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey    /**
92b81440bd9bcb22ba93c2bfec4e3c2da39a57c95dJeff Sharkey     * @deprecated Use {@link #getFreeBlocksLong()} instead.
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
94b81440bd9bcb22ba93c2bfec4e3c2da39a57c95dJeff Sharkey    @Deprecated
95bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root    public int getFreeBlocks() {
96bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root        return (int) mStat.f_bfree;
97bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root    }
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
100b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey     * The total number of blocks that are free on the file system, including
101b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey     * reserved blocks (that are not available to normal applications). This
102befd0b1bdfb4492425944a1b7fcf6d10eaed9b4cElliott Hughes     * corresponds to the Unix {@code statvfs.f_bfree} field. Most applications
103b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey     * will want to use {@link #getAvailableBlocks()} instead.
104b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey     */
105b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey    public long getFreeBlocksLong() {
106b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey        return mStat.f_bfree;
107b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey    }
108b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey
109b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey    /**
110b81440bd9bcb22ba93c2bfec4e3c2da39a57c95dJeff Sharkey     * The number of bytes that are free on the file system, including reserved
111b81440bd9bcb22ba93c2bfec4e3c2da39a57c95dJeff Sharkey     * blocks (that are not available to normal applications). Most applications
112b81440bd9bcb22ba93c2bfec4e3c2da39a57c95dJeff Sharkey     * will want to use {@link #getAvailableBytes()} instead.
113b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey     */
114b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey    public long getFreeBytes() {
115b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey        return mStat.f_bfree * mStat.f_bsize;
116b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey    }
117b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey
118b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey    /**
119b81440bd9bcb22ba93c2bfec4e3c2da39a57c95dJeff Sharkey     * @deprecated Use {@link #getAvailableBlocksLong()} instead.
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
121b81440bd9bcb22ba93c2bfec4e3c2da39a57c95dJeff Sharkey    @Deprecated
122bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root    public int getAvailableBlocks() {
123bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root        return (int) mStat.f_bavail;
124bdd23ae9f5ea2b3e0720e76711345cad0fa8d6ddKenny Root    }
125b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey
126b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey    /**
127b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey     * The number of blocks that are free on the file system and available to
128e16a21c6b4f108813e5f5e00f30e0ff075de504cElliott Hughes     * applications. This corresponds to the Unix {@code statvfs.f_bavail} field.
129b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey     */
130b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey    public long getAvailableBlocksLong() {
131b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey        return mStat.f_bavail;
132b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey    }
133b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey
134b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey    /**
135b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey     * The number of bytes that are free on the file system and available to
136b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey     * applications.
137b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey     */
138b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey    public long getAvailableBytes() {
139b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey        return mStat.f_bavail * mStat.f_bsize;
140b65ce57675ecd983c85dd4d755fe0167f33ecc87Jeff Sharkey    }
141b81440bd9bcb22ba93c2bfec4e3c2da39a57c95dJeff Sharkey
142b81440bd9bcb22ba93c2bfec4e3c2da39a57c95dJeff Sharkey    /**
143b81440bd9bcb22ba93c2bfec4e3c2da39a57c95dJeff Sharkey     * The total number of bytes supported by the file system.
144b81440bd9bcb22ba93c2bfec4e3c2da39a57c95dJeff Sharkey     */
145b81440bd9bcb22ba93c2bfec4e3c2da39a57c95dJeff Sharkey    public long getTotalBytes() {
146b81440bd9bcb22ba93c2bfec4e3c2da39a57c95dJeff Sharkey        return mStat.f_blocks * mStat.f_bsize;
147b81440bd9bcb22ba93c2bfec4e3c2da39a57c95dJeff Sharkey    }
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
149