- Improvements to fetching file system information on Windows.

- Fixes for reset and clearing to end of line for Windows console.
This commit is contained in:
Adam Murdoch
2012-09-15 17:52:51 +10:00
parent 17ab1f04ef
commit 30cc864813
2 changed files with 122 additions and 99 deletions

View File

@@ -3,6 +3,7 @@
#include "native.h" #include "native.h"
#include "generic.h" #include "generic.h"
#include <windows.h> #include <windows.h>
#include <wchar.h>
/* /*
* Marks the given result as failed, using the current value of GetLastError() * Marks the given result as failed, using the current value of GetLastError()
@@ -55,36 +56,53 @@ Java_net_rubygrapefruit_platform_internal_jni_PosixProcessFunctions_getPid(JNIEn
*/ */
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_PosixFileSystemFunctions_listFileSystems(JNIEnv *env, jclass target, jobject info, jobject result) { Java_net_rubygrapefruit_platform_internal_jni_PosixFileSystemFunctions_listFileSystems(JNIEnv *env, jclass target, jobject info, jobject result) {
// TODO - don't use the stack wchar_t* volumeName = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH+1));
wchar_t volumeName[MAX_PATH+1];
wchar_t deviceName[MAX_PATH+1];
wchar_t pathNames[MAX_PATH+1];
wchar_t fsName[MAX_PATH+1];
jclass info_class = env->GetObjectClass(info); jclass info_class = env->GetObjectClass(info);
jmethodID method = env->GetMethodID(info_class, "add", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)V"); jmethodID method = env->GetMethodID(info_class, "add", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)V");
HANDLE handle = FindFirstVolumeW(volumeName, MAX_PATH+1); HANDLE handle = FindFirstVolumeW(volumeName, MAX_PATH+1);
if (handle == INVALID_HANDLE_VALUE) { if (handle == INVALID_HANDLE_VALUE) {
printf("no volumes\r\n"); free(volumeName);
mark_failed_with_errno(env, "could not find first volume", result);
return; return;
} }
wchar_t* deviceName = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH+1));
wchar_t* pathNames = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH+1));
wchar_t* fsName = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH+1));
while(true) { while(true) {
// Chop off the trailing '\' // Chop off the trailing '\'
size_t len = wcslen(volumeName); size_t len = wcslen(volumeName);
if (len < 5) {
mark_failed_with_message(env, "volume name is too short", result);
break;
}
volumeName[len-1] = L'\0'; volumeName[len-1] = L'\0';
QueryDosDeviceW(&volumeName[4], deviceName, MAX_PATH+1); if (QueryDosDeviceW(&volumeName[4], deviceName, MAX_PATH+1) == 0) {
// TODO - error mark_failed_with_errno(env, "could not query dos device", result);
break;
}
volumeName[len-1] = L'\\'; volumeName[len-1] = L'\\';
DWORD used; DWORD used;
GetVolumePathNamesForVolumeNameW(volumeName, pathNames, MAX_PATH+1, &used); if (GetVolumePathNamesForVolumeNameW(volumeName, pathNames, MAX_PATH+1, &used) == 0) {
// TODO - error // TODO - try again if the buffer is too small
mark_failed_with_errno(env, "could not query volume paths", result);
break;
}
wchar_t* cur = pathNames; wchar_t* cur = pathNames;
if (cur[0] != L'\0') { if (cur[0] != L'\0') {
GetVolumeInformationW(cur, NULL, 0, NULL, NULL, NULL, fsName, MAX_PATH+1); if(GetVolumeInformationW(cur, NULL, 0, NULL, NULL, NULL, fsName, MAX_PATH+1) == 0) {
// TODO - error if (GetLastError() != ERROR_NOT_READY) {
mark_failed_with_errno(env, "could not query volume information", result);
break;
}
wcscpy(fsName, L"unknown");
}
for (;cur[0] != L'\0'; cur += wcslen(cur) + 1) { for (;cur[0] != L'\0'; cur += wcslen(cur) + 1) {
env->CallVoidMethod(info, method, env->NewString((jchar*)deviceName, wcslen(deviceName)), env->CallVoidMethod(info, method, env->NewString((jchar*)deviceName, wcslen(deviceName)),
env->NewString((jchar*)fsName, wcslen(fsName)), env->NewString((jchar*)cur, wcslen(cur)), JNI_FALSE); env->NewString((jchar*)fsName, wcslen(fsName)), env->NewString((jchar*)cur, wcslen(cur)), JNI_FALSE);
@@ -93,11 +111,15 @@ Java_net_rubygrapefruit_platform_internal_jni_PosixFileSystemFunctions_listFileS
if (FindNextVolumeW(handle, volumeName, MAX_PATH) == 0) { if (FindNextVolumeW(handle, volumeName, MAX_PATH) == 0) {
if (GetLastError() != ERROR_NO_MORE_FILES) { if (GetLastError() != ERROR_NO_MORE_FILES) {
// TODO - fail mark_failed_with_errno(env, "could find next volume", result);
} }
break; break;
} }
} }
free(volumeName);
free(deviceName);
free(pathNames);
free(fsName);
FindVolumeClose(handle); FindVolumeClose(handle);
} }
@@ -196,8 +218,7 @@ Java_net_rubygrapefruit_platform_internal_jni_WindowsConsoleFunctions_normal(JNI
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_WindowsConsoleFunctions_reset(JNIEnv *env, jclass target, jobject result) { Java_net_rubygrapefruit_platform_internal_jni_WindowsConsoleFunctions_reset(JNIEnv *env, jclass target, jobject result) {
SetConsoleTextAttribute(current_console, original_attributes); if (!SetConsoleTextAttribute(current_console, original_attributes)) {
if (!SetConsoleTextAttribute(current_console, current_attributes)) {
mark_failed_with_errno(env, "could not set text attributes", result); mark_failed_with_errno(env, "could not set text attributes", result);
} }
} }
@@ -309,8 +330,9 @@ Java_net_rubygrapefruit_platform_internal_jni_WindowsConsoleFunctions_clearToEnd
mark_failed_with_errno(env, "could not get console buffer", result); mark_failed_with_errno(env, "could not get console buffer", result);
return; return;
} }
for (int i = console_info.dwCursorPosition.X; i < console_info.dwSize.X; i++) { DWORD count;
WriteConsole(current_console, " ", 1, NULL, NULL); if (!FillConsoleOutputCharacterW(current_console, L' ', console_info.dwSize.X - console_info.dwCursorPosition.X, console_info.dwCursorPosition, &count)) {
mark_failed_with_errno(env, "could not clear console", result);
} }
} }

View File

@@ -1,82 +1,83 @@
package net.rubygrapefruit.platform; package net.rubygrapefruit.platform;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(); System.out.println();
System.out.println("* OS: " + System.getProperty("os.name") + ' ' + System.getProperty("os.version") + ' ' + System.getProperty("os.arch")); System.out.println("* OS: " + System.getProperty("os.name") + ' ' + System.getProperty("os.version") + ' ' + System.getProperty("os.arch"));
System.out.println("* JVM: " + System.getProperty("java.vm.vendor") + ' ' + System.getProperty("java.version")); System.out.println("* JVM: " + System.getProperty("java.vm.vendor") + ' ' + System.getProperty("java.version"));
SystemInfo systemInfo = Native.get(SystemInfo.class); SystemInfo systemInfo = Native.get(SystemInfo.class);
System.out.println("* Kernel: " + systemInfo.getKernelName() + ' ' + systemInfo.getKernelVersion() + ' ' + systemInfo.getMachineArchitecture()); System.out.println("* Kernel: " + systemInfo.getKernelName() + ' ' + systemInfo.getKernelVersion() + ' ' + systemInfo.getMachineArchitecture());
Process process = Native.get(Process.class); Process process = Native.get(Process.class);
System.out.println("* PID: " + process.getProcessId()); System.out.println("* PID: " + process.getProcessId());
FileSystems fileSystems = Native.get(FileSystems.class); FileSystems fileSystems = Native.get(FileSystems.class);
System.out.println("* File systems: "); System.out.println("* File systems: ");
for (FileSystem fileSystem : fileSystems.getFileSystems()) { for (FileSystem fileSystem : fileSystems.getFileSystems()) {
System.out.println(" * " + fileSystem.getMountPoint() + ' ' + fileSystem.getFileSystemType() + ' ' + fileSystem.getDeviceName() + (fileSystem.isRemote() ? " remote" : " local")); System.out.println(" * " + fileSystem.getMountPoint() + ' ' + fileSystem.getFileSystemType() + ' ' + fileSystem.getDeviceName() + (fileSystem.isRemote() ? " remote" : " local"));
} }
Terminals terminals = Native.get(Terminals.class); Terminals terminals = Native.get(Terminals.class);
boolean stdoutIsTerminal = terminals.isTerminal(Terminals.Output.Stdout); boolean stdoutIsTerminal = terminals.isTerminal(Terminals.Output.Stdout);
boolean stderrIsTerminal = terminals.isTerminal(Terminals.Output.Stderr); boolean stderrIsTerminal = terminals.isTerminal(Terminals.Output.Stderr);
System.out.println("* stdout: " + (stdoutIsTerminal ? "terminal" : "not a terminal")); System.out.println("* stdout: " + (stdoutIsTerminal ? "terminal" : "not a terminal"));
System.out.println("* stderr: " + (stderrIsTerminal ? "terminal" : "not a terminal")); System.out.println("* stderr: " + (stderrIsTerminal ? "terminal" : "not a terminal"));
if (stdoutIsTerminal) { if (stdoutIsTerminal) {
Terminal terminal = terminals.getTerminal(Terminals.Output.Stdout); Terminal terminal = terminals.getTerminal(Terminals.Output.Stdout);
TerminalSize terminalSize = terminal.getTerminalSize(); TerminalSize terminalSize = terminal.getTerminalSize();
System.out.println("* terminal size: " + terminalSize.getCols() + " cols x " + terminalSize.getRows() + " rows"); System.out.println("* terminal size: " + terminalSize.getCols() + " cols x " + terminalSize.getRows() + " rows");
System.out.println("* text attributes: " + (terminal.supportsTextAttributes() ? "yes" : "no")); System.out.println("* text attributes: " + (terminal.supportsTextAttributes() ? "yes" : "no"));
System.out.println("* color: " + (terminal.supportsColor() ? "yes" : "no")); System.out.println("* color: " + (terminal.supportsColor() ? "yes" : "no"));
System.out.println("* cursor motion: " + (terminal.supportsCursorMotion() ? "yes" : "no")); System.out.println("* cursor motion: " + (terminal.supportsCursorMotion() ? "yes" : "no"));
System.out.println(); System.out.println();
System.out.println("TEXT ATTRIBUTES"); System.out.println("TEXT ATTRIBUTES");
System.out.print("[normal] "); System.out.print("[normal] ");
terminal.bold(); terminal.bold();
System.out.print("[bold]"); System.out.print("[bold]");
terminal.normal(); terminal.normal();
System.out.println(" [normal]"); System.out.println(" [normal]");
System.out.println(); System.out.println();
System.out.println("COLORS"); System.out.println("COLORS");
for (Terminal.Color color : Terminal.Color.values()) { for (Terminal.Color color : Terminal.Color.values()) {
terminal.foreground(color); terminal.foreground(color);
System.out.print(String.format("[%s] ", color.toString().toLowerCase())); System.out.print(String.format("[%s] ", color.toString().toLowerCase()));
terminal.bold(); terminal.bold();
System.out.print(String.format("[%s]", color.toString().toLowerCase())); System.out.print(String.format("[%s]", color.toString().toLowerCase()));
terminal.normal(); terminal.normal();
System.out.println(); System.out.println();
} }
System.out.println(); System.out.println();
terminal.reset(); terminal.reset();
if (terminal.supportsCursorMotion()) { if (terminal.supportsCursorMotion()) {
System.out.println("CURSOR MOVEMENT"); System.out.println("CURSOR MOVEMENT");
System.out.println(" "); System.out.println(" ");
System.out.println(" "); System.out.println(" ");
System.out.print("[delete me]"); System.out.print("[delete me]");
terminal.cursorLeft(11); terminal.cursorLeft(11);
terminal.cursorUp(1); terminal.cursorUp(1);
terminal.cursorRight(10); terminal.cursorRight(10);
System.out.print("[4]"); System.out.print("[4]");
terminal.cursorUp(1); terminal.cursorUp(1);
terminal.cursorLeft(3); terminal.cursorLeft(3);
System.out.print("[2]"); System.out.print("[2]");
terminal.cursorLeft(13); terminal.cursorLeft(13);
System.out.print("[1]"); System.out.print("[1]");
terminal.cursorLeft(3); terminal.cursorLeft(3);
terminal.cursorDown(1); terminal.cursorDown(1);
System.out.print("[3]"); System.out.print("[3]");
terminal.cursorDown(1); terminal.cursorDown(1);
terminal.cursorStartOfLine(); terminal.cursorStartOfLine();
terminal.clearToEndOfLine(); terminal.foreground(Terminal.Color.Blue).bold();
terminal.foreground(Terminal.Color.Blue).bold(); System.out.print("done");
System.out.println("done!"); terminal.clearToEndOfLine();
System.out.println(); System.out.println("!");
} System.out.println();
} }
} }
} }
}