aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreehugger Robot <treehugger-gerrit@google.com>2019-06-13 05:21:22 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2019-06-13 05:21:22 +0000
commitd3295cfaee8dcd0cd86589fde428f4f0998b9641 (patch)
tree0950e17982c4616fe5dbc99a20f5e01cd7f7c3ed
parent6826439246edd081da9df6949ef3536d7c9ee872 (diff)
parent78604e9ffc09feeb7bcbb99d83613ab94541a81a (diff)
downloadtradefederation-d3295cfaee8dcd0cd86589fde428f4f0998b9641.tar.gz
Merge "Fix the check against the "no result" string for Content Provider" into pie-cts-dev
-rw-r--r--src/com/android/tradefed/device/contentprovider/ContentProviderHandler.java167
-rw-r--r--tests/src/com/android/tradefed/device/contentprovider/ContentProviderHandlerTest.java2
2 files changed, 94 insertions, 75 deletions
diff --git a/src/com/android/tradefed/device/contentprovider/ContentProviderHandler.java b/src/com/android/tradefed/device/contentprovider/ContentProviderHandler.java
index 482b9704d..a250987bc 100644
--- a/src/com/android/tradefed/device/contentprovider/ContentProviderHandler.java
+++ b/src/com/android/tradefed/device/contentprovider/ContentProviderHandler.java
@@ -156,53 +156,7 @@ public class ContentProviderHandler {
*/
public boolean pullDir(String deviceFilePath, File localDir)
throws DeviceNotAvailableException {
- if (!localDir.isDirectory()) {
- CLog.e("Local path %s is not a directory", localDir.getAbsolutePath());
- return false;
- }
-
- String contentUri = createEscapedContentUri(deviceFilePath);
- String queryContentCommand =
- String.format(
- "content query --user %d --uri %s", mDevice.getCurrentUser(), contentUri);
-
- String listCommandResult = mDevice.executeShellCommand(queryContentCommand);
-
- if (listCommandResult.equals(NO_RESULTS_STRING)) {
- // Empty directory.
- return true;
- }
-
- String[] listResult = listCommandResult.split("[\\r\\n]+");
-
- for (String row : listResult) {
- HashMap<String, String> columnValues = parseQueryResultRow(row);
- boolean isDirectory = Boolean.valueOf(columnValues.get(COLUMN_DIRECTORY));
- String name = columnValues.get(COLUMN_NAME);
- String path = columnValues.get(COLUMN_ABSOLUTE_PATH);
-
- File localChild = new File(localDir, name);
- if (isDirectory) {
- if (!localChild.mkdir()) {
- CLog.w(
- "Failed to create sub directory %s, aborting.",
- localChild.getAbsolutePath());
- return false;
- }
-
- if (!pullDir(path, localChild)) {
- CLog.w("Failed to pull sub directory %s from device, aborting", path);
- return false;
- }
- } else {
- // handle regular file
- if (!pullFile(path, localChild)) {
- CLog.w("Failed to pull file %s from device, aborting", path);
- return false;
- }
- }
- }
- return true;
+ return pullDirInternal(deviceFilePath, localDir, /* currentUser */ null);
}
/**
@@ -216,33 +170,7 @@ public class ContentProviderHandler {
*/
public boolean pullFile(String deviceFilePath, File localFile)
throws DeviceNotAvailableException {
- String contentUri = createEscapedContentUri(deviceFilePath);
- String pullCommand =
- String.format(
- "content read --user %d --uri %s", mDevice.getCurrentUser(), contentUri);
-
- // Open the output stream to the local file.
- OutputStream localFileStream;
- try {
- localFileStream = new FileOutputStream(localFile);
- } catch (FileNotFoundException e) {
- CLog.e("Failed to open OutputStream to the local file. Error: %s", e.getMessage());
- return false;
- }
-
- try {
- CommandResult pullResult = mDevice.executeShellV2Command(pullCommand, localFileStream);
- if (isSuccessful(pullResult)) {
- return true;
- }
-
- CLog.e(
- "Failed to pull a file at '%s' to %s using content provider. Error: '%s'",
- deviceFilePath, localFile, pullResult.getStderr());
- return false;
- } finally {
- StreamUtil.close(localFileStream);
- }
+ return pullFileInternal(deviceFilePath, localFile, /* currentUser */ null);
}
/**
@@ -346,4 +274,95 @@ public class ContentProviderHandler {
}
return columnValues;
}
+
+ /**
+ * Internal method to actually do the pull directory but without re-querying the current user
+ * while doing the recursive pull.
+ */
+ private boolean pullDirInternal(String deviceFilePath, File localDir, Integer currentUser)
+ throws DeviceNotAvailableException {
+ if (!localDir.isDirectory()) {
+ CLog.e("Local path %s is not a directory", localDir.getAbsolutePath());
+ return false;
+ }
+
+ String contentUri = createEscapedContentUri(deviceFilePath);
+ if (currentUser == null) {
+ // Keep track of the user so if we recursively pull dir we don't re-query it.
+ currentUser = mDevice.getCurrentUser();
+ }
+ String queryContentCommand =
+ String.format("content query --user %d --uri %s", currentUser, contentUri);
+
+ String listCommandResult = mDevice.executeShellCommand(queryContentCommand);
+
+ if (NO_RESULTS_STRING.equals(listCommandResult.trim())) {
+ // Empty directory.
+ return true;
+ }
+
+ String[] listResult = listCommandResult.split("[\\r\\n]+");
+
+ for (String row : listResult) {
+ HashMap<String, String> columnValues = parseQueryResultRow(row);
+ boolean isDirectory = Boolean.valueOf(columnValues.get(COLUMN_DIRECTORY));
+ String name = columnValues.get(COLUMN_NAME);
+ String path = columnValues.get(COLUMN_ABSOLUTE_PATH);
+
+ File localChild = new File(localDir, name);
+ if (isDirectory) {
+ if (!localChild.mkdir()) {
+ CLog.w(
+ "Failed to create sub directory %s, aborting.",
+ localChild.getAbsolutePath());
+ return false;
+ }
+
+ if (!pullDirInternal(path, localChild, currentUser)) {
+ CLog.w("Failed to pull sub directory %s from device, aborting", path);
+ return false;
+ }
+ } else {
+ // handle regular file
+ if (!pullFileInternal(path, localChild, currentUser)) {
+ CLog.w("Failed to pull file %s from device, aborting", path);
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ private boolean pullFileInternal(String deviceFilePath, File localFile, Integer currentUser)
+ throws DeviceNotAvailableException {
+ String contentUri = createEscapedContentUri(deviceFilePath);
+ if (currentUser == null) {
+ currentUser = mDevice.getCurrentUser();
+ }
+ String pullCommand =
+ String.format("content read --user %d --uri %s", currentUser, contentUri);
+
+ // Open the output stream to the local file.
+ OutputStream localFileStream;
+ try {
+ localFileStream = new FileOutputStream(localFile);
+ } catch (FileNotFoundException e) {
+ CLog.e("Failed to open OutputStream to the local file. Error: %s", e.getMessage());
+ return false;
+ }
+
+ try {
+ CommandResult pullResult = mDevice.executeShellV2Command(pullCommand, localFileStream);
+ if (isSuccessful(pullResult)) {
+ return true;
+ }
+
+ CLog.e(
+ "Failed to pull a file at '%s' to %s using content provider. Error: '%s'",
+ deviceFilePath, localFile, pullResult.getStderr());
+ return false;
+ } finally {
+ StreamUtil.close(localFileStream);
+ }
+ }
}
diff --git a/tests/src/com/android/tradefed/device/contentprovider/ContentProviderHandlerTest.java b/tests/src/com/android/tradefed/device/contentprovider/ContentProviderHandlerTest.java
index f03922f43..a9f453352 100644
--- a/tests/src/com/android/tradefed/device/contentprovider/ContentProviderHandlerTest.java
+++ b/tests/src/com/android/tradefed/device/contentprovider/ContentProviderHandlerTest.java
@@ -222,7 +222,7 @@ public class ContentProviderHandlerTest {
public void testPullDir_EmptyDirectory() throws Exception {
File pullTo = FileUtil.createTempDir("content-provider-test");
- doReturn("No result found.").when(mMockDevice).executeShellCommand(anyString());
+ doReturn("No result found.\n").when(mMockDevice).executeShellCommand(anyString());
try {
assertTrue(mProvider.pullDir("path/somewhere", pullTo));