src/jdk.jpackage/share/classes/jdk/jpackage/internal/PathGroup.java
author herrick
Tue, 24 Sep 2019 13:43:58 -0400
branchJDK-8200758-branch
changeset 58302 718bd56695b3
parent 58301 e0efb29609bd
child 58670 6fb9e12d5595
permissions -rw-r--r--
8231277 : Adjust Linux application image layout Submitted-by: asemenyuk Reviewed-by: herrick, almatvee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58301
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
     1
/*
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
     4
 *
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    10
 *
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    15
 * accompanied this code).
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    16
 *
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    20
 *
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    23
 * questions.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    24
 */
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    25
package jdk.jpackage.internal;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    26
58302
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
    27
import java.io.File;
58301
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    28
import java.io.IOException;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    29
import java.nio.file.Files;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    30
import java.nio.file.Path;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    31
import java.util.ArrayList;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    32
import java.util.Collection;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    33
import java.util.Collections;
58302
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
    34
import java.util.HashMap;
58301
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    35
import java.util.List;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    36
import java.util.Map;
58302
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
    37
import java.util.function.BiFunction;
58301
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    38
import java.util.stream.Collectors;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    39
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    40
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    41
/**
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    42
 * Group of paths.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    43
 * Each path in the group is assigned a unique id.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    44
 */
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    45
final class PathGroup {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    46
    PathGroup(Map<Object, Path> paths) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    47
        entries = Collections.unmodifiableMap(paths);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    48
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    49
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    50
    Path getPath(Object id) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    51
        return entries.get(id);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    52
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    53
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    54
    /**
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    55
     * All configured entries.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    56
     */
58302
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
    57
    List<Path> paths() {
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
    58
        return entries.values().stream().collect(Collectors.toList());
58301
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    59
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    60
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    61
    /**
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    62
     * Root entries.
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    63
     */
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    64
    List<Path> roots() {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    65
        // Sort by the number of path components in ascending order.
58302
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
    66
        List<Map.Entry<Path, Path>> sorted = normalizedPaths().stream().sorted(
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
    67
                (a, b) -> a.getKey().getNameCount() - b.getKey().getNameCount()).collect(
58301
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    68
                        Collectors.toList());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    69
58302
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
    70
        // Returns `true` if `a` is a parent of `b`
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
    71
        BiFunction<Map.Entry<Path, Path>, Map.Entry<Path, Path>, Boolean> isParentOrSelf = (a, b) -> {
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
    72
            return a == b || b.getKey().startsWith(a.getKey());
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
    73
        };
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
    74
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
    75
        return sorted.stream().filter(
58301
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    76
                v -> v == sorted.stream().sequential().filter(
58302
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
    77
                        v2 -> isParentOrSelf.apply(v2, v)).findFirst().get()).map(
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
    78
                        v -> v.getValue()).collect(Collectors.toList());
58301
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    79
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    80
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    81
    long sizeInBytes() throws IOException {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    82
        long reply = 0;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    83
        for (Path dir : roots().stream().filter(f -> Files.isDirectory(f)).collect(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    84
                Collectors.toList())) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    85
            reply += Files.walk(dir).filter(p -> Files.isRegularFile(p)).mapToLong(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    86
                    f -> f.toFile().length()).sum();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    87
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    88
        return reply;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    89
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    90
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    91
    PathGroup resolveAt(Path root) {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    92
        return new PathGroup(entries.entrySet().stream().collect(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    93
                Collectors.toMap(e -> e.getKey(),
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    94
                        e -> root.resolve(e.getValue()))));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    95
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    96
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    97
    void copy(PathGroup dst) throws IOException {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    98
        copy(this, dst, false);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
    99
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   100
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   101
    void move(PathGroup dst) throws IOException {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   102
        copy(this, dst, true);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   103
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   104
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   105
    static interface Facade<T> {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   106
        PathGroup pathGroup();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   107
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   108
        default Collection<Path> paths() {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   109
            return pathGroup().paths();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   110
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   111
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   112
        default List<Path> roots() {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   113
            return pathGroup().roots();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   114
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   115
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   116
        default long sizeInBytes() throws IOException {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   117
            return pathGroup().sizeInBytes();
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   118
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   119
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   120
        T resolveAt(Path root);
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   121
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   122
        default void copy(Facade<T> dst) throws IOException {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   123
            pathGroup().copy(dst.pathGroup());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   124
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   125
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   126
        default void move(Facade<T> dst) throws IOException {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   127
            pathGroup().move(dst.pathGroup());
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   128
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   129
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   130
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   131
    private static void copy(PathGroup src, PathGroup dst, boolean move) throws
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   132
            IOException {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   133
        copy(move, src.entries.keySet().stream().filter(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   134
                id -> dst.entries.containsKey(id)).map(id -> Map.entry(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   135
                src.entries.get(id), dst.entries.get(id))).collect(
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   136
                Collectors.toCollection(ArrayList::new)));
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   137
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   138
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   139
    private static void copy(boolean move, List<Map.Entry<Path, Path>> entries)
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   140
            throws IOException {
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   141
58302
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   142
        // destination -> source file mapping
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   143
        Map<Path, Path> actions = new HashMap<>();
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   144
        for (var action: entries) {
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   145
            Path src = action.getKey();
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   146
            Path dst = action.getValue();
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   147
            if (src.toFile().isDirectory()) {
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   148
                Files.walk(src).forEach(path -> actions.put(dst.resolve(
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   149
                        src.relativize(path)).toAbsolutePath().normalize(), path));
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   150
            } else {
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   151
                actions.put(dst.toAbsolutePath().normalize(), src);
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   152
            }
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   153
        }
58301
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   154
58302
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   155
        for (var action : actions.entrySet()) {
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   156
            Path dst = action.getKey();
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   157
            Path src = action.getValue();
58301
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   158
58302
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   159
            if (src.equals(dst) || !src.toFile().exists()) {
58301
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   160
                continue;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   161
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   162
58302
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   163
            if (src.toFile().isDirectory()) {
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   164
                Files.createDirectories(dst);
58301
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   165
            } else {
58302
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   166
                Files.createDirectories(dst.getParent());
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   167
                if (move) {
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   168
                    Files.move(src, dst);
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   169
                } else {
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   170
                    Files.copy(src, dst);
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   171
                }
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   172
            }
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   173
        }
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   174
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   175
        if (move) {
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   176
            // Delete source dirs.
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   177
            for (var entry: entries) {
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   178
                File srcFile = entry.getKey().toFile();
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   179
                if (srcFile.isDirectory()) {
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   180
                    IOUtils.deleteRecursive(srcFile);
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   181
                }
58301
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   182
            }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   183
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   184
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   185
58302
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   186
    private static Map.Entry<Path, Path> normalizedPath(Path v) {
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   187
        final Path normalized;
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   188
        if (!v.isAbsolute()) {
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   189
            normalized = Path.of("./").resolve(v.normalize());
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   190
        } else {
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   191
            normalized = v.normalize();
58301
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   192
        }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   193
58302
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   194
        return Map.entry(normalized, v);
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   195
    }
58301
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   196
58302
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   197
    private List<Map.Entry<Path, Path>> normalizedPaths() {
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   198
        return entries.values().stream().map(PathGroup::normalizedPath).collect(
718bd56695b3 8231277 : Adjust Linux application image layout
herrick
parents: 58301
diff changeset
   199
                Collectors.toList());
58301
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   200
    }
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   201
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   202
    private final Map<Object, Path> entries;
e0efb29609bd 8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
diff changeset
   203
}