* Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * \ * with the License. You may obtain a copy of the License at *
* http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an \
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
package org.apache.hadoop.fs;
import java.io.*;
import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile;
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.permission.FsAction; import org.apache.hadoop.fs.permission.FsPermission; import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.nativeio.NativeIO; import org.apache.hadoop.util.StringUtils; import org.apache.hadoop.util.Shell;
import org.apache.hadoop.util.Shell.ShellCommandExecutor; /**
* A collection of file-processing util methods */
public class FileUtil {
private static final Log LOG = LogFactory.getLog(FileUtil.class); /**
* convert an array of FileStatus to an array of Path *
* @param stats
* an array of FileStatus objects
* @return an array of paths corresponding to the input */
public static Path[] stat2Paths(FileStatus[] stats) { if (stats == null) return null;
Path[] ret = new Path[stats.length]; for (int i = 0; i < stats.length; ++i) { ret[i] = stats[i].getPath(); } return ret; } /**
* convert an array of FileStatus to an array of Path. * If stats if null, return path * @param stats
* an array of FileStatus objects * @param path
* default path to return in stats is null * @return an array of paths corresponding to the input */
public static Path[] stat2Paths(FileStatus[] stats, Path path) { if (stats == null)
return new Path[]{path}; else
return stat2Paths(stats); } /**
* Delete a directory and all its contents. If
* we return false, the directory may be partially-deleted. */
public static boolean fullyDelete(File dir) throws IOException { if (!fullyDeleteContents(dir)) { return false; }
return dir.delete(); } /**
* Delete the contents of a directory, not the directory itself. If * we return false, the directory may be partially-deleted. */
public static boolean fullyDeleteContents(File dir) throws IOException { boolean deletionSucceeded = true; File contents[] = dir.listFiles(); if (contents != null) {
for (int i = 0; i < contents.length; i++) { if (contents[i].isFile()) { if (!contents[i].delete()) { deletionSucceeded = false;
continue; // continue deletion of other files/dirs under dir } } else {
//try deleting the directory // this might be a symlink boolean b = false; b = contents[i].delete(); if (b){
//this was indeed a symlink or an empty directory
continue; }
// if not an empty directory or symlink let // fullydelete handle it. if (!fullyDelete(contents[i])) { deletionSucceeded = false;
continue; // continue deletion of other files/dirs under dir } } } }
return deletionSucceeded; } /**
* Recursively delete a directory. *
* @param fs {@link FileSystem} on which the path is present * @param dir directory to recursively delete * @throws IOException
* @deprecated Use {@link FileSystem#delete(Path, boolean)} */
@Deprecated
public static void fullyDelete(FileSystem fs, Path dir) throws IOException { fs.delete(dir, true); } //
// If the destination is a subdirectory of the source, then // generate exception //
private static void checkDependencies(FileSystem srcFS, Path src,
FileSystem dstFS,
Path dst)
throws IOException { if (srcFS == dstFS) {
String srcq = src.makeQualified(srcFS).toString() + Path.SEPARATOR; String dstq = dst.makeQualified(dstFS).toString() + Path.SEPARATOR; if (dstq.startsWith(srcq)) {
if (srcq.length() == dstq.length()) {
throw new IOException(\ } else {
throw new IOException(\ dst); } } } }
/** Copy files between FileSystems. */
public static boolean copy(FileSystem srcFS, Path src, FileSystem dstFS, Path dst, boolean deleteSource,
Configuration conf) throws IOException { return copy(srcFS, src, dstFS, dst, deleteSource, true, conf); }
public static boolean copy(FileSystem srcFS, Path[] srcs, FileSystem dstFS, Path dst, boolean deleteSource,
boolean overwrite, Configuration conf) throws IOException { boolean gotException = false; boolean returnVal = true;
StringBuffer exceptions = new StringBuffer();
if (srcs.length == 1)
return copy(srcFS, srcs[0], dstFS, dst, deleteSource, overwrite, conf);
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库hadoop单机部署、集群部署及win7本地Eclipse远程配置管理(4)在线全文阅读。
相关推荐: