Search in sources :

Example 56 with StatFs

use of android.os.StatFs in project android_frameworks_base by DirtyUnicorns.

the class AppCacheTest method testAppCacheClear.

//@LargeTest
public void testAppCacheClear() {
    String dataDir = "/data/data";
    StatFs st = new StatFs(dataDir);
    long blkSize = st.getBlockSize();
    long totBlks = st.getBlockCount();
    long availableBlks = st.getFreeBlocks();
    long thresholdBlks = (totBlks * THRESHOLD) / 100L;
    String testDirName = "testdir";
    //create directory in cache
    File testDir = new File(mContext.getCacheDir(), testDirName);
    testDir.mkdirs();
    byte[] buffer = getBuffer();
    int i = 1;
    if (localLOGV)
        Log.i(TAG, "availableBlks=" + availableBlks + ", thresholdBlks=" + thresholdBlks);
    long createdFileBlks = 0;
    int imax = 300;
    while ((availableBlks > thresholdBlks) && (i < imax)) {
        File testFile = new File(testDir, "testFile" + i + ".txt");
        if (localLOGV)
            Log.i(TAG, "Creating " + i + "th test file " + testFile);
        int jmax = i;
        i++;
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(testFile);
        } catch (FileNotFoundException e) {
            Log.i(TAG, "Failed creating test file:" + testFile);
            continue;
        }
        boolean err = false;
        for (int j = 1; j <= jmax; j++) {
            try {
                fos.write(buffer);
            } catch (IOException e) {
                Log.i(TAG, "Failed to write to file:" + testFile);
                err = true;
            }
        }
        try {
            fos.close();
        } catch (IOException e) {
            Log.i(TAG, "Failed closing file:" + testFile);
        }
        if (err) {
            continue;
        }
        createdFileBlks += getFileNumBlocks(testFile.length(), blkSize);
        st.restat(dataDir);
        availableBlks = st.getFreeBlocks();
    }
    st.restat(dataDir);
    long availableBytes = st.getFreeBlocks() * blkSize;
    long shouldFree = (ACTUAL_THRESHOLD - THRESHOLD) * totBlks;
    //wait for some time and confirm cache is deleted
    try {
        Log.i(TAG, "Sleeping for 2 minutes...");
        Thread.sleep(2 * 60 * 1000);
    } catch (InterruptedException e) {
        fail("Exception when sleeping " + e);
    }
    boolean removedFlag = false;
    long existingFileBlks = 0;
    for (int k = 1; k < i; k++) {
        File testFile = new File(testDir, "testFile" + k + ".txt");
        if (!testFile.exists()) {
            removedFlag = true;
            if (localLOGV)
                Log.i(TAG, testFile + " removed");
        } else {
            existingFileBlks += getFileNumBlocks(testFile.length(), blkSize);
        }
    }
    if (localLOGV)
        Log.i(TAG, "createdFileBlks=" + createdFileBlks + ", existingFileBlks=" + existingFileBlks);
    long fileSize = createdFileBlks - existingFileBlks;
    //verify fileSize number of bytes have been cleared from cache
    if (localLOGV)
        Log.i(TAG, "deletedFileBlks=" + fileSize + " shouldFreeBlks=" + shouldFree);
    if ((fileSize > (shouldFree - blkSize) && (fileSize < (shouldFree + blkSize)))) {
        Log.i(TAG, "passed");
    }
    assertTrue("Files should have been removed", removedFlag);
}
Also used : StatFs(android.os.StatFs) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File)

Example 57 with StatFs

use of android.os.StatFs in project android_frameworks_base by DirtyUnicorns.

the class AppCacheTest method testFreeApplicationCacheSomeFiles.

public void testFreeApplicationCacheSomeFiles() throws Exception {
    StatFs st = new StatFs("/data");
    long blks1 = getFreeStorageBlks(st);
    File cacheDir = mContext.getCacheDir();
    assertNotNull(cacheDir);
    createTestFiles1(cacheDir, "testtmpdir", 5);
    long blks2 = getFreeStorageBlks(st);
    Log.i(TAG, "blk1=" + blks1 + ", blks2=" + blks2);
    long diff = (blks1 - blks2 - 2);
    if (!invokePMFreeApplicationCache(diff * st.getBlockSize())) {
        fail("Could not successfully invoke PackageManager free app cache API");
    }
    long blks3 = getFreeStorageBlks(st);
    //blks3 should be greater than blks2 and less than blks1
    if (!((blks3 <= blks1) && (blks3 >= blks2))) {
        failStr("Expected " + (blks1 - blks2) + " number of blocks to be freed but freed only " + (blks1 - blks3));
    }
}
Also used : StatFs(android.os.StatFs) File(java.io.File)

Example 58 with StatFs

use of android.os.StatFs in project android_frameworks_base by DirtyUnicorns.

the class AppCacheTest method testFreeApplicationCacheAllFiles.

@LargeTest
// Failing.
@Suppress
public void testFreeApplicationCacheAllFiles() throws Exception {
    boolean TRACKING = true;
    StatFs st = new StatFs("/data");
    long blks1 = getFreeStorageBlks(st);
    long availableMem = getFreeStorageSize(st);
    File cacheDir = mContext.getCacheDir();
    assertNotNull(cacheDir);
    createTestFiles1(cacheDir, "testtmpdir", 5);
    long blks2 = getFreeStorageBlks(st);
    if (localLOGV || TRACKING)
        Log.i(TAG, "blk1=" + blks1 + ", blks2=" + blks2);
    //this should free up the test files that were created earlier
    if (!invokePMFreeApplicationCache(availableMem)) {
        fail("Could not successfully invoke PackageManager free app cache API");
    }
    long blks3 = getFreeStorageBlks(st);
    if (localLOGV || TRACKING)
        Log.i(TAG, "blks3=" + blks3);
    verifyTestFiles1(cacheDir, "testtmpdir", 5);
}
Also used : StatFs(android.os.StatFs) File(java.io.File) Suppress(android.test.suitebuilder.annotation.Suppress) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 59 with StatFs

use of android.os.StatFs in project android_frameworks_base by DirtyUnicorns.

the class DropBoxManagerService method init.

///////////////////////////////////////////////////////////////////////////
/** If never run before, scans disk contents to build in-memory tracking data. */
private synchronized void init() throws IOException {
    if (mStatFs == null) {
        if (!mDropBoxDir.isDirectory() && !mDropBoxDir.mkdirs()) {
            throw new IOException("Can't mkdir: " + mDropBoxDir);
        }
        try {
            mStatFs = new StatFs(mDropBoxDir.getPath());
            mBlockSize = mStatFs.getBlockSize();
        } catch (IllegalArgumentException e) {
            // StatFs throws this on error
            throw new IOException("Can't statfs: " + mDropBoxDir);
        }
    }
    if (mAllFiles == null) {
        File[] files = mDropBoxDir.listFiles();
        if (files == null)
            throw new IOException("Can't list files: " + mDropBoxDir);
        mAllFiles = new FileList();
        mFilesByTag = new HashMap<String, FileList>();
        // Scan pre-existing files.
        for (File file : files) {
            if (file.getName().endsWith(".tmp")) {
                Slog.i(TAG, "Cleaning temp file: " + file);
                file.delete();
                continue;
            }
            EntryFile entry = new EntryFile(file, mBlockSize);
            if (entry.tag == null) {
                Slog.w(TAG, "Unrecognized file: " + file);
                continue;
            } else if (entry.timestampMillis == 0) {
                Slog.w(TAG, "Invalid filename: " + file);
                file.delete();
                continue;
            }
            enrollEntry(entry);
        }
    }
}
Also used : StatFs(android.os.StatFs) IOException(java.io.IOException) File(java.io.File)

Example 60 with StatFs

use of android.os.StatFs in project PhotoNoter by yydcdut.

the class FilePathUtils method getSDCardStorage.

public static long[] getSDCardStorage() {
    long[] storage = new long[] { -1, -1 };
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        StatFs sf = new StatFs(Environment.getExternalStorageDirectory().getPath());
        long blockSize = 0;
        long availCount = 0;
        long totalCount = 0;
        if (Build.VERSION.SDK_INT < 18) {
            blockSize = sf.getBlockSize();
            availCount = sf.getAvailableBlocks();
            totalCount = sf.getBlockCount();
            //单位MB;
            storage[0] = availCount * blockSize / 1024 / 1024;
            storage[1] = totalCount * blockSize / 1024 / 1024;
        } else {
            blockSize = sf.getBlockSizeLong();
            availCount = sf.getAvailableBlocksLong();
            totalCount = sf.getBlockCountLong();
            //单位MB;
            storage[0] = availCount * blockSize / 1024 / 1024;
            storage[1] = totalCount * blockSize / 1024 / 1024;
        }
    }
    return storage;
}
Also used : StatFs(android.os.StatFs)

Aggregations

StatFs (android.os.StatFs)196 File (java.io.File)120 IOException (java.io.IOException)30 FileOutputStream (java.io.FileOutputStream)17 TargetApi (android.annotation.TargetApi)15 LargeTest (android.test.suitebuilder.annotation.LargeTest)12 FileNotFoundException (java.io.FileNotFoundException)11 ContentResolver (android.content.ContentResolver)10 DropBoxManager (android.os.DropBoxManager)10 TextView (android.widget.TextView)10 DropBoxManagerService (com.android.server.DropBoxManagerService)10 Test (org.junit.Test)7 PendingIntent (android.app.PendingIntent)6 Intent (android.content.Intent)6 IntentFilter (android.content.IntentFilter)6 Context (android.content.Context)5 Suppress (android.test.suitebuilder.annotation.Suppress)5 Button (android.widget.Button)5 SuppressLint (android.annotation.SuppressLint)3 SDCardInfo (com.yzy.supercleanmaster.model.SDCardInfo)3