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