Skip to content

Commit cfb9b61

Browse files
authored
use Files.createTempFile (#141)
* use Files.createTempFile * Don't always apply POSIX permissions * back out permissions change * Revert "back out permissions change" This reverts commit 8ec486b. * pin back windows * add slack alert & cron * broken dockcross tag * docker rmi * build only specific artifact per test * fix
1 parent 0dfcd64 commit cfb9b61

File tree

6 files changed

+87
-18
lines changed

6 files changed

+87
-18
lines changed

.github/workflows/slack-alert.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: slack-alert
2+
3+
on:
4+
workflow_run:
5+
workflows: [tests]
6+
types: [completed]
7+
8+
jobs:
9+
on-failure:
10+
runs-on: ubuntu-latest
11+
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
12+
steps:
13+
- name: Send Slack Alert
14+
id: slack
15+
uses: slackapi/[email protected]
16+
with:
17+
payload: |
18+
Github Actions ${{ github.event.workflow_run.conclusion }}
19+
Repo: ${{github.event.workflow_run.repository.name }}
20+
Workflow URL: ${{ github.event.workflow_run.html_url }}
21+
env:
22+
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}

.github/workflows/tests.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ on:
55
branches: [master, stable-*]
66
pull_request:
77
branches: [master, stable-*]
8+
schedule:
9+
# Every Sunday, rerun
10+
- cron: "0 12 * * 0"
811

912
jobs:
1013
tests:
@@ -56,15 +59,16 @@ jobs:
5659
if-no-files-found: error
5760

5861
tests-new-dockcross:
59-
name: Dockcross ${{ matrix.dockcross-tag }} Java ${{ matrix.java-version }} ${{ matrix.os }}
62+
name: Dockcross ${{ matrix.dockcross-tag }} Java ${{ matrix.java-version }} ${{ matrix.os }} ${{ matrix.dockcross-only }}
6063
runs-on: ${{ matrix.os }}
6164

6265
strategy:
6366
matrix:
6467
os: [ubuntu-latest]
6568
java-distribution: [adopt]
6669
java-version: [17]
67-
dockcross-tag: ["20220906-e88a3ce", "20230116-670f7f7"]
70+
dockcross-tag: ["20230116-670f7f7", "20240418-88c04a4", "latest"]
71+
dockcross-only: ["android-arm", "android-arm64", "linux-arm64", "linux-armv5", "linux-armv7", "linux-s390x", "linux-ppc64le", "linux-x64", "linux-x86", "windows-static-x64", "windows-static-x86"]
6872

6973
steps:
7074
- uses: actions/[email protected]
@@ -85,7 +89,7 @@ jobs:
8589
${{ runner.os }}-maven-
8690
8791
- name: Tests
88-
run: mvn "-Dh3.system.prune=true" "-Dh3.dockcross.tag=${{ matrix.dockcross-tag }}" -B -V clean test
92+
run: mvn "-Dh3.system.prune=true" "-Dh3.dockcross.tag=${{ matrix.dockcross-tag }}" "-Dh3.dockcross.only=${{ matrix.dockcross-only }}" -B -V clean test
8993

9094
tests-no-docker:
9195
name: Java (No Docker) ${{ matrix.java-version }} ${{ matrix.os }}
@@ -94,7 +98,8 @@ jobs:
9498
strategy:
9599
matrix:
96100
# TODO: Docker on macos-latest running is not working
97-
os: [macos-latest, windows-latest]
101+
# TODO: Windows pinned back
102+
os: [macos-latest, windows-2019]
98103
java-distribution: [adopt]
99104
java-version: [8, 11, 15, 17]
100105

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<h3.use.docker>true</h3.use.docker>
7373
<h3.system.prune>false</h3.system.prune>
7474
<h3.dockcross.tag>20210624-de7b1b0</h3.dockcross.tag>
75+
<h3.dockcross.only />
7576
<h3.github.artifacts.use>false</h3.github.artifacts.use>
7677
<h3.github.artifacts.by_run />
7778
<!-- Used for passing additional arguments to surefire when using JaCoCo -->
@@ -270,6 +271,7 @@
270271
<argument>${h3.use.docker}</argument>
271272
<argument>${h3.system.prune}</argument>
272273
<argument>${h3.dockcross.tag}</argument>
274+
<argument>${h3.dockcross.only}</argument>
273275
<argument>${h3.github.artifacts.use}</argument>
274276
<argument>${h3.github.artifacts.by_run}</argument>
275277
</arguments>

src/main/c/h3-java/build-h3.sh

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
# system prune will be run after each step
2626
# (i.e. for disk space constrained environments like CI)
2727
# dockcross-tag - Tag name for dockcross
28+
# dockcross-only - When set, build only a specific architecture with dockcross.
2829
# github-artifacts - When set, all build artifacts are retrieved from Github
2930
# Actions artifacts rather than built locally (overrides
3031
# all other settings.)
@@ -44,8 +45,9 @@ GIT_REVISION=$2
4445
USE_DOCKER=$3
4546
SYSTEM_PRUNE=$4
4647
DOCKCROSS_TAG=$5
47-
GITHUB_ARTIFACTS=$6
48-
GITHUB_ARTIFACTS_RUN=$7
48+
DOCKCROSS_ONLY=$6
49+
GITHUB_ARTIFACTS=$7
50+
GITHUB_ARTIFACTS_RUN=$8
4951

5052
if $GITHUB_ARTIFACTS; then
5153
src/main/c/h3-java/pull-from-github.sh "$GITHUB_ARTIFACTS_RUN"
@@ -180,10 +182,15 @@ UPGRADE_CMAKE=true
180182
CMAKE_ROOT=target/cmake-3.23.2-linux-x86_64
181183
mkdir -p $CMAKE_ROOT
182184

185+
DOCKCROSS_IMAGES="android-arm android-arm64 linux-arm64 linux-armv5 linux-armv7 linux-s390x linux-ppc64le linux-x64 linux-x86 windows-static-x64 windows-static-x86"
186+
if ! $DOCKCROSS_ONLY; then
187+
DOCKCROSS_IMAGES=$DOCKCROSS_ONLY
188+
echo Building only: $DOCKCROSS_IMAGES
189+
fi
190+
183191
# linux-armv6 excluded because of build failure
184192
# linux-mips excluded due to manifest error
185-
for image in android-arm android-arm64 linux-arm64 linux-armv5 linux-armv7 linux-s390x \
186-
linux-ppc64le linux-x64 linux-x86 windows-static-x64 windows-static-x86; do
193+
for image in $DOCKCROSS_IMAGES; do
187194

188195
# Setup for using dockcross
189196
BUILD_ROOT=target/h3-java-build-$image
@@ -210,6 +217,9 @@ for image in android-arm android-arm64 linux-arm64 linux-armv5 linux-armv7 linux
210217
if [ -e $BUILD_ROOT/lib/libh3-java.dll ]; then cp $BUILD_ROOT/lib/libh3-java.dll $OUTPUT_ROOT ; fi
211218

212219
if $SYSTEM_PRUNE; then
220+
# Remove the image we just ran
221+
docker rmi dockcross/$image:$DOCKCROSS_TAG
222+
# Aggressively try to free more disk space
213223
docker system prune --force --all
214224
docker system df
215225
rm $BUILD_ROOT/dockcross

src/main/java/com/uber/h3core/H3CoreLoader.java

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2018 Uber Technologies, Inc.
2+
* Copyright 2017-2018, 2024 Uber Technologies, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,12 @@
2020
import java.io.IOException;
2121
import java.io.InputStream;
2222
import java.io.OutputStream;
23+
import java.nio.file.Files;
24+
import java.nio.file.attribute.FileAttribute;
25+
import java.nio.file.attribute.PosixFilePermission;
26+
import java.nio.file.attribute.PosixFilePermissions;
2327
import java.util.Locale;
28+
import java.util.Set;
2429

2530
/** Extracts the native H3 core library to the local filesystem and loads it. */
2631
public final class H3CoreLoader {
@@ -60,11 +65,6 @@ private static void copyStream(InputStream in, OutputStream out) throws IOExcept
6065
* @throws UnsatisfiedLinkError The resource path does not exist
6166
*/
6267
static void copyResource(String resourcePath, File newH3LibFile) throws IOException {
63-
// Set the permissions
64-
newH3LibFile.setReadable(true);
65-
newH3LibFile.setWritable(true, true);
66-
newH3LibFile.setExecutable(true, true);
67-
6868
// Shove the resource into the file and close it
6969
try (InputStream resource = H3CoreLoader.class.getResourceAsStream(resourcePath)) {
7070
if (resource == null) {
@@ -93,6 +93,24 @@ public static NativeMethods loadNatives() throws IOException {
9393
return loadNatives(os, arch);
9494
}
9595

96+
private static File createTempLibraryFile(OperatingSystem os) throws IOException {
97+
if (os.isPosix()) {
98+
// Note this is already done by the implementation of Files.createTempFile that I looked at,
99+
// but the javadoc does not seem to gaurantee the permissions will be restricted to owner
100+
// write.
101+
final FileAttribute<Set<PosixFilePermission>> attr =
102+
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------"));
103+
return Files.createTempFile("libh3-java", os.getSuffix(), attr).toFile();
104+
} else {
105+
// When not a POSIX OS, try to ensure the permissions are secure
106+
final File f = Files.createTempFile("libh3-java", os.getSuffix()).toFile();
107+
f.setReadable(true, true);
108+
f.setWritable(true, true);
109+
f.setExecutable(true, true);
110+
return f;
111+
}
112+
}
113+
96114
/**
97115
* For use when the H3 library should be unpacked from the JAR and loaded. The native library for
98116
* the specified operating system and architecture will be extract.
@@ -115,8 +133,7 @@ public static synchronized NativeMethods loadNatives(OperatingSystem os, String
115133
final String dirName = String.format("%s-%s", os.getDirName(), arch);
116134
final String libName = String.format("libh3-java%s", os.getSuffix());
117135

118-
final File newLibraryFile = File.createTempFile("libh3-java", os.getSuffix());
119-
136+
final File newLibraryFile = createTempLibraryFile(os);
120137
newLibraryFile.deleteOnExit();
121138

122139
copyResource(String.format("/%s/%s", dirName, libName), newLibraryFile);
@@ -146,13 +163,20 @@ public enum OperatingSystem {
146163
ANDROID(".so"),
147164
DARWIN(".dylib"),
148165
FREEBSD(".so"),
149-
WINDOWS(".dll"),
166+
WINDOWS(".dll", false),
150167
LINUX(".so");
151168

152169
private final String suffix;
170+
private final boolean posix;
153171

154172
OperatingSystem(String suffix) {
155173
this.suffix = suffix;
174+
this.posix = true;
175+
}
176+
177+
OperatingSystem(String suffix, boolean posix) {
178+
this.suffix = suffix;
179+
this.posix = posix;
156180
}
157181

158182
/** Suffix for native libraries. */
@@ -164,6 +188,11 @@ public String getSuffix() {
164188
public String getDirName() {
165189
return name().toLowerCase(H3_CORE_LOCALE);
166190
}
191+
192+
/** Whether to try to use POSIX file permissions when creating the native library temp file. */
193+
public boolean isPosix() {
194+
return this.posix;
195+
}
167196
}
168197

169198
/**

src/test/java/com/uber/h3core/TestH3CoreLoader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.io.File;
2121
import java.io.IOException;
22+
import java.nio.file.Files;
2223
import org.junit.Test;
2324

2425
/** H3CoreLoader is mostly tested by {@link TestH3CoreFactory}. This also tests OS detection. */
@@ -60,7 +61,7 @@ public void testDetectArch() {
6061

6162
@Test(expected = UnsatisfiedLinkError.class)
6263
public void testExtractNonexistant() throws IOException {
63-
File tempFile = File.createTempFile("test-extract-resource", null);
64+
File tempFile = Files.createTempFile("test-extract-resource", null).toFile();
6465

6566
tempFile.deleteOnExit();
6667

0 commit comments

Comments
 (0)