FileUtilities.java revision 1470
30N/A/*
30N/A * CDDL HEADER START
30N/A *
30N/A * The contents of this file are subject to the terms of the
30N/A * Common Development and Distribution License (the "License").
30N/A * You may not use this file except in compliance with the License.
30N/A *
30N/A * See LICENSE.txt included in this distribution for the specific
30N/A * language governing permissions and limitations under the License.
30N/A *
30N/A * When distributing Covered Code, include this CDDL HEADER in each
30N/A * file and include the License file at LICENSE.txt.
30N/A * If applicable, add the following below this CDDL HEADER, with the
30N/A * fields enclosed by brackets "[]" replaced with your own identifying
30N/A * information: Portions Copyright [yyyy] [name of copyright owner]
30N/A *
30N/A * CDDL HEADER END
30N/A */
30N/A
30N/A/*
30N/A * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
30N/A */
30N/Apackage org.opensolaris.opengrok.util;
30N/A
30N/Aimport java.io.File;
58N/Aimport java.io.FileOutputStream;
58N/Aimport java.io.IOException;
58N/Aimport java.io.InputStream;
99N/Aimport java.io.OutputStream;
30N/Aimport java.util.Enumeration;
30N/Aimport java.util.List;
99N/Aimport java.util.zip.ZipEntry;
58N/Aimport java.util.zip.ZipFile;
58N/A
58N/Aimport org.opensolaris.opengrok.configuration.Configuration;
58N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
58N/Aimport org.opensolaris.opengrok.index.IgnoredNames;
58N/Aimport static org.junit.Assert.*;
58N/A
58N/A/**
290N/A * Various filesystem utilities used by the different test setups
112N/A *
30N/A * @author Trond Norbye
30N/A */
30N/Apublic class FileUtilities {
58N/A
30N/A /**
30N/A * Extract the source bundle to the given path.
58N/A * @param sourceBundle bundle to extract
145N/A * @param root target dir
30N/A * @throws IOException
30N/A */
30N/A @SuppressWarnings("resource")
77N/A public static void extractArchive(File sourceBundle, File root)
77N/A throws IOException
77N/A {
77N/A ZipFile zipfile = new ZipFile(sourceBundle);
30N/A
30N/A Enumeration<? extends ZipEntry> e = zipfile.entries();
30N/A
30N/A while (e.hasMoreElements()) {
30N/A ZipEntry ze = e.nextElement();
77N/A File file = new File(root, ze.getName());
77N/A if (ze.isDirectory()) {
30N/A file.mkdirs();
30N/A } else {
58N/A InputStream in = null;
145N/A FileOutputStream out = null;
145N/A try {
145N/A in = zipfile.getInputStream(ze);
145N/A assertNotNull(in);
145N/A out = new FileOutputStream(file);
58N/A assertNotNull(out);
77N/A copyFile(in, out);
77N/A } finally {
77N/A IOUtils.close(in);
77N/A IOUtils.close(out);
77N/A }
58N/A }
145N/A }
58N/A }
58N/A
77N/A /**
77N/A * Delete the given directory (rm -rf).
77N/A * @param root directory to delete.
77N/A */
30N/A public static void removeDirs(File root) {
58N/A for (File f : root.listFiles()) {
58N/A if (f.isDirectory()) {
58N/A removeDirs(f);
58N/A } else {
58N/A f.delete();
58N/A }
58N/A }
30N/A root.delete();
58N/A }
77N/A
77N/A /**
77N/A * Copy th given input to the given output 1:1.
77N/A * @param in source to copy
77N/A * @param out destination for copy
77N/A * @throws IOException
145N/A */
77N/A public static void copyFile(InputStream in, OutputStream out) throws IOException {
77N/A byte[] array = new byte[8192];
77N/A int nr;
77N/A
77N/A while ((nr = in.read(array)) > 0) {
77N/A out.write(array, 0, nr);
77N/A }
145N/A out.flush();
77N/A }
77N/A
77N/A
77N/A /**
77N/A * Get all files of the given directory and all its descendants obeying
77N/A * {@link Configuration#getIgnoredNames()}.
77N/A * @param root where to start the search
145N/A * @param files where to store files found
77N/A * @param directories if {@code true} add directories to <var>files</var>
77N/A * as well.
77N/A */
77N/A public static void getAllFiles(File root, List<File> files, boolean directories) {
77N/A assertNotNull(files);
77N/A if (directories) {
30N/A files.add(root);
58N/A }
58N/A
58N/A IgnoredNames ignore = RuntimeEnvironment.getConfig().getIgnoredNames();
58N/A for (File f : root.listFiles()) {
58N/A if (!ignore.ignore(f)) {
58N/A if (f.isDirectory()) {
58N/A getAllFiles(f, files, directories);
58N/A } else {
58N/A files.add(f);
77N/A }
77N/A }
77N/A }
77N/A }
77N/A
125N/A /**
145N/A * Create an empty directory under {@code /tmp} or similar.
58N/A *
58N/A * @param prefix string to prefix the directory name with
77N/A * @return a {@code File} object pointing to the directory
77N/A * @throws IOException if the temporary directory cannot be created
77N/A */
77N/A public static File createTemporaryDirectory(String prefix)
58N/A throws IOException {
145N/A File file = File.createTempFile(prefix, "opengrok");
58N/A if (!file.delete()) {
58N/A throw new IOException(
77N/A "Could not create delete temporary file " + file);
77N/A }
77N/A if (!file.mkdir()) {
77N/A throw new IOException(
58N/A "Could not create temporary directory " + file);
58N/A }
58N/A return file;
58N/A }
58N/A
77N/A private FileUtilities() {
77N/A }
77N/A
77N/A /**
58N/A * Determine if given program is present in one of the directories
145N/A * in PATH environment variable.
58N/A *
58N/A * @param progName name of the program
77N/A * @return absolute path to the program or null
77N/A */
77N/A public static File findProgInPath(String progName) {
77N/A String systemPath = System.getenv("PATH");
58N/A if (systemPath == null) {
145N/A return null;
58N/A }
58N/A
77N/A String[] pathDirs = systemPath.split(File.pathSeparator);
77N/A File absoluteFile = null;
77N/A
77N/A for (String dir : pathDirs) {
77N/A File file = new File(dir, progName);
58N/A if (file.isFile() && file.canExecute()) {
145N/A absoluteFile = file;
58N/A break;
58N/A }
77N/A }
77N/A return absoluteFile;
77N/A }
77N/A}
58N/A