JNIEXPORT jlong JNICALL Java_java_lang_ProcessImpl_openForAtomicAppend(JNIEnv *env, jclass ignored, jstring path) { const DWORD access = (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA); const DWORD sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; const DWORD disposition = OPEN_ALWAYS; const DWORD flagsAndAttributes = FILE_ATTRIBUTE_NORMAL; HANDLE h; WCHAR *pathbuf = pathToNTPath(env, path, JNI_FALSE); if (pathbuf == NULL) { /* Exception already pending */ return -1; } h = CreateFileW( pathbuf, /* Wide char path name */ access, /* Read and/or write permission */ sharing, /* File sharing flags */ NULL, /* Security attributes */ disposition, /* creation disposition */ flagsAndAttributes, /* flags and attributes */ NULL); free(pathbuf); if (h == INVALID_HANDLE_VALUE) { JNU_ThrowIOExceptionWithLastError(env, "CreateFileW"); } return ptr_to_jlong(h); }
/* If this returns NULL then an exception is pending */ WCHAR* fileToNTPath(JNIEnv *env, jobject file, jfieldID id) { jstring path = NULL; if (file != NULL) { path = (*env)->GetObjectField(env, file, id); } return pathToNTPath(env, path, JNI_FALSE); }