Search in sources :

Example 1 with Rename

use of org.apache.hadoop.fs.Options.Rename in project hadoop by apache.

the class FileSystem method rename.

/**
   * Renames Path src to Path dst
   * <ul>
   *   <li>Fails if src is a file and dst is a directory.</li>
   *   <li>Fails if src is a directory and dst is a file.</li>
   *   <li>Fails if the parent of dst does not exist or is a file.</li>
   * </ul>
   * <p>
   * If OVERWRITE option is not passed as an argument, rename fails
   * if the dst already exists.
   * <p>
   * If OVERWRITE option is passed as an argument, rename overwrites
   * the dst if it is a file or an empty directory. Rename fails if dst is
   * a non-empty directory.
   * <p>
   * Note that atomicity of rename is dependent on the file system
   * implementation. Please refer to the file system documentation for
   * details. This default implementation is non atomic.
   * <p>
   * This method is deprecated since it is a temporary method added to
   * support the transition from FileSystem to FileContext for user
   * applications.
   *
   * @param src path to be renamed
   * @param dst new path after rename
   * @throws FileNotFoundException src path does not exist, or the parent
   * path of dst does not exist.
   * @throws FileAlreadyExistsException dest path exists and is a file
   * @throws ParentNotDirectoryException if the parent path of dest is not
   * a directory
   * @throws IOException on failure
   */
@Deprecated
protected void rename(final Path src, final Path dst, final Rename... options) throws IOException {
    // Default implementation
    final FileStatus srcStatus = getFileLinkStatus(src);
    if (srcStatus == null) {
        throw new FileNotFoundException("rename source " + src + " not found.");
    }
    boolean overwrite = false;
    if (null != options) {
        for (Rename option : options) {
            if (option == Rename.OVERWRITE) {
                overwrite = true;
            }
        }
    }
    FileStatus dstStatus;
    try {
        dstStatus = getFileLinkStatus(dst);
    } catch (IOException e) {
        dstStatus = null;
    }
    if (dstStatus != null) {
        if (srcStatus.isDirectory() != dstStatus.isDirectory()) {
            throw new IOException("Source " + src + " Destination " + dst + " both should be either file or directory");
        }
        if (!overwrite) {
            throw new FileAlreadyExistsException("rename destination " + dst + " already exists.");
        }
        // Delete the destination that is a file or an empty directory
        if (dstStatus.isDirectory()) {
            FileStatus[] list = listStatus(dst);
            if (list != null && list.length != 0) {
                throw new IOException("rename cannot overwrite non empty destination directory " + dst);
            }
        }
        delete(dst, false);
    } else {
        final Path parent = dst.getParent();
        final FileStatus parentStatus = getFileStatus(parent);
        if (parentStatus == null) {
            throw new FileNotFoundException("rename destination parent " + parent + " not found.");
        }
        if (!parentStatus.isDirectory()) {
            throw new ParentNotDirectoryException("rename destination parent " + parent + " is a file.");
        }
    }
    if (!rename(src, dst)) {
        throw new IOException("rename from " + src + " to " + dst + " failed.");
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) MultipleIOException(org.apache.hadoop.io.MultipleIOException) Rename(org.apache.hadoop.fs.Options.Rename)

Example 2 with Rename

use of org.apache.hadoop.fs.Options.Rename in project hadoop by apache.

the class AbstractFileSystem method rename.

/**
   * The specification of this method matches that of
   * {@link FileContext#rename(Path, Path, Options.Rename...)} except that Path
   * f must be for this file system.
   */
public final void rename(final Path src, final Path dst, final Options.Rename... options) throws AccessControlException, FileAlreadyExistsException, FileNotFoundException, ParentNotDirectoryException, UnresolvedLinkException, IOException {
    boolean overwrite = false;
    if (null != options) {
        for (Rename option : options) {
            if (option == Rename.OVERWRITE) {
                overwrite = true;
            }
        }
    }
    renameInternal(src, dst, overwrite);
}
Also used : Rename(org.apache.hadoop.fs.Options.Rename)

Example 3 with Rename

use of org.apache.hadoop.fs.Options.Rename in project hadoop by apache.

the class TestFilterFileSystem method testRenameOptions.

@Test
public void testRenameOptions() throws Exception {
    FileSystem mockFs = mock(FileSystem.class);
    FileSystem fs = new FilterFileSystem(mockFs);
    Path src = new Path("/src");
    Path dst = new Path("/dest");
    Rename opt = Rename.TO_TRASH;
    fs.rename(src, dst, opt);
    verify(mockFs).rename(eq(src), eq(dst), eq(opt));
}
Also used : Rename(org.apache.hadoop.fs.Options.Rename) Test(org.junit.Test)

Example 4 with Rename

use of org.apache.hadoop.fs.Options.Rename in project hadoop by apache.

the class ClientNamenodeProtocolTranslatorPB method rename2.

@Override
public void rename2(String src, String dst, Rename... options) throws IOException {
    boolean overwrite = false;
    boolean toTrash = false;
    if (options != null) {
        for (Rename option : options) {
            if (option == Rename.OVERWRITE) {
                overwrite = true;
            } else if (option == Rename.TO_TRASH) {
                toTrash = true;
            }
        }
    }
    Rename2RequestProto req = Rename2RequestProto.newBuilder().setSrc(src).setDst(dst).setOverwriteDest(overwrite).setMoveToTrash(toTrash).build();
    try {
        if (Client.isAsynchronousMode()) {
            rpcProxy.rename2(null, req);
            setAsyncReturnValue();
        } else {
            rpcProxy.rename2(null, req);
        }
    } catch (ServiceException e) {
        throw ProtobufHelper.getRemoteException(e);
    }
}
Also used : Rename2RequestProto(org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.Rename2RequestProto) ServiceException(com.google.protobuf.ServiceException) Rename(org.apache.hadoop.fs.Options.Rename)

Example 5 with Rename

use of org.apache.hadoop.fs.Options.Rename in project hadoop by apache.

the class AdlFileSystem method rename.

@Override
@Deprecated
public void rename(final Path src, final Path dst, final Options.Rename... options) throws IOException {
    statistics.incrementWriteOps(1);
    boolean overwrite = false;
    for (Rename renameOption : options) {
        if (renameOption == Rename.OVERWRITE) {
            overwrite = true;
            break;
        }
    }
    adlClient.rename(toRelativeFilePath(src), toRelativeFilePath(dst), overwrite);
}
Also used : Rename(org.apache.hadoop.fs.Options.Rename)

Aggregations

Rename (org.apache.hadoop.fs.Options.Rename)5 ServiceException (com.google.protobuf.ServiceException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Rename2RequestProto (org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.Rename2RequestProto)1 MultipleIOException (org.apache.hadoop.io.MultipleIOException)1 Test (org.junit.Test)1