Search in sources :

Example 1 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project elasticsearch by elastic.

the class InstallPluginCommandTests method testMissingDescriptor.

public void testMissingDescriptor() throws Exception {
    Tuple<Path, Environment> env = createEnv(fs, temp);
    Path pluginDir = createPluginDir(temp);
    Files.createFile(pluginDir.resolve("fake.yml"));
    String pluginZip = writeZip(pluginDir, "elasticsearch");
    NoSuchFileException e = expectThrows(NoSuchFileException.class, () -> installPlugin(pluginZip, env.v1()));
    assertTrue(e.getMessage(), e.getMessage().contains("plugin-descriptor.properties"));
    assertInstallCleaned(env.v2());
}
Also used : Path(java.nio.file.Path) NoSuchFileException(java.nio.file.NoSuchFileException) Environment(org.elasticsearch.env.Environment) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 2 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project buck by facebook.

the class AppleSdkDiscovery method discoverAppleSdkPaths.

/**
   * Given a path to an Xcode developer directory and a map of
   * (xctoolchain ID: path) pairs as returned by
   * {@link AppleToolchainDiscovery}, walks through the platforms
   * and builds a map of ({@link AppleSdk}: {@link AppleSdkPaths})
   * objects describing the paths to the SDKs inside.
   *
   * The {@link AppleSdk#getName()} strings match the ones displayed by {@code xcodebuild -showsdks}
   * and look like {@code macosx10.9}, {@code iphoneos8.0}, {@code iphonesimulator8.0},
   * etc.
   */
public static ImmutableMap<AppleSdk, AppleSdkPaths> discoverAppleSdkPaths(Optional<Path> developerDir, ImmutableList<Path> extraDirs, ImmutableMap<String, AppleToolchain> xcodeToolchains, AppleConfig appleConfig) throws IOException {
    Optional<AppleToolchain> defaultToolchain = Optional.ofNullable(xcodeToolchains.get(DEFAULT_TOOLCHAIN_ID));
    ImmutableMap.Builder<AppleSdk, AppleSdkPaths> appleSdkPathsBuilder = ImmutableMap.builder();
    HashSet<Path> platformPaths = new HashSet<Path>(extraDirs);
    if (developerDir.isPresent()) {
        Path platformsDir = developerDir.get().resolve("Platforms");
        LOG.debug("Searching for Xcode platforms under %s", platformsDir);
        platformPaths.add(platformsDir);
    }
    // We need to find the most recent SDK for each platform so we can
    // make the fall-back SDKs with no version number in their name
    // ("macosx", "iphonesimulator", "iphoneos").
    //
    // To do this, we store a map of (platform: [sdk1, sdk2, ...])
    // pairs where the SDKs for each platform are ordered by version.
    TreeMultimap<ApplePlatform, AppleSdk> orderedSdksForPlatform = TreeMultimap.create(Ordering.natural(), APPLE_SDK_VERSION_ORDERING);
    for (Path platforms : platformPaths) {
        if (!Files.exists(platforms)) {
            LOG.debug("Skipping platform search path %s that does not exist", platforms);
            continue;
        }
        LOG.debug("Searching for Xcode SDKs in %s", platforms);
        try (DirectoryStream<Path> platformStream = Files.newDirectoryStream(platforms, "*.platform")) {
            for (Path platformDir : platformStream) {
                Path developerSdksPath = platformDir.resolve("Developer/SDKs");
                try (DirectoryStream<Path> sdkStream = Files.newDirectoryStream(developerSdksPath, "*.sdk")) {
                    Set<Path> scannedSdkDirs = new HashSet<>();
                    for (Path sdkDir : sdkStream) {
                        LOG.debug("Fetching SDK name for %s", sdkDir);
                        sdkDir = sdkDir.toRealPath();
                        if (scannedSdkDirs.contains(sdkDir)) {
                            LOG.debug("Skipping already scanned SDK directory %s", sdkDir);
                            continue;
                        }
                        AppleSdk.Builder sdkBuilder = AppleSdk.builder();
                        if (buildSdkFromPath(sdkDir, sdkBuilder, xcodeToolchains, defaultToolchain, appleConfig)) {
                            AppleSdk sdk = sdkBuilder.build();
                            LOG.debug("Found SDK %s", sdk);
                            AppleSdkPaths.Builder xcodePathsBuilder = AppleSdkPaths.builder();
                            for (AppleToolchain toolchain : sdk.getToolchains()) {
                                xcodePathsBuilder.addToolchainPaths(toolchain.getPath());
                            }
                            AppleSdkPaths xcodePaths = xcodePathsBuilder.setDeveloperPath(developerDir).setPlatformPath(platformDir).setSdkPath(sdkDir).build();
                            appleSdkPathsBuilder.put(sdk, xcodePaths);
                            orderedSdksForPlatform.put(sdk.getApplePlatform(), sdk);
                        }
                        scannedSdkDirs.add(sdkDir);
                    }
                } catch (NoSuchFileException e) {
                    LOG.warn(e, "Couldn't discover SDKs at path %s, ignoring platform %s", developerSdksPath, platformDir);
                }
            }
        }
    }
    // Get a snapshot of what's in appleSdkPathsBuilder, then for each
    // ApplePlatform, add to appleSdkPathsBuilder the most recent
    // SDK with an unversioned name.
    ImmutableMap<AppleSdk, AppleSdkPaths> discoveredSdkPaths = appleSdkPathsBuilder.build();
    for (ApplePlatform platform : orderedSdksForPlatform.keySet()) {
        Set<AppleSdk> platformSdks = orderedSdksForPlatform.get(platform);
        boolean shouldCreateUnversionedSdk = true;
        for (AppleSdk sdk : platformSdks) {
            shouldCreateUnversionedSdk &= !sdk.getName().equals(platform.getName());
        }
        if (shouldCreateUnversionedSdk) {
            AppleSdk mostRecentSdkForPlatform = orderedSdksForPlatform.get(platform).last();
            appleSdkPathsBuilder.put(mostRecentSdkForPlatform.withName(platform.getName()), discoveredSdkPaths.get(mostRecentSdkForPlatform));
        }
    }
    // the unversioned aliases added just above.
    return appleSdkPathsBuilder.build();
}
Also used : Path(java.nio.file.Path) NoSuchFileException(java.nio.file.NoSuchFileException) ImmutableMap(com.google.common.collect.ImmutableMap) HashSet(java.util.HashSet)

Example 3 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project buck by facebook.

the class AppleSdkDiscovery method buildSdkFromPath.

private static boolean buildSdkFromPath(Path sdkDir, AppleSdk.Builder sdkBuilder, ImmutableMap<String, AppleToolchain> xcodeToolchains, Optional<AppleToolchain> defaultToolchain, AppleConfig appleConfig) throws IOException {
    try (InputStream sdkSettingsPlist = Files.newInputStream(sdkDir.resolve("SDKSettings.plist"));
        BufferedInputStream bufferedSdkSettingsPlist = new BufferedInputStream(sdkSettingsPlist)) {
        NSDictionary sdkSettings;
        try {
            sdkSettings = (NSDictionary) PropertyListParser.parse(bufferedSdkSettingsPlist);
        } catch (PropertyListFormatException | ParseException | SAXException e) {
            LOG.error(e, "Malformatted SDKSettings.plist. Skipping SDK path %s.", sdkDir);
            return false;
        } catch (ParserConfigurationException e) {
            throw new IOException(e);
        }
        String name = sdkSettings.objectForKey("CanonicalName").toString();
        String version = sdkSettings.objectForKey("Version").toString();
        NSDictionary defaultProperties = (NSDictionary) sdkSettings.objectForKey("DefaultProperties");
        Optional<ImmutableList<String>> toolchains = appleConfig.getToolchainsOverrideForSDKName(name);
        boolean foundToolchain = false;
        if (!toolchains.isPresent()) {
            NSArray settingsToolchains = (NSArray) sdkSettings.objectForKey("Toolchains");
            if (settingsToolchains != null) {
                toolchains = Optional.of(Arrays.stream(settingsToolchains.getArray()).map(Object::toString).collect(MoreCollectors.toImmutableList()));
            }
        }
        if (toolchains.isPresent()) {
            for (String toolchainId : toolchains.get()) {
                AppleToolchain toolchain = xcodeToolchains.get(toolchainId);
                if (toolchain != null) {
                    foundToolchain = true;
                    sdkBuilder.addToolchains(toolchain);
                } else {
                    LOG.debug("Specified toolchain %s not found for SDK path %s", toolchainId, sdkDir);
                }
            }
        }
        if (!foundToolchain && defaultToolchain.isPresent()) {
            foundToolchain = true;
            sdkBuilder.addToolchains(defaultToolchain.get());
        }
        if (!foundToolchain) {
            LOG.warn("No toolchains found and no default toolchain. Skipping SDK path %s.", sdkDir);
            return false;
        } else {
            NSString platformName = (NSString) defaultProperties.objectForKey("PLATFORM_NAME");
            ApplePlatform applePlatform = ApplePlatform.of(platformName.toString());
            sdkBuilder.setName(name).setVersion(version).setApplePlatform(applePlatform);
            ImmutableList<String> architectures = validArchitecturesForPlatform(applePlatform, sdkDir);
            sdkBuilder.addAllArchitectures(architectures);
            return true;
        }
    } catch (NoSuchFileException e) {
        LOG.warn(e, "Skipping SDK at path %s, no SDKSettings.plist found", sdkDir);
        return false;
    }
}
Also used : NSArray(com.dd.plist.NSArray) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) NSDictionary(com.dd.plist.NSDictionary) ImmutableList(com.google.common.collect.ImmutableList) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) NSString(com.dd.plist.NSString) NSString(com.dd.plist.NSString) SAXException(org.xml.sax.SAXException) PropertyListFormatException(com.dd.plist.PropertyListFormatException) BufferedInputStream(java.io.BufferedInputStream) ParseException(java.text.ParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 4 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project buck by facebook.

the class AppleSimulatorDiscovery method discoverAppleSimulatorProfile.

/**
   * Given a simulators, looks up metadata on the supported architectures
   * and product families for that simulator (if present).
   */
public static Optional<AppleSimulatorProfile> discoverAppleSimulatorProfile(AppleSimulator appleSimulator, Path iphonesimulatorPlatformRoot) throws IOException {
    Path simulatorProfilePlistPath = iphonesimulatorPlatformRoot.resolve(String.format("Developer/Library/CoreSimulator/Profiles/DeviceTypes/%s.simdevicetype/" + "Contents/Resources/profile.plist", appleSimulator.getName()));
    LOG.debug("Parsing simulator profile plist %s", simulatorProfilePlistPath);
    try (InputStream inputStream = Files.newInputStream(simulatorProfilePlistPath)) {
        // This might return Optional.empty() if the input could not be parsed.
        return AppleSimulatorProfileParsing.parseProfilePlistStream(inputStream);
    } catch (FileNotFoundException | NoSuchFileException e) {
        LOG.warn(e, "Could not open simulator profile %s, ignoring", simulatorProfilePlistPath);
        return Optional.empty();
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException)

Example 5 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project elasticsearch by elastic.

the class IndexFolderUpgrader method upgrade.

/**
     * Moves the index folder found in <code>source</code> to <code>target</code>
     */
void upgrade(final Index index, final Path source, final Path target) throws IOException {
    boolean success = false;
    try {
        Files.move(source, target, StandardCopyOption.ATOMIC_MOVE);
        success = true;
    } catch (NoSuchFileException | FileNotFoundException exception) {
        // thrown when the source is non-existent because the folder was renamed
        // by another node (shared FS) after we checked if the target exists
        logger.error((Supplier<?>) () -> new ParameterizedMessage("multiple nodes trying to upgrade [{}] in parallel, retry " + "upgrading with single node", target), exception);
        throw exception;
    } finally {
        if (success) {
            logger.info("{} moved from [{}] to [{}]", index, source, target);
            logger.trace("{} syncing directory [{}]", index, target);
            IOUtils.fsync(target, true);
        }
    }
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) FileNotFoundException(java.io.FileNotFoundException) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Aggregations

NoSuchFileException (java.nio.file.NoSuchFileException)262 IOException (java.io.IOException)107 Path (java.nio.file.Path)104 FileNotFoundException (java.io.FileNotFoundException)41 Test (org.junit.Test)35 InputStream (java.io.InputStream)31 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)25 File (java.io.File)22 NotDirectoryException (java.nio.file.NotDirectoryException)19 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)18 ArrayList (java.util.ArrayList)16 HashSet (java.util.HashSet)16 OutputStream (java.io.OutputStream)15 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)15 FileChannel (java.nio.channels.FileChannel)14 AccessDeniedException (java.nio.file.AccessDeniedException)14 ByteBuffer (java.nio.ByteBuffer)13 HashMap (java.util.HashMap)13 Map (java.util.Map)12 SeekableByteChannel (java.nio.channels.SeekableByteChannel)11