Skip to content

Commit

Permalink
Merge pull request #2 from Chillax-0v0/fix-direct-io-flag-on-arm64
Browse files Browse the repository at this point in the history
fix(OpenFlags): correct the `O_DIRECT` flag on aarch64
  • Loading branch information
lexburner authored Mar 17, 2024
2 parents 29ddf36 + f7e1c4a commit a8a75f7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
7 changes: 3 additions & 4 deletions src/main/java/moe/cnkirito/kdio/DirectIOLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ public int pwrite(int fd, ByteBuffer buf, long offset) throws IOException {
*
*/
public int oDirectOpen(String pathname, boolean readOnly) throws IOException {
int flags = OpenFlags.O_DIRECT;
int flags = OpenFlags.INSTANCE.oDIRECT();
if (readOnly) {
flags |= OpenFlags.O_RDONLY;
flags |= OpenFlags.INSTANCE.oRDONLY();
} else {
flags |= OpenFlags.O_RDWR | OpenFlags.O_CREAT;
flags |= OpenFlags.INSTANCE.oWRONLY() | OpenFlags.INSTANCE.oCREAT();
}
int fd = open(pathname, flags, 00644);
if (fd < 0) {
Expand Down Expand Up @@ -413,4 +413,3 @@ public static int lcm(long x, long y) {
private static native String strerror(int errnum);

}

52 changes: 43 additions & 9 deletions src/main/java/moe/cnkirito/kdio/OpenFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,48 @@
/**
* Constants for {@link DirectIOLib#oDirectOpen(String, boolean)}.
*/
public final class OpenFlags {
public static final int O_RDONLY = 00;
public static final int O_WRONLY = 01;
public static final int O_RDWR = 02;
public static final int O_CREAT = 0100;
public static final int O_TRUNC = 01000;
public static final int O_DIRECT = 040000;
public static final int O_SYNC = 04000000;
public interface OpenFlags {
OpenFlags INSTANCE = instance();

private OpenFlags() {}
static OpenFlags instance() {
String arch = System.getProperty("os.arch");
switch (arch) {
case "aarch64":
return new Aarch64OpenFlags();
default:
return new DefaultOpenFlags();
}
}

default int oRDONLY() {
return 00;
}
default int oWRONLY() {
return 01;
}
default int oRDWR() {
return 02;
}
default int oCREAT() {
return 0100;
}
default int oTRUNC() {
return 01000;
}
default int oDIRECT() {
return 040000;
}
default int oSYNC() {
return 04010000;
}

class DefaultOpenFlags implements OpenFlags {
}

class Aarch64OpenFlags implements OpenFlags {
@Override
public int oDIRECT() {
return 0200000;
}
}
}

0 comments on commit a8a75f7

Please sign in to comment.