Skip to content

Commit

Permalink
jnr#95: fix fstat(int, FileStat) in WindowsPOSIX
Browse files Browse the repository at this point in the history
Unfortunately, behaviour is undefined if given fd is not valid
  • Loading branch information
joerg.frantzius authored and joerg.frantzius committed Jan 6, 2017
1 parent 30c3bf1 commit daa082f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/main/java/jnr/posix/WindowsPOSIX.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,16 @@ public FileStat fstat(int fd) {
return stat;
}

@Override
public int fstat(int fd, FileStat stat) {
FileDescriptor fileDescriptor = JavaLibCHelper.toFileDescriptor(fd);
return fstat(fileDescriptor, stat);
}

@Override
public int fstat(FileDescriptor fileDescriptor, FileStat stat) {
WindowsByHandleFileInformation info = new WindowsByHandleFileInformation(getRuntime());
if (wlibc().GetFileInformationByHandle(JavaLibCHelper.gethandle(fileDescriptor), info) == 0) return -1;
//if (wlibc().GetFileInformationByHandle(JavaLibCHelper.gethandle(fileDescriptor), info) == 0) return -1;

((WindowsRawFileStat) stat).setup(info);

Expand Down
17 changes: 17 additions & 0 deletions src/test/java/jnr/posix/FileStatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ public void filestatDescriptor() throws Throwable {

}

@Test
public void filestatDescriptor2() throws Throwable {
File f = File.createTempFile("stat", null);

try {
int fd = posix.open(f.getAbsolutePath(), 0, 438);
FileStat stat = posix.allocateStat();
int result = posix.fstat(fd, stat);
assertEquals(0, result);
assertEquals(0, stat.st_size());
} finally {
f.delete();
}

}


@Test
public void filestatInt() throws Throwable {
// Windows does not store fd in FileDescriptor so this test wll not work
Expand Down

0 comments on commit daa082f

Please sign in to comment.