use of android.os.StatFs in project android_frameworks_base by crdroidandroid.
the class DiskStatsService method reportFreeSpace.
private void reportFreeSpace(File path, String name, PrintWriter pw) {
try {
StatFs statfs = new StatFs(path.getPath());
long bsize = statfs.getBlockSize();
long avail = statfs.getAvailableBlocks();
long total = statfs.getBlockCount();
if (bsize <= 0 || total <= 0) {
throw new IllegalArgumentException("Invalid stat: bsize=" + bsize + " avail=" + avail + " total=" + total);
}
pw.print(name);
pw.print("-Free: ");
pw.print(avail * bsize / 1024);
pw.print("K / ");
pw.print(total * bsize / 1024);
pw.print("K total = ");
pw.print(avail * 100 / total);
pw.println("% free");
} catch (IllegalArgumentException e) {
pw.print(name);
pw.print("-Error: ");
pw.println(e.toString());
return;
}
}
use of android.os.StatFs in project android_frameworks_base by crdroidandroid.
the class DownloadManagerStressTest method testDownloadToCacheWithAlmostFullCache.
/**
* Tests downloading a file to system cache when there isn't enough space in the system cache
* to hold the entire file. DownloadManager deletes enough files to make space for the
* new download.
*/
@LargeTest
public void testDownloadToCacheWithAlmostFullCache() throws Exception {
// 1MB
int DOWNLOAD_FILE_SIZE = 1024 * 1024;
StatFs fs = new StatFs(CACHE_DIR);
int blockSize = fs.getBlockSize();
int availableBlocks = fs.getAvailableBlocks();
int availableBytes = blockSize * availableBlocks;
Log.i(TAG, "INITIAL stage, available space in /cache: " + availableBytes);
File outFile = File.createTempFile("DM_TEST", null, new File(CACHE_DIR));
byte[] buffer = new byte[blockSize];
try {
// download size, and leave that much freespace left on the cache partition
if (DOWNLOAD_FILE_SIZE <= availableBytes) {
int writeSizeBytes = availableBytes - (DOWNLOAD_FILE_SIZE / 2);
int writeSizeBlocks = writeSizeBytes / blockSize;
int remainderSizeBlocks = availableBlocks - writeSizeBlocks;
FileOutputStream fo = null;
try {
fo = new FileOutputStream(outFile);
while (fs.getAvailableBlocks() >= remainderSizeBlocks) {
fo.write(buffer);
fs.restat(CACHE_DIR);
}
} catch (IOException e) {
Log.e(LOG_TAG, "error filling file: ", e);
throw e;
} finally {
if (fo != null) {
fo.close();
}
}
}
// /cache should now be almost full.
long spaceAvailable = fs.getAvailableBlocks() * blockSize;
Log.i(TAG, "BEFORE download, available space in /cache: " + spaceAvailable);
assertTrue(DOWNLOAD_FILE_SIZE > spaceAvailable);
// try to download 1MB file into /cache - and it should succeed
byte[] blobData = generateData(DOWNLOAD_FILE_SIZE, DataType.TEXT);
long dlRequest = doBasicDownload(blobData, DOWNLOAD_TO_SYSTEM_CACHE);
verifyAndCleanupSingleFileDownload(dlRequest, blobData);
} finally {
if (outFile != null) {
outFile.delete();
}
}
}
use of android.os.StatFs in project android_frameworks_base by crdroidandroid.
the class DropBoxTest method testAgeLimits.
public void testAgeLimits() throws Exception {
File dir = getEmptyDir("testAgeLimits");
int blockSize = new StatFs(dir.getPath()).getBlockSize();
// Limit storage to 10 blocks with an expiration of 1 second
int kb = blockSize * 10 / 1024;
ContentResolver cr = getContext().getContentResolver();
Settings.Global.putString(cr, Settings.Global.DROPBOX_AGE_SECONDS, "1");
Settings.Global.putString(cr, Settings.Global.DROPBOX_QUOTA_KB, Integer.toString(kb));
// Write one normal entry and another so big that it is instantly tombstoned
long before = System.currentTimeMillis();
DropBoxManagerService service = new DropBoxManagerService(getContext(), dir);
DropBoxManager dropbox = new DropBoxManager(getContext(), service.getServiceStub());
dropbox.addText("DropBoxTest", "TEST");
addRandomEntry(dropbox, "DropBoxTest", blockSize * 20);
// Verify that things are as expected
DropBoxManager.Entry e0 = dropbox.getNextEntry(null, before);
DropBoxManager.Entry e1 = dropbox.getNextEntry(null, e0.getTimeMillis());
assertTrue(null == dropbox.getNextEntry(null, e1.getTimeMillis()));
assertEquals("TEST", e0.getText(80));
assertEquals(null, e1.getText(80));
assertEquals(-1, getEntrySize(e1));
e0.close();
e1.close();
// Wait a second and write another entry -- old ones should be expunged
Thread.sleep(2000);
dropbox.addText("DropBoxTest", "TEST1");
e0 = dropbox.getNextEntry(null, before);
assertTrue(null == dropbox.getNextEntry(null, e0.getTimeMillis()));
assertEquals("TEST1", e0.getText(80));
e0.close();
}
use of android.os.StatFs in project android_frameworks_base by crdroidandroid.
the class DropBoxTest method testSizeLimits.
public void testSizeLimits() throws Exception {
File dir = getEmptyDir("testSizeLimits");
int blockSize = new StatFs(dir.getPath()).getBlockSize();
// Limit storage to 10 blocks
int kb = blockSize * 10 / 1024;
ContentResolver cr = getContext().getContentResolver();
Settings.Global.putString(cr, Settings.Global.DROPBOX_QUOTA_KB, Integer.toString(kb));
// Three tags using a total of 12 blocks:
// DropBoxTest0 [ ][ ]
// DropBoxTest1 [x][ ][ ][ ][xxx(20 blocks)xxx]
// DropBoxTest2 [xxxxxxxxxx][ ][ ]
//
// The blocks marked "x" will be removed due to storage restrictions.
// Use random fill (so it doesn't compress), subtract a little for gzip overhead
final int overhead = 64;
long before = System.currentTimeMillis();
DropBoxManagerService service = new DropBoxManagerService(getContext(), dir);
DropBoxManager dropbox = new DropBoxManager(getContext(), service.getServiceStub());
addRandomEntry(dropbox, "DropBoxTest0", blockSize - overhead);
addRandomEntry(dropbox, "DropBoxTest0", blockSize - overhead);
addRandomEntry(dropbox, "DropBoxTest1", blockSize - overhead);
addRandomEntry(dropbox, "DropBoxTest1", blockSize - overhead);
addRandomEntry(dropbox, "DropBoxTest1", blockSize * 2 - overhead);
addRandomEntry(dropbox, "DropBoxTest1", blockSize - overhead);
addRandomEntry(dropbox, "DropBoxTest1", blockSize * 20 - overhead);
addRandomEntry(dropbox, "DropBoxTest2", blockSize * 4 - overhead);
addRandomEntry(dropbox, "DropBoxTest2", blockSize - overhead);
addRandomEntry(dropbox, "DropBoxTest2", blockSize - overhead);
DropBoxManager.Entry e0 = dropbox.getNextEntry(null, before);
DropBoxManager.Entry e1 = dropbox.getNextEntry(null, e0.getTimeMillis());
DropBoxManager.Entry e2 = dropbox.getNextEntry(null, e1.getTimeMillis());
DropBoxManager.Entry e3 = dropbox.getNextEntry(null, e2.getTimeMillis());
DropBoxManager.Entry e4 = dropbox.getNextEntry(null, e3.getTimeMillis());
DropBoxManager.Entry e5 = dropbox.getNextEntry(null, e4.getTimeMillis());
DropBoxManager.Entry e6 = dropbox.getNextEntry(null, e5.getTimeMillis());
DropBoxManager.Entry e7 = dropbox.getNextEntry(null, e6.getTimeMillis());
DropBoxManager.Entry e8 = dropbox.getNextEntry(null, e7.getTimeMillis());
DropBoxManager.Entry e9 = dropbox.getNextEntry(null, e8.getTimeMillis());
assertTrue(null == dropbox.getNextEntry(null, e9.getTimeMillis()));
assertEquals("DropBoxTest0", e0.getTag());
assertEquals("DropBoxTest0", e1.getTag());
assertEquals(blockSize - overhead, getEntrySize(e0));
assertEquals(blockSize - overhead, getEntrySize(e1));
assertEquals("DropBoxTest1", e2.getTag());
assertEquals("DropBoxTest1", e3.getTag());
assertEquals("DropBoxTest1", e4.getTag());
assertEquals("DropBoxTest1", e5.getTag());
assertEquals("DropBoxTest1", e6.getTag());
// Tombstone
assertEquals(-1, getEntrySize(e2));
assertEquals(blockSize - overhead, getEntrySize(e3));
assertEquals(blockSize * 2 - overhead, getEntrySize(e4));
assertEquals(blockSize - overhead, getEntrySize(e5));
assertEquals(-1, getEntrySize(e6));
assertEquals("DropBoxTest2", e7.getTag());
assertEquals("DropBoxTest2", e8.getTag());
assertEquals("DropBoxTest2", e9.getTag());
// Tombstone
assertEquals(-1, getEntrySize(e7));
assertEquals(blockSize - overhead, getEntrySize(e8));
assertEquals(blockSize - overhead, getEntrySize(e9));
e0.close();
e1.close();
e2.close();
e3.close();
e4.close();
e5.close();
e6.close();
e7.close();
e8.close();
e9.close();
// Specifying a tag name skips tombstone records.
DropBoxManager.Entry t0 = dropbox.getNextEntry("DropBoxTest1", before);
DropBoxManager.Entry t1 = dropbox.getNextEntry("DropBoxTest1", t0.getTimeMillis());
DropBoxManager.Entry t2 = dropbox.getNextEntry("DropBoxTest1", t1.getTimeMillis());
assertTrue(null == dropbox.getNextEntry("DropBoxTest1", t2.getTimeMillis()));
assertEquals("DropBoxTest1", t0.getTag());
assertEquals("DropBoxTest1", t1.getTag());
assertEquals("DropBoxTest1", t2.getTag());
assertEquals(blockSize - overhead, getEntrySize(t0));
assertEquals(blockSize * 2 - overhead, getEntrySize(t1));
assertEquals(blockSize - overhead, getEntrySize(t2));
t0.close();
t1.close();
t2.close();
}
use of android.os.StatFs in project android-toolbox by Knickedi.
the class AndroidUtils method getFreeExteranlStorageSize.
/**
* Get free (unused) space on external storage.<br>
* <br>
* See {@link Environment#getExternalStorageDirectory()}.
*
* @return free space in kilobytes
*/
public static int getFreeExteranlStorageSize() {
StatFs stats = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
int availableBlocks = stats.getAvailableBlocks();
int blockSizeInBytes = stats.getBlockSize();
return availableBlocks * (blockSizeInBytes / 1024);
}
Aggregations