1 /* 2 * Copyright (C) 2023 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 android.tools.device.traces.io 18 19 import android.tools.device.traces.executeShellCommand 20 import java.io.File 21 import java.nio.file.Files 22 import java.nio.file.Paths 23 24 object IoUtils { copyFilenull25 private fun copyFile(src: File, dst: File) { 26 executeShellCommand("cp $src $dst") 27 executeShellCommand("chmod a+r $dst") 28 } 29 moveFilenull30 fun moveFile(src: File, dst: File) { 31 if (src.isDirectory) { 32 moveDirectory(src, dst) 33 } 34 // Move the file to the output directory 35 // Note: Due to b/141386109, certain devices do not allow moving the files between 36 // directories with different encryption policies, so manually copy and then 37 // remove the original file 38 // Moreover, the copied trace file may end up with different permissions, resulting 39 // in b/162072200, to prevent this, ensure the files are readable after copying 40 copyFile(src, dst) 41 executeShellCommand("rm $src") 42 } 43 moveDirectorynull44 private fun moveDirectory(src: File, dst: File) { 45 require(src.isDirectory) { "$src is not a directory" } 46 47 Files.createDirectories(Paths.get(dst.path)) 48 49 src.listFiles()?.forEach { 50 if (it.isDirectory) { 51 moveDirectory(src, dst.resolve(it.name)) 52 } else { 53 moveFile(it, dst.resolve(it.name)) 54 } 55 } 56 } 57 } 58