Skip to content

Commit

Permalink
prevent sigsev when mapping FileInfo on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
infeo committed Oct 19, 2023
1 parent d725a32 commit 63281af
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.cryptomator.jfuse.api.FileInfo;
import org.cryptomator.jfuse.win.extr.fcntl.fcntl_h;
import org.cryptomator.jfuse.win.extr.fuse3.fuse3_file_info;
import org.jetbrains.annotations.Nullable;

import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
Expand All @@ -20,6 +21,18 @@ record FileInfoImpl(MemorySegment segment) implements FileInfo {
private static final int O_TRUNC = fcntl_h.O_TRUNC();
private static final int O_EXCL = fcntl_h.O_EXCL();

/**
* Factory method to map native memory to an {@link FileInfo} object
*
* @param address the {@link MemorySegment} representing the starting address
* @param scope the {@link Arena} in which this object will be alive
* @return an {@link FileInfo} object or {@code null} if {@code address} is a NULL pointer
*/
@Nullable
public static FileInfoImpl of(MemorySegment address, Arena scope) {
return MemorySegment.NULL.equals(address) ? null : new FileInfoImpl(address, scope);
}

public FileInfoImpl(MemorySegment address, Arena scope) {
this(fuse3_file_info.ofAddress(address, scope));
}
Expand Down
10 changes: 5 additions & 5 deletions jfuse-win/src/main/java/org/cryptomator/jfuse/win/FuseImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ MemorySegment init(MemorySegment conn, MemorySegment cfg) {

private int chmod(MemorySegment path, int mode, MemorySegment fi) {
try (var arena = Arena.ofConfined()) {
return fuseOperations.chmod(path.getUtf8String(0), mode, new FileInfoImpl(fi, arena));
return fuseOperations.chmod(path.getUtf8String(0), mode, FileInfoImpl.of(fi, arena));
}
}

@VisibleForTesting
int chown(MemorySegment path, int uid, int gid, MemorySegment fi) {
try (var arena = Arena.ofConfined()) {
return fuseOperations.chown(path.getUtf8String(0), uid, gid, new FileInfoImpl(fi, arena));
return fuseOperations.chown(path.getUtf8String(0), uid, gid, FileInfoImpl.of(fi, arena));
}
}

Expand Down Expand Up @@ -171,7 +171,7 @@ int fsyncdir(MemorySegment path, int datasync, MemorySegment fi) {
@VisibleForTesting
int getattr(MemorySegment path, MemorySegment stat, MemorySegment fi) {
try (var arena = Arena.ofConfined()) {
return fuseOperations.getattr(path.getUtf8String(0), new StatImpl(stat, arena), new FileInfoImpl(fi, arena));
return fuseOperations.getattr(path.getUtf8String(0), new StatImpl(stat, arena), FileInfoImpl.of(fi, arena));
}
}

Expand Down Expand Up @@ -265,7 +265,7 @@ private int symlink(MemorySegment linkname, MemorySegment target) {
@VisibleForTesting
int truncate(MemorySegment path, long size, MemorySegment fi) {
try (var arena = Arena.ofConfined()) {
return fuseOperations.truncate(path.getUtf8String(0), size, new FileInfoImpl(fi, arena));
return fuseOperations.truncate(path.getUtf8String(0), size, FileInfoImpl.of(fi, arena));
}
}

Expand All @@ -282,7 +282,7 @@ int utimens(MemorySegment path, MemorySegment times, MemorySegment fi) {
var segment = times.reinterpret(seq.byteSize());
var time0 = segment.asSlice(0, fuse_timespec.$LAYOUT().byteSize());
var time1 = segment.asSlice(fuse_timespec.$LAYOUT().byteSize(), fuse_timespec.$LAYOUT().byteSize());
return fuseOperations.utimens(path.getUtf8String(0), new TimeSpecImpl(time0), new TimeSpecImpl(time1), new FileInfoImpl(fi, arena));
return fuseOperations.utimens(path.getUtf8String(0), new TimeSpecImpl(time0), new TimeSpecImpl(time1), FileInfoImpl.of(fi, arena));
}
}

Expand Down

0 comments on commit 63281af

Please sign in to comment.