• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
move(String source, String target)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      */
moveContents(String source, String target)34     public int moveContents(String source, String target) {
35         List<String> files = new Command("find", source, "-type", "f") .execute();
36         for (String file : files) {
37             String targetFile = target + "/" + file.substring(source.length());
38             mkdir(parent(targetFile));
39             new Command("mv", "-i", file, targetFile).execute();
40         }
41         return files.size();
42     }
43 
parent(String file)44     private String parent(String file) {
45         return file.substring(0, file.lastIndexOf('/'));
46     }
47 
mkdir(String dir)48     public void mkdir(String dir) {
49         new Command("mkdir", "-p", dir).execute();
50     }
51 
find(String where, String name)52     public List<String> find(String where, String name) {
53         return new Command("find", where, "-name", name).execute();
54     }
55 
rm(Collection<String> files)56     public void rm(Collection<String> files) {
57         new Command.Builder().args("rm", "-r").args(files).execute();
58     }
59 
rm(String file)60     public void rm(String file) {
61         new Command("rm", "-r", file).execute();
62     }
63 }
64