Add the root of crates by Context.getCrateDir

Context.getCrateDir() creates and returns the File for the
root of the crates for the application context. Any child directories
under the crates root in Context.getDataFile() is called
the crate directory.

For example:
  /data/user/0/com.exmaple.app/crates/IAmCratedFolder/decentFiles
  /data/user/0/com.exmaple.app/crates/IAmCratedFolder/decentDirs

Context.getCrateDir return the crate dir
  --> /data/user/0/com.exmaple.app/crates/IAmCratedFolder

The crate dir
  --> IAmCratedFolder
The decent directories or files are not crated folders
  --> decentFiles, decentDirs

Test: atest CtsOsTestCases:android.os.storage.cts.StorageCrateTest
Bug: 141660526
Change-Id: If3ec1e121fb8f72bab5571d6190378e3ae208832
This commit is contained in:
Felka Chang
2019-11-19 15:06:52 +08:00
parent 70b6079730
commit 0d824d4161
5 changed files with 67 additions and 0 deletions

View File

@@ -697,6 +697,7 @@ package android.content {
public abstract class Context {
method @NonNull public android.content.Context createContextAsUser(@NonNull android.os.UserHandle, int);
method @NonNull public android.content.Context createPackageContextAsUser(@NonNull String, int, @NonNull android.os.UserHandle) throws android.content.pm.PackageManager.NameNotFoundException;
method @NonNull public java.io.File getCrateDir(@NonNull String);
method public abstract android.view.Display getDisplay();
method public abstract int getDisplayId();
method public android.os.UserHandle getUser();

View File

@@ -99,6 +99,7 @@ import java.io.InputStream;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.nio.ByteOrder;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Objects;
import java.util.concurrent.Executor;
@@ -251,6 +252,8 @@ class ContextImpl extends Context {
@GuardedBy("mSync")
private File mFilesDir;
@GuardedBy("mSync")
private File mCratesDir;
@GuardedBy("mSync")
private File mNoBackupFilesDir;
@GuardedBy("mSync")
private File mCacheDir;
@@ -701,6 +704,24 @@ class ContextImpl extends Context {
}
}
@Override
public File getCrateDir(@NonNull String crateId) {
Preconditions.checkArgument(FileUtils.isValidExtFilename(crateId), "invalidated crateId");
final Path cratesRootPath = getDataDir().toPath().resolve("crates");
final Path absoluteNormalizedCratePath = cratesRootPath.resolve(crateId)
.toAbsolutePath().normalize();
synchronized (mSync) {
if (mCratesDir == null) {
mCratesDir = cratesRootPath.toFile();
}
ensurePrivateDirExists(mCratesDir);
}
File cratedDir = absoluteNormalizedCratePath.toFile();
return ensurePrivateDirExists(cratedDir);
}
@Override
public File getNoBackupFilesDir() {
synchronized (mSync) {

View File

@@ -1059,6 +1059,31 @@ public abstract class Context {
*/
public abstract File getFilesDir();
/**
* Returns the absolute path to the directory that is related to the crate on the filesystem.
* <p>
* The crateId require a validated file name. It can't contain any "..", ".",
* {@link File#separatorChar} etc..
* </p>
* <p>
* The returned path may change over time if the calling app is moved to an
* adopted storage device, so only relative paths should be persisted.
* </p>
* <p>
* No additional permissions are required for the calling app to read or
* write files under the returned path.
*</p>
*
* @param crateId the relative validated file name under {@link Context#getDataDir()}/crates
* @return the crate directory file.
* @hide
*/
@NonNull
@TestApi
public File getCrateDir(@NonNull String crateId) {
throw new RuntimeException("Not implemented. Must override in a subclass.");
}
/**
* Returns the absolute path to the directory on the filesystem similar to
* {@link #getFilesDir()}. The difference is that files placed under this

View File

@@ -252,6 +252,16 @@ public class ContextWrapper extends Context {
return mBase.getFilesDir();
}
/**
* {@inheritDoc Context#getCrateDir()}
* @hide
*/
@NonNull
@Override
public File getCrateDir(@NonNull String cratedId) {
return mBase.getCrateDir(cratedId);
}
@Override
public File getNoBackupFilesDir() {
return mBase.getNoBackupFilesDir();

View File

@@ -16,6 +16,7 @@
package android.test.mock;
import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.app.IApplicationThread;
import android.app.IServiceConnection;
@@ -211,6 +212,15 @@ public class MockContext extends Context {
throw new UnsupportedOperationException();
}
/**
* {@inheritDoc Context#getCrateDir()}
* @hide
*/
@Override
public File getCrateDir(@NonNull String crateId) {
throw new UnsupportedOperationException();
}
@Override
public File getNoBackupFilesDir() {
throw new UnsupportedOperationException();