| 1 | /* |
| 2 | Copyright (C) 2004 MySQL AB |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License version 2 as |
| 6 | published by the Free Software Foundation. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program; if not, write to the Free Software |
| 15 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | |
| 17 | */ |
| 18 | package com.mysql.management.util; |
| 19 | |
| 20 | import java.io.File; |
| 21 | import java.io.FileInputStream; |
| 22 | import java.io.IOException; |
| 23 | import java.io.PrintStream; |
| 24 | |
| 25 | /** |
| 26 | * @author Eric Herman <eric@mysql.com> |
| 27 | * @version $Id: Files.java,v 1.2 2005/12/01 21:45:31 eherman Exp $ |
| 28 | */ |
| 29 | public class Files { |
| 30 | |
| 31 | public static final String JAVA_IO_TMPDIR = "java.io.tmpdir"; |
| 32 | |
| 33 | public static final String USE_TEST_DIR = "c-mxj.files.use-test-dir"; |
| 34 | |
| 35 | private Shell.Factory shellFactory; |
| 36 | |
| 37 | private char separatorChar; |
| 38 | |
| 39 | private Streams streams; |
| 40 | |
| 41 | public Files() { |
| 42 | this(new Shell.Factory(), File.separatorChar, new Streams()); |
| 43 | } |
| 44 | |
| 45 | Files(Shell.Factory shellFactory, char separatorChar, Streams streams) { |
| 46 | this.shellFactory = shellFactory; |
| 47 | this.separatorChar = separatorChar; |
| 48 | this.streams = streams; |
| 49 | } |
| 50 | |
| 51 | public File testDir() { |
| 52 | return new File(tmp(), "test-c.mxj"); |
| 53 | } |
| 54 | |
| 55 | public File tmp() { |
| 56 | return cononical(new File(System.getProperty(JAVA_IO_TMPDIR))); |
| 57 | } |
| 58 | |
| 59 | public File tmp(String subdir) { |
| 60 | String useTestDir = System.getProperty(USE_TEST_DIR); |
| 61 | if (Boolean.TRUE.toString().equalsIgnoreCase(useTestDir)) { |
| 62 | return new File(testDir(), subdir); |
| 63 | } |
| 64 | return new File(tmp(), subdir); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Depth First traversal of the directory. Attempts to delete every file in |
| 69 | * the structure. |
| 70 | * |
| 71 | * @return true if the file passed in is successfully deleted |
| 72 | */ |
| 73 | public boolean deleteTree(File file) { |
| 74 | if (file.isDirectory()) { |
| 75 | File[] files = file.listFiles(); |
| 76 | for (int i = 0; i < files.length; i++) { |
| 77 | deleteTree(files[i]); |
| 78 | } |
| 79 | } |
| 80 | return file.delete(); |
| 81 | } |
| 82 | |
| 83 | /* TODO make this more platform independant */ |
| 84 | /** |
| 85 | * On UNIX systems, in order for a file to be executable, it needs to have |
| 86 | * the execute bit set. This method executes a "chmod +x filename" |
| 87 | */ |
| 88 | public void addExecutableRights(File executable, PrintStream out, |
| 89 | PrintStream err) { |
| 90 | if (isWindows()) { |
| 91 | return; |
| 92 | } |
| 93 | String[] args = { "chmod", "+x", executable.getPath() }; |
| 94 | String tName = "make " + executable + " runable"; |
| 95 | shellFactory.newShell(args, tName, out, err).run(); |
| 96 | } |
| 97 | |
| 98 | public boolean isWindows() { |
| 99 | return separatorChar == '\\'; |
| 100 | } |
| 101 | |
| 102 | public String asString(final File file) { |
| 103 | return new Exceptions.StringBlock() { |
| 104 | public String inner() throws IOException { |
| 105 | FileInputStream fis = new FileInputStream(file); |
| 106 | try { |
| 107 | return streams.readString(fis); |
| 108 | } finally { |
| 109 | fis.close(); |
| 110 | } |
| 111 | } |
| 112 | }.exec(); |
| 113 | } |
| 114 | |
| 115 | public File nullFile() { |
| 116 | return new File(""); |
| 117 | } |
| 118 | |
| 119 | public File newFile(Object fileName) { |
| 120 | if (fileName == null) { |
| 121 | return nullFile(); |
| 122 | } |
| 123 | return cononical(new File(fileName.toString())); |
| 124 | } |
| 125 | |
| 126 | public File cononical(final File file) { |
| 127 | if (file == null) { |
| 128 | throw new IllegalArgumentException("File may not be null"); |
| 129 | } |
| 130 | |
| 131 | return (File) new Exceptions.Block() { |
| 132 | protected Object inner() throws Exception { |
| 133 | return file.getCanonicalFile(); |
| 134 | } |
| 135 | }.exec(); |
| 136 | } |
| 137 | |
| 138 | public String getPath(final File file) { |
| 139 | return (String) new Exceptions.Block() { |
| 140 | protected Object inner() throws Exception { |
| 141 | return file.getCanonicalPath(); |
| 142 | } |
| 143 | }.exec(); |
| 144 | } |
| 145 | |
| 146 | public boolean cleanTestDir() { |
| 147 | return deleteTree(testDir()); |
| 148 | } |
| 149 | |
| 150 | public File validCononicalDir(File dir, File defaultDir) { |
| 151 | if (dir == null || dir.equals(nullFile())) { |
| 152 | dir = defaultDir; |
| 153 | } |
| 154 | return validCononicalDir(dir); |
| 155 | } |
| 156 | |
| 157 | public File validCononicalDir(final File dir) { |
| 158 | File cononical = cononical(dir); |
| 159 | |
| 160 | if (!cononical.exists()) { |
| 161 | cononical.mkdirs(); |
| 162 | } |
| 163 | if (!cononical.isDirectory()) { |
| 164 | throw new IllegalArgumentException(cononical + " not a directory"); |
| 165 | } |
| 166 | return cononical; |
| 167 | } |
| 168 | } |