diff options
Diffstat (limited to 'node-admin/src/main/java/com')
5 files changed, 36 insertions, 38 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/container/Cgroup.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/container/Cgroup.java index c7616abd508..1d87415b78e 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/container/Cgroup.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/container/Cgroup.java @@ -5,12 +5,7 @@ package com.yahoo.vespa.hosted.node.admin.container; import com.yahoo.vespa.hosted.node.admin.nodeagent.NodeAgentContext; import com.yahoo.vespa.hosted.node.admin.task.util.file.UnixPath; -import java.io.IOException; -import java.io.UncheckedIOException; -import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; -import java.nio.file.Files; -import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.util.OptionalInt; import java.util.logging.Logger; @@ -85,17 +80,9 @@ public class Cgroup { } private OptionalInt readCgroupsCpuInt(UnixPath unixPath) { - final byte[] currentContentBytes; - try { - currentContentBytes = Files.readAllBytes(unixPath.toPath()); - } catch (NoSuchFileException e) { - return OptionalInt.empty(); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - - String currentContent = new String(currentContentBytes, StandardCharsets.UTF_8).strip(); - return OptionalInt.of(Integer.parseInt(currentContent)); + return unixPath.readUtf8FileIfExists() + .map(s -> OptionalInt.of(Integer.parseInt(s.strip()))) + .orElseGet(OptionalInt::empty); } private boolean writeCgroupsCpuInt(NodeAgentContext context, UnixPath unixPath, int value) { diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/identity/AthenzCredentialsMaintainer.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/identity/AthenzCredentialsMaintainer.java index bd01b320666..23fbf01e14b 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/identity/AthenzCredentialsMaintainer.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/identity/AthenzCredentialsMaintainer.java @@ -266,11 +266,9 @@ public class AthenzCredentialsMaintainer implements CredentialsMaintainer { } private static void writeFile(Path path, int vespaUidOnHost, String utf8Content) { - new UnixPath(path.toString() + ".tmp") - .deleteIfExists() - .createNewFile("r--------") + new UnixPath(path.resolveSibling(path.getFileName() + ".tmp")) + .writeUtf8File(utf8Content, "r--------") .setOwnerId(vespaUidOnHost) - .writeUtf8File(utf8Content) .atomicMove(path); } diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/servicedump/VespaServiceDumperImpl.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/servicedump/VespaServiceDumperImpl.java index 23a6ed2aa8c..27b3467163c 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/servicedump/VespaServiceDumperImpl.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/servicedump/VespaServiceDumperImpl.java @@ -100,8 +100,7 @@ public class VespaServiceDumperImpl implements VespaServiceDumper { directoryOnHost.deleteRecursively(); } context.log(log, Level.INFO, "Creating '" + directoryOnHost +"'."); - directoryOnHost.createDirectory(); - directoryOnHost.setPermissions("rwxrwxrwx"); + directoryOnHost.createDirectory("rwxrwxrwx"); URI destination = serviceDumpDestination(nodeSpec, createDumpId(request)); ProducerContext producerCtx = new ProducerContext(context, directoryInNode.toPath(), request); List<Artifact> producedArtifacts = new ArrayList<>(); diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/UserNamespace.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/UserNamespace.java index 020a8b6c7f6..5b53879ceec 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/UserNamespace.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/UserNamespace.java @@ -1,3 +1,4 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.node.admin.nodeagent; /** diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java index 2ce49ae383a..6949f648865 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java @@ -3,6 +3,8 @@ package com.yahoo.vespa.hosted.node.admin.task.util.file; import java.io.IOException; import java.io.UncheckedIOException; +import java.nio.ByteBuffer; +import java.nio.channels.SeekableByteChannel; import java.nio.charset.StandardCharsets; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; @@ -12,6 +14,7 @@ import java.nio.file.OpenOption; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; +import java.nio.file.StandardOpenOption; import java.nio.file.attribute.FileAttribute; import java.nio.file.attribute.FileTime; import java.nio.file.attribute.GroupPrincipal; @@ -35,6 +38,10 @@ import static com.yahoo.yolean.Exceptions.uncheck; */ // @Immutable public class UnixPath { + + private static final Set<OpenOption> DEFAULT_OPEN_OPTIONS = + Set.of(StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE); + private final Path path; public UnixPath(Path path) { this.path = path; } @@ -97,13 +104,29 @@ public class UnixPath { return writeBytes(content.getBytes(StandardCharsets.UTF_8), options); } + public UnixPath writeUtf8File(String content, String permissions, OpenOption... options) { + return writeBytes(content.getBytes(StandardCharsets.UTF_8), permissions, options); + } + public UnixPath writeBytes(byte[] content, OpenOption... options) { - uncheck(() -> Files.write(path, content, options)); - return this; + return writeBytes(content, null, options); } - public UnixPath atomicWriteUt8(String content) { - return atomicWriteBytes(content.getBytes(StandardCharsets.UTF_8)); + public UnixPath writeBytes(byte[] content, String permissions, OpenOption... options) { + FileAttribute<?>[] attributes = Optional.ofNullable(permissions) + .map(this::getPosixFilePermissionsFromString) + .map(PosixFilePermissions::asFileAttribute) + .map(attribute -> new FileAttribute<?>[]{attribute}) + .orElseGet(() -> new FileAttribute<?>[0]); + + Set<OpenOption> optionsSet = options.length == 0 ? DEFAULT_OPEN_OPTIONS : Set.of(options); + + try (SeekableByteChannel channel = Files.newByteChannel(path, optionsSet, attributes)) { + channel.write(ByteBuffer.wrap(content)); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + return this; } /** Write a file to the same dir as this, and then atomically move it to this' path. */ @@ -184,18 +207,8 @@ public class UnixPath { return this; } - public UnixPath createNewFile(String permissions) { - FileAttribute<?> attribute = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(permissions)); - uncheck(() -> Files.createFile(path, attribute)); - return this; - } - public UnixPath createParents() { - Path parent = path.getParent(); - if (!Files.isDirectory(parent)) { - uncheck(() -> Files.createDirectories(parent)); - } - + uncheck(() -> Files.createDirectories(path.getParent())); return this; } @@ -275,7 +288,7 @@ public class UnixPath { } catch (NoSuchFileException ignored) { return Stream.empty(); } catch (IOException e) { - throw new RuntimeException("Failed to list contents of directory " + path.toAbsolutePath(), e); + throw new UncheckedIOException("Failed to list contents of directory " + path.toAbsolutePath(), e); } } |