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/*
1336N/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;
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
377N/A public static void extractArchive(File sourceBundle, File root) throws IOException {
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 {
377N/A InputStream in = zipfile.getInputStream(ze);
377N/A assertNotNull(in);
377N/A FileOutputStream out = new FileOutputStream(file);
377N/A assertNotNull(out);
377N/A copyFile(in, out);
377N/A }
377N/A }
377N/A }
377N/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
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
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
377N/A IgnoredNames ignore = RuntimeEnvironment.getInstance().getIgnoredNames();
377N/A
377N/A for (File f : root.listFiles()) {
377N/A if (!ignore.ignore(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
1336N/A /**
1336N/A * Determine if given program is present in one of the directories
1336N/A * in PATH environment variable.
1336N/A *
1336N/A * @param progName name of the program
1336N/A * @return absolute path to the program or null
1336N/A */
1336N/A public static File findProgInPath(String progName) {
1336N/A String systemPath = System.getenv("PATH");
1336N/A if (systemPath == null) {
1336N/A return null;
1336N/A }
1336N/A
1336N/A String[] pathDirs = systemPath.split(File.pathSeparator);
1336N/A File absoluteFile = null;
1336N/A
1336N/A for (String dir : pathDirs) {
1336N/A File file = new File(dir, progName);
1336N/A if (file.isFile() && file.canExecute()) {
1336N/A absoluteFile = file;
1336N/A break;
1336N/A }
1336N/A }
1336N/A return absoluteFile;
1336N/A }
377N/A}