1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package vogar.android; 18 19 import com.google.common.collect.ImmutableList; 20 import java.io.File; 21 import java.io.FileNotFoundException; 22 import java.util.ArrayList; 23 import java.util.HashSet; 24 import java.util.LinkedList; 25 import java.util.List; 26 import java.util.Set; 27 import vogar.Log; 28 import vogar.commands.Command; 29 import vogar.commands.CommandFailedException; 30 31 /** 32 * Make directories on a remote filesystem. 33 */ 34 public final class DeviceFilesystem { 35 private final Set<File> mkdirCache = new HashSet<File>(); 36 private final List<String> targetProcessPrefix; 37 private final Log log; 38 39 public DeviceFilesystem(Log log, ImmutableList<String> targetProcessPrefix) { 40 this.log = log; 41 this.targetProcessPrefix = targetProcessPrefix; 42 } 43 44 public void mkdirs(File name) { 45 LinkedList<File> directoryStack = new LinkedList<File>(); 46 File dir = name; 47 // Do some directory bootstrapping since "mkdir -p" doesn't work in adb shell. Don't bother 48 // trying to create /sdcard or /. This might reach dir == null if given a relative path, 49 // otherwise it should terminate with "/sdcard" or "/". 50 while (dir != null && !dir.getPath().equals("/sdcard") && !dir.getPath().equals("/")) { 51 directoryStack.addFirst(dir); 52 dir = dir.getParentFile(); 53 } 54 // would love to do "adb shell mkdir DIR1 DIR2 DIR3 ..." but unfortunately this will stop 55 // if any of the directories fail to be created (even for a reason like "file exists"), so 56 // they have to be created one by one. 57 for (File createDir : directoryStack) { 58 // to reduce adb traffic, only try to make a directory if we haven't tried before. 59 if (!mkdirCache.contains(createDir)) { 60 mkdir(createDir); 61 mkdirCache.add(createDir); 62 } 63 } 64 } 65 66 private void mkdir(File name) { 67 List<String> args = new ArrayList<String>(); 68 args.addAll(targetProcessPrefix); 69 args.add("mkdir"); 70 args.add(name.getPath()); 71 72 List<String> rawResult = new Command.Builder(log) 73 .args(args) 74 .permitNonZeroExitStatus(true) 75 .execute(); 76 // fail if this failed for any reason other than the file existing. 77 if (!rawResult.isEmpty() && !rawResult.get(0).contains("File exists")) { 78 throw new CommandFailedException(args, rawResult); 79 } 80 } 81 82 public List<File> ls(File dir) throws FileNotFoundException { 83 List<String> args = new ArrayList<String>(); 84 args.addAll(targetProcessPrefix); 85 args.add("ls"); 86 args.add(dir.getPath()); 87 88 List<String> rawResult = new Command.Builder(log) 89 .args(args) 90 // Note: When all supported versions of Android correctly return the exit code 91 // from adb we can rely on the exit code to detect failure. Until then: no. 92 .permitNonZeroExitStatus(true) 93 .execute(); 94 List<File> files = new ArrayList<File>(); 95 for (String fileString : rawResult) { 96 if (fileString.equals(dir.getPath() + ": No such file or directory")) { 97 throw new FileNotFoundException(dir + " not found."); 98 } 99 if (fileString.equals(dir.getPath())) { 100 // The argument must have been a file or symlink, not a directory 101 files.add(dir); 102 } else { 103 files.add(new File(dir, fileString)); 104 } 105 } 106 return files; 107 } 108 } 109