1/*
2 * Copyright (C) 2008 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
17package dalvik.system;
18
19import java.io.File;
20
21/**
22 * Utility class to handle the setup of the core library's concept of
23 * what the "default temporary directory" is. Application code may
24 * call into this class with an appropriate base directory during its
25 * startup, as a reasonably easy way to get the standard property
26 * <code>java.io.tmpdir</code> to point at something useful.
27 *
28 * @hide
29 */
30public class TemporaryDirectory {
31    /** system property name for the temporary directory */
32    private static final String PROPERTY = "java.io.tmpdir";
33
34    /** final path component name for the temporary directory */
35    private static final String PATH_NAME = "tmp";
36
37    /** whether a temporary directory has been configured yet */
38    private static boolean configured = false;
39
40    /**
41     * Convenience method which is equivalent to
42     * <code>setupDirectory(new File(baseDir))</code>.
43     *
44     * @param baseDir the base directory of the temporary directory
45     */
46    public static void setUpDirectory(String baseDir) {
47        setUpDirectory(new File(baseDir));
48    }
49
50    /**
51     * Sets up the temporary directory, but only if one isn't already
52     * defined for this process, and only if it is possible (e.g., the
53     * directory already exists and is read-write, or the directory
54     * can be created). This call will do one of three things:
55     *
56     * <ul>
57     * <li>return without error and without doing anything, if a
58     * previous call to this method succeeded</li>
59     * <li>return without error, having either created a temporary
60     * directory under the given base or verified that such a directory
61     * already exists</li>
62     * <li>throw <code>UnsupportedOperationException</code> if the
63     * directory could not be created or accessed</li>
64     * </ul>
65     *
66     * @param baseDir the base directory of the temporary directory
67     */
68    public static synchronized void setUpDirectory(File baseDir) {
69        if (configured) {
70            System.logE("Already set to: " + System.getProperty(PROPERTY));
71            return;
72        }
73
74        File dir = new File(baseDir, PATH_NAME);
75        String absolute = dir.getAbsolutePath();
76
77        if (dir.exists()) {
78            if (!dir.isDirectory()) {
79                throw new UnsupportedOperationException(
80                        "Name is used by a non-directory file: " +
81                        absolute);
82            } else if (!(dir.canRead() && dir.canWrite())) {
83                throw new UnsupportedOperationException(
84                        "Existing directory is not readable and writable: " +
85                        absolute);
86            }
87        } else {
88            if (!dir.mkdirs()) {
89                throw new UnsupportedOperationException(
90                        "Failed to create directory: " + absolute);
91            }
92        }
93
94        System.setProperty(PROPERTY, absolute);
95        configured = true;
96    }
97}
98