• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.system.ErrnoException;
20 import android.system.Os;
21 import android.system.StructStatVfs;
22 
23 /**
24  * Retrieve overall information about the space on a filesystem. This is a
25  * wrapper for Unix statvfs().
26  */
27 public class StatFs {
28     private StructStatVfs 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      * @throws IllegalArgumentException if the file system access fails
39      */
StatFs(String path)40     public StatFs(String path) {
41         mStat = doStat(path);
42     }
43 
44     /**
45      * @throws IllegalArgumentException if the file system access fails
46      */
doStat(String path)47     private static StructStatVfs doStat(String path) {
48         try {
49             return Os.statvfs(path);
50         } catch (ErrnoException e) {
51             throw new IllegalArgumentException("Invalid path: " + path, e);
52         }
53     }
54 
55     /**
56      * Perform a restat of the file system referenced by this object. This is
57      * the same as re-constructing the object with the same file system path,
58      * and the new stat values are available upon return.
59      *
60      * @throws IllegalArgumentException if the file system access fails
61      */
restat(String path)62     public void restat(String path) {
63         mStat = doStat(path);
64     }
65 
66     /**
67      * @deprecated Use {@link #getBlockSizeLong()} instead.
68      */
69     @Deprecated
getBlockSize()70     public int getBlockSize() {
71         return (int) mStat.f_frsize;
72     }
73 
74     /**
75      * The size, in bytes, of a block on the file system. This corresponds to
76      * the Unix {@code statvfs.f_frsize} field.
77      */
getBlockSizeLong()78     public long getBlockSizeLong() {
79         return mStat.f_frsize;
80     }
81 
82     /**
83      * @deprecated Use {@link #getBlockCountLong()} instead.
84      */
85     @Deprecated
getBlockCount()86     public int getBlockCount() {
87         return (int) mStat.f_blocks;
88     }
89 
90     /**
91      * The total number of blocks on the file system. This corresponds to the
92      * Unix {@code statvfs.f_blocks} field.
93      */
getBlockCountLong()94     public long getBlockCountLong() {
95         return mStat.f_blocks;
96     }
97 
98     /**
99      * @deprecated Use {@link #getFreeBlocksLong()} instead.
100      */
101     @Deprecated
getFreeBlocks()102     public int getFreeBlocks() {
103         return (int) mStat.f_bfree;
104     }
105 
106     /**
107      * The total number of blocks that are free on the file system, including
108      * reserved blocks (that are not available to normal applications). This
109      * corresponds to the Unix {@code statvfs.f_bfree} field. Most applications
110      * will want to use {@link #getAvailableBlocks()} instead.
111      */
getFreeBlocksLong()112     public long getFreeBlocksLong() {
113         return mStat.f_bfree;
114     }
115 
116     /**
117      * The number of bytes that are free on the file system, including reserved
118      * blocks (that are not available to normal applications). Most applications
119      * will want to use {@link #getAvailableBytes()} instead.
120      */
getFreeBytes()121     public long getFreeBytes() {
122         return mStat.f_bfree * mStat.f_frsize;
123     }
124 
125     /**
126      * @deprecated Use {@link #getAvailableBlocksLong()} instead.
127      */
128     @Deprecated
getAvailableBlocks()129     public int getAvailableBlocks() {
130         return (int) mStat.f_bavail;
131     }
132 
133     /**
134      * The number of blocks that are free on the file system and available to
135      * applications. This corresponds to the Unix {@code statvfs.f_bavail} field.
136      */
getAvailableBlocksLong()137     public long getAvailableBlocksLong() {
138         return mStat.f_bavail;
139     }
140 
141     /**
142      * The number of bytes that are free on the file system and available to
143      * applications.
144      */
getAvailableBytes()145     public long getAvailableBytes() {
146         return mStat.f_bavail * mStat.f_frsize;
147     }
148 
149     /**
150      * The total number of bytes supported by the file system.
151      */
getTotalBytes()152     public long getTotalBytes() {
153         return mStat.f_blocks * mStat.f_frsize;
154     }
155 }
156