Bug 787299 - Device Storage - Filter out mimetypes during calculation of disk usages. r=bent

This commit is contained in:
Doug Turner 2012-09-05 14:30:36 -07:00
Родитель 05cb22bb46
Коммит c17bdba719
5 изменённых файлов: 119 добавлений и 13 удалений

Просмотреть файл

@ -313,7 +313,7 @@ DeviceStorageRequestParent::StatFileEvent::CancelableRun()
nsCOMPtr<nsIRunnable> r;
uint64_t diskUsage = 0;
DeviceStorageFile::DirectoryDiskUsage(mFile->mFile, &diskUsage);
DeviceStorageFile::DirectoryDiskUsage(mFile->mFile, &diskUsage, mFile->mStorageType);
int64_t freeSpace = 0;
nsresult rv = mFile->mFile->GetDiskSpaceAvailable(&freeSpace);
if (NS_FAILED(rv)) {

Просмотреть файл

@ -151,10 +151,8 @@ DeviceStorageFile::IsSafePath()
}
bool
DeviceStorageFile::IsType(nsAString& aType)
DeviceStorageFile::IsType(nsIFile* aFile, const nsAString& aStorageType)
{
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
// String bundles are cached by the bundle service.
nsCOMPtr<nsIStringBundleService> stringService = mozilla::services::GetStringBundleService();
if (!stringService) {
@ -168,7 +166,7 @@ DeviceStorageFile::IsType(nsAString& aType)
}
nsString path;
mFile->GetPath(path);
aFile->GetPath(path);
int32_t dotIdx = path.RFindChar(PRUnichar('.'));
if (dotIdx == kNotFound) {
@ -181,7 +179,7 @@ DeviceStorageFile::IsType(nsAString& aType)
extensionMatch.AppendASCII(";");
nsString extensionListStr;
if (NS_FAILED(filterBundle->GetStringFromName(aType.BeginReading(),
if (NS_FAILED(filterBundle->GetStringFromName(aStorageType.BeginReading(),
getter_Copies(extensionListStr)))) {
return false;
}
@ -405,7 +403,7 @@ DeviceStorageFile::collectFilesInternal(nsTArray<nsRefPtr<DeviceStorageFile> > &
}
void
DeviceStorageFile::DirectoryDiskUsage(nsIFile* aFile, uint64_t* aSoFar)
DeviceStorageFile::DirectoryDiskUsage(nsIFile* aFile, uint64_t* aSoFar, const nsAString& aStorageType)
{
if (!aFile) {
return;
@ -441,12 +439,18 @@ DeviceStorageFile::DirectoryDiskUsage(nsIFile* aFile, uint64_t* aSoFar)
if (NS_FAILED(rv)) {
continue;
}
if (isLink) {
// for now, lets just totally ignore symlinks.
NS_WARNING("DirectoryDiskUsage ignores symlinks");
} else if (isDir) {
DirectoryDiskUsage(f, aSoFar);
DirectoryDiskUsage(f, aSoFar, aStorageType);
} else if (isFile) {
if (!DeviceStorageFile::IsType(f, aStorageType)) {
continue;
}
int64_t size;
rv = f->GetFileSize(&size);
if (NS_SUCCEEDED(rv)) {
@ -773,7 +777,7 @@ ContinueCursorEvent::Run() {
while (cursor->mFiles.Length() > 0) {
nsRefPtr<DeviceStorageFile> file = cursor->mFiles[0];
cursor->mFiles.RemoveElementAt(0);
if (!file->IsType(cursorStorageType)) {
if (!DeviceStorageFile::IsType(file->mFile, cursorStorageType)) {
continue;
}
val = nsIFileToJsval(cursor->GetOwner(), file);
@ -1187,7 +1191,7 @@ public:
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
nsCOMPtr<nsIRunnable> r;
uint64_t diskUsage = 0;
DeviceStorageFile::DirectoryDiskUsage(mFile->mFile, &diskUsage);
DeviceStorageFile::DirectoryDiskUsage(mFile->mFile, &diskUsage, mFile->mStorageType);
int64_t freeSpace = 0;
nsresult rv = mFile->mFile->GetDiskSpaceAvailable(&freeSpace);
if (NS_FAILED(rv)) {
@ -1623,7 +1627,7 @@ nsDOMDeviceStorage::AddNamed(nsIDOMBlob *aBlob,
nsCOMPtr<nsIRunnable> r;
nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(mStorageType, mRootDirectory, aPath);
if (!dsf->IsType(mStorageType) || !IsMimeTypeCorrectForStorageType(mStorageType, aBlob)) {
if (!DeviceStorageFile::IsType(dsf->mFile, mStorageType) || !IsMimeTypeCorrectForStorageType(mStorageType, aBlob)) {
r = new PostErrorEvent(request, POST_ERROR_EVENT_ILLEGAL_TYPE, dsf);
}
else if (!dsf->IsSafePath()) {

Просмотреть файл

@ -58,7 +58,6 @@ public:
// we want to make sure that the names of file can't reach
// outside of the type of storage the user asked for.
bool IsSafePath();
bool IsType(nsAString& aType);
nsresult Remove();
nsresult Write(nsIInputStream* aInputStream);
@ -66,7 +65,8 @@ public:
void CollectFiles(nsTArray<nsRefPtr<DeviceStorageFile> > &aFiles, uint64_t aSince = 0);
void collectFilesInternal(nsTArray<nsRefPtr<DeviceStorageFile> > &aFiles, uint64_t aSince, nsAString& aRootPath);
static void DirectoryDiskUsage(nsIFile* aFile, uint64_t* aSoFar);
static bool IsType(nsIFile* aFile, const nsAString& aStorageType);
static void DirectoryDiskUsage(nsIFile* aFile, uint64_t* aSoFar, const nsAString& aStorageType);
private:
void NormalizeFilePath();

Просмотреть файл

@ -20,6 +20,7 @@ MOCHITEST_FILES = \
test_enumerate.html \
test_enumerateMultipleContinue.html \
test_overwrite.html \
test_diskSpace.html \
test_dotdot.html \
test_enumerateOptions.html \
test_lastModificationFilter.html \

Просмотреть файл

@ -0,0 +1,101 @@
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html> <!--
https://bugzilla.mozilla.org/show_bug.cgi?id=717103
-->
<head>
<title>Test for the device storage API </title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="devicestorage_common.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=717103">Mozilla Bug 717103</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
devicestorage_setup();
var freeBytes = -1;
var stats = 0;
function stat(s, file_list_length) {
if (freeBytes == -1) {
freeBytes = s.target.result.freeBytes;
}
ok(freeBytes == s.target.result.freeBytes, "Free bytes should be the same");
ok(file_list_length * 1024 == s.target.result.totalBytes, "space taken up by files should match")
stats = stats + 1;
if (stats == 2) {
devicestorage_cleanup();
}
}
function addSuccess(e) {
added = added - 1;
if (added == 0) {
request = pictures.stat();
request.onsuccess = function(s) {stat(s, picture_files.length)};
request = videos.stat();
request.onsuccess = function(s) {stat(s, video_files.length)};
request = music.stat();
request.onsuccess = function(s) {stat(s, music_files.length)};
}
}
function addError(e) {
ok(false, "addError was called : " + e.target.error.name);
devicestorage_cleanup();
}
ok(true, "hi");
var pictures = navigator.getDeviceStorage("pictures");
var picture_files = [ "a.png", "b.png", "c.png", "d.png", "e.png" ];
var videos = navigator.getDeviceStorage("videos");
var video_files = [ "a.ogv", "b.ogv" ];
var music = navigator.getDeviceStorage("music");
var music_files = [ "a.mp3", "b.mp3", "c.mp3" ];
var added = picture_files.length + video_files.length + music_files.length;
for (var i=0; i < picture_files.length; i++) {
request = pictures.addNamed(createRandomBlob('image/png'), picture_files[i]);
request.onsuccess = addSuccess;
request.onerror = addError;
}
for (var i=0; i < video_files.length; i++) {
request = videos.addNamed(createRandomBlob('video/ogv'), video_files[i]);
request.onsuccess = addSuccess;
request.onerror = addError;
}
for (var i=0; i < music_files.length; i++) {
request = music.addNamed(createRandomBlob('audio/mp3'), music_files[i]);
request.onsuccess = addSuccess;
request.onerror = addError;
}
</script>
</pre>
</body>
</html>