Home | History | Annotate | Download | only in os
      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 libcore.io.ErrnoException;
     20 import libcore.io.Libcore;
     21 import libcore.io.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     public StatFs(String path) {
     39         mStat = doStat(path);
     40     }
     41 
     42     private static StructStatVfs doStat(String path) {
     43         try {
     44             return Libcore.os.statvfs(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      * @deprecated Use {@link #getBlockSizeLong()} instead.
     61      */
     62     @Deprecated
     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 statvfs.f_bsize} field.
     70      */
     71     public long getBlockSizeLong() {
     72         return mStat.f_bsize;
     73     }
     74 
     75     /**
     76      * @deprecated Use {@link #getBlockCountLong()} instead.
     77      */
     78     @Deprecated
     79     public int getBlockCount() {
     80         return (int) mStat.f_blocks;
     81     }
     82 
     83     /**
     84      * The total number of blocks on the file system. This corresponds to the
     85      * Unix {@code statvfs.f_blocks} field.
     86      */
     87     public long getBlockCountLong() {
     88         return mStat.f_blocks;
     89     }
     90 
     91     /**
     92      * @deprecated Use {@link #getFreeBlocksLong()} instead.
     93      */
     94     @Deprecated
     95     public int getFreeBlocks() {
     96         return (int) mStat.f_bfree;
     97     }
     98 
     99     /**
    100      * The total number of blocks that are free on the file system, including
    101      * reserved blocks (that are not available to normal applications). This
    102      * corresponds to the Unix {@code statvfs.f_bfree} field. Most applications
    103      * will want to use {@link #getAvailableBlocks()} instead.
    104      */
    105     public long getFreeBlocksLong() {
    106         return mStat.f_bfree;
    107     }
    108 
    109     /**
    110      * 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.
    113      */
    114     public long getFreeBytes() {
    115         return mStat.f_bfree * mStat.f_bsize;
    116     }
    117 
    118     /**
    119      * @deprecated Use {@link #getAvailableBlocksLong()} instead.
    120      */
    121     @Deprecated
    122     public int getAvailableBlocks() {
    123         return (int) mStat.f_bavail;
    124     }
    125 
    126     /**
    127      * The number of blocks that are free on the file system and available to
    128      * applications. This corresponds to the Unix {@code statvfs.f_bavail} field.
    129      */
    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     }
    141 
    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     }
    148 }
    149