Jesse Wilson | 0ad6047 | 2009-09-14 11:23:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | import java.util.Collection; |
| 18 | import java.util.List; |
| 19 | |
| 20 | /** |
| 21 | * Factory for filesystem commands. |
| 22 | */ |
| 23 | class Filesystem { |
| 24 | |
| 25 | public void move(String source, String target) { |
| 26 | new Command("mv", source, target).execute(); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Moves all of the files in {@code source} to {@code target}, one at a |
| 31 | * time. Unlike {@code move}, this approach works even if the target |
| 32 | * directory is nonempty. |
| 33 | */ |
| 34 | public int moveContents(String source, String target) { |
Jesse Wilson | d383e2a | 2009-10-08 00:25:33 -0700 | [diff] [blame] | 35 | return copyContents(true, source, target); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Copies all of the files in {@code source} to {@code target}, one at a |
| 40 | * time. Unlike {@code move}, this approach works even if the target |
| 41 | * directory is nonempty. |
| 42 | */ |
| 43 | public int copyContents(String source, String target) { |
| 44 | return copyContents(false, source, target); |
| 45 | } |
| 46 | |
| 47 | private int copyContents(boolean move, String source, String target) { |
Jesse Wilson | 0ad6047 | 2009-09-14 11:23:39 -0700 | [diff] [blame] | 48 | List<String> files = new Command("find", source, "-type", "f") .execute(); |
| 49 | for (String file : files) { |
| 50 | String targetFile = target + "/" + file.substring(source.length()); |
| 51 | mkdir(parent(targetFile)); |
Jesse Wilson | d383e2a | 2009-10-08 00:25:33 -0700 | [diff] [blame] | 52 | if (move) { |
| 53 | new Command("mv", "-i", file, targetFile).execute(); |
| 54 | } else { |
| 55 | new Command("cp", file, targetFile).execute(); |
| 56 | } |
Jesse Wilson | 0ad6047 | 2009-09-14 11:23:39 -0700 | [diff] [blame] | 57 | } |
| 58 | return files.size(); |
| 59 | } |
| 60 | |
| 61 | private String parent(String file) { |
| 62 | return file.substring(0, file.lastIndexOf('/')); |
| 63 | } |
| 64 | |
| 65 | public void mkdir(String dir) { |
| 66 | new Command("mkdir", "-p", dir).execute(); |
| 67 | } |
| 68 | |
| 69 | public List<String> find(String where, String name) { |
| 70 | return new Command("find", where, "-name", name).execute(); |
| 71 | } |
| 72 | |
| 73 | public void rm(Collection<String> files) { |
| 74 | new Command.Builder().args("rm", "-r").args(files).execute(); |
| 75 | } |
| 76 | |
| 77 | public void rm(String file) { |
| 78 | new Command("rm", "-r", file).execute(); |
| 79 | } |
| 80 | } |