|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, Joyent, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 | + */ |
| 8 | +package com.joyent.manta.client; |
| 9 | + |
| 10 | +import com.joyent.manta.exception.MantaClientHttpResponseException; |
| 11 | +import com.joyent.manta.exception.MantaErrorCode; |
| 12 | +import com.joyent.manta.http.MantaHttpHeaders; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | + |
| 18 | +import static com.joyent.manta.util.MantaUtils.writeablePrefixPaths; |
| 19 | + |
| 20 | +/** |
| 21 | + * Utility class for recursive directory creation strategies. |
| 22 | + * |
| 23 | + * @author <a href="https://github.com/tjcelaya">Tomas Celayac</a> |
| 24 | + * @since 3.1.7 |
| 25 | + */ |
| 26 | +final class RecursiveDirectoryCreationStrategy { |
| 27 | + |
| 28 | + private static final Logger LOG = LoggerFactory.getLogger(RecursiveDirectoryCreationStrategy.class); |
| 29 | + |
| 30 | + private RecursiveDirectoryCreationStrategy() { |
| 31 | + } |
| 32 | + |
| 33 | + static long createWithSkipDepth(final MantaClient client, |
| 34 | + final String rawPath, |
| 35 | + final MantaHttpHeaders headers, |
| 36 | + final int skipDepth) throws IOException { |
| 37 | + final String[] paths = writeablePrefixPaths(rawPath); |
| 38 | + |
| 39 | + if (paths.length <= skipDepth) { |
| 40 | + return createCompletely(client, rawPath, headers); |
| 41 | + } |
| 42 | + |
| 43 | + final String assumedExistingDirectory = paths[skipDepth - 1]; |
| 44 | + final String maybeNewDirectory = paths[skipDepth]; |
| 45 | + |
| 46 | + LOG.debug("ASSUME {}", assumedExistingDirectory); |
| 47 | + |
| 48 | + final Boolean redundantPut = createNewDirectory(client, maybeNewDirectory, headers, rawPath); |
| 49 | + long ops = 1; |
| 50 | + |
| 51 | + if (redundantPut == null) { |
| 52 | + LOG.debug("FAILED {}", maybeNewDirectory); |
| 53 | + |
| 54 | + // failed to create directory at the skip depth, proceed normally |
| 55 | + return ops + createCompletely(client, rawPath, headers); |
| 56 | + } |
| 57 | + |
| 58 | + for (int idx = skipDepth + 1; idx < paths.length; idx++) { |
| 59 | + client.putDirectory(paths[idx], headers); |
| 60 | + ops++; |
| 61 | + } |
| 62 | + |
| 63 | + return ops; |
| 64 | + } |
| 65 | + |
| 66 | + static long createCompletely(final MantaClient client, |
| 67 | + final String rawPath, |
| 68 | + final MantaHttpHeaders headers) throws IOException { |
| 69 | + long ops = 0; |
| 70 | + for (final String path : writeablePrefixPaths(rawPath)) { |
| 71 | + client.putDirectory(path, headers); |
| 72 | + ops++; |
| 73 | + } |
| 74 | + |
| 75 | + return ops; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Try to create a directory and unpack the error. The Boolean is intentional and acts as a tri-state variable. |
| 80 | + * |
| 81 | + * NULL = creation failed. |
| 82 | + * TRUE = a new directory was actually created |
| 83 | + * FALSE = the directory already existed |
| 84 | + * |
| 85 | + * @return whether or not the directory was actually new, or null if it failed to be created |
| 86 | + */ |
| 87 | + private static Boolean createNewDirectory(final MantaClient client, |
| 88 | + final String path, |
| 89 | + final MantaHttpHeaders headers, |
| 90 | + final String targetPath) throws IOException { |
| 91 | + try { |
| 92 | + return client.putDirectory(path, headers); |
| 93 | + } catch (final MantaClientHttpResponseException mchre) { |
| 94 | + if (mchre.getServerCode().equals(MantaErrorCode.DIRECTORY_DOES_NOT_EXIST_ERROR)) { |
| 95 | + return null; |
| 96 | + } else { |
| 97 | + mchre.setContextValue("recursiveDirectoryCreationTarget", targetPath); |
| 98 | + throw mchre; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments