use of android.os.StatFs in project android_frameworks_base by ResurrectionRemix.
the class LowStorageTest method fillupdisk.
// Fill up 100% of the data partition
public void fillupdisk(Context context) {
final Context contextfill = context;
new Thread() {
@Override
public void run() {
try {
// Fill up all the memory
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
int totalBlocks = stat.getBlockCount();
int noOfBlockToFill = stat.getAvailableBlocks();
FileOutputStream fs = contextfill.openFileOutput("testdata", Context.MODE_APPEND);
for (int i = 0; i < (noOfBlockToFill / NO_OF_BLOCKS_TO_FILL); i++) {
byte[] buf = new byte[mBlockSize * NO_OF_BLOCKS_TO_FILL];
fs.write(buf);
fs.flush();
}
// Fill up the last few block
byte[] buf = new byte[(noOfBlockToFill % NO_OF_BLOCKS_TO_FILL) * mBlockSize];
fs.write(buf);
fs.flush();
fs.close();
// Finished, update the info
synchronized (fillUpDone) {
fillUpDone.notify();
}
} catch (Exception e) {
Log.v(TAG, e.toString());
}
}
}.start();
}
use of android.os.StatFs in project android_frameworks_base by ResurrectionRemix.
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 ResurrectionRemix.
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 ResurrectionRemix.
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-common by litesuits.
the class SdCardUtil method getAvailableSize.
/**
* Get available size of SD card.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static long getAvailableSize(String path) {
try {
File base = new File(path);
StatFs stat = new StatFs(base.getPath());
return stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
Aggregations