1/*
2 * Copyright (C) 2013 DroidDriver committers
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
17package io.appium.droiddriver.util;
18
19import android.annotation.TargetApi;
20import android.os.Build;
21import android.util.Log;
22
23import java.io.BufferedOutputStream;
24import java.io.File;
25import java.io.FileNotFoundException;
26import java.io.FileOutputStream;
27
28import io.appium.droiddriver.exceptions.DroidDriverException;
29
30/**
31 * Internal helper methods for manipulating files.
32 */
33public class FileUtils {
34  /**
35   * Opens file at {@code path} to output. If any directories on {@code path} do
36   * not exist, they will be created. The file will be readable and writable to
37   * all.
38   */
39  @TargetApi(Build.VERSION_CODES.GINGERBREAD)
40  public static BufferedOutputStream open(String path) throws FileNotFoundException {
41    File file = getAbsoluteFile(path);
42
43    Logs.log(Log.INFO, "opening file " + file.getAbsolutePath());
44    BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(file));
45    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
46      file.setReadable(true /* readable */, false/* ownerOnly */);
47      file.setWritable(true /* readable */, false/* ownerOnly */);
48    }
49    return stream;
50  }
51
52  /**
53   * Returns a new file constructed using the absolute path of {@code path}.
54   * Unlike {@link File#getAbsoluteFile()}, default parent is "java.io.tmpdir"
55   * instead of "user.dir".
56   * <p>
57   * If any directories on {@code path} do not exist, they will be created.
58   */
59  public static File getAbsoluteFile(String path) {
60    File file = new File(path);
61    if (!file.isAbsolute()) {
62      file = new File(System.getProperty("java.io.tmpdir"), path);
63    }
64    mkdirs(file.getParentFile());
65    return file;
66  }
67
68  @TargetApi(Build.VERSION_CODES.GINGERBREAD)
69  private static void mkdirs(File dir) {
70    if (dir == null || dir.exists()) {
71      return;
72    }
73
74    mkdirs(dir.getParentFile());
75    if (!dir.mkdir()) {
76      throw new DroidDriverException("failed to mkdir " + dir);
77    }
78    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
79      dir.setReadable(true /* readable */, false/* ownerOnly */);
80      dir.setWritable(true /* readable */, false/* ownerOnly */);
81      dir.setExecutable(true /* executable */, false/* ownerOnly */);
82    }
83  }
84}
85