| #include "os.h" | |
| #include <stdio.h> | |
| #include <sys/types.h> | |
| #include <sys/stat.h> | |
| #ifdef _WIN32 | |
| #include <windows.h> | |
| #include <shlobj.h> | |
| #include <tchar.h> | |
| #include <stdlib.h> | |
| #endif /* _WIN32 */ | |
| #ifndef NO_DeleteFileOnReboot | |
| JNIEXPORT jboolean JNICALL OS_NATIVE(DeleteFileOnReboot) | |
| (JNIEnv *env, jclass that, jstring arg0 ) | |
| { | |
| const jbyte *path; | |
| jboolean result; | |
| path = (*env)->GetStringUTFChars(env, arg0, JNI_FALSE); | |
| if ( path == NULL ) return 0; /* OutOfMemoryError already thrown */ | |
| #ifdef _WIN32 | |
| result = MoveFileEx(path,NULL,MOVEFILE_DELAY_UNTIL_REBOOT); | |
| #else /* _WIN32 */ | |
| result = unlink(path) == 0; | |
| #endif /* _WIN32 */ | |
| (*env)->ReleaseStringUTFChars(env, arg0, path); | |
| return result; | |
| } | |
| #endif | |
| #ifndef NO_GetSpecialFolderPath | |
| JNIEXPORT jstring JNICALL OS_NATIVE(GetSpecialFolderPath) | |
| (JNIEnv *env, jclass that, jint id ) | |
| { | |
| jstring str = NULL; | |
| #ifdef _WIN32 | |
| TCHAR szPath[MAX_PATH]; | |
| if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL, SHGFP_TYPE_CURRENT, szPath) ) ) | |
| { | |
| str = (*env)->NewStringUTF(env,szPath); | |
| } | |
| #else /* _WIN32 */ | |
| str = (*env)->NewStringUTF(env,""); | |
| #endif /* _WIN32 */ | |
| return str; | |
| } | |
| #endif | |
| #ifndef NO_GetDriveType | |
| JNIEXPORT jint JNICALL OS_NATIVE(GetDriveType) | |
| (JNIEnv *env, jclass that, jstring arg0 ) | |
| { | |
| jint result; | |
| #ifdef _WIN32 | |
| const jbyte *path; | |
| TCHAR drive[_MAX_DRIVE]; | |
| path = (*env)->GetStringUTFChars(env, arg0, JNI_FALSE); | |
| if ( path == NULL ) return 0; /* OutOfMemoryError already thrown */ | |
| _splitpath(path, drive, NULL, NULL, NULL ); | |
| result = GetDriveType(drive); | |
| (*env)->ReleaseStringUTFChars(env, arg0, path); | |
| #else /* _WIN32 */ | |
| result = 0; | |
| #endif /* _WIN32 */ | |
| return result; | |
| } | |
| #endif | |
| #ifndef NO_CanWrite | |
| JNIEXPORT jboolean JNICALL OS_NATIVE(CanWrite) | |
| (JNIEnv *jenv, jclass that, jstring arg0 ) | |
| { | |
| struct _stat buf; | |
| int statRes; | |
| jboolean ret=0; | |
| const char *str_path = (*jenv)->GetStringUTFChars(jenv, arg0, 0); | |
| statRes = _stat( str_path, &buf ); | |
| if( statRes != 0 ) | |
| ret=0; | |
| else if((buf.st_mode&_S_IWRITE)==_S_IWRITE) | |
| ret=1; | |
| (*jenv)->ReleaseStringUTFChars(jenv, arg0, str_path); | |
| return ret; | |
| } | |
| #endif | |
| #ifndef NO_CanRead | |
| JNIEXPORT jboolean JNICALL OS_NATIVE(CanRead) | |
| (JNIEnv *jenv, jclass that, jstring arg0 ) | |
| { | |
| struct _stat buf; | |
| int statRes; | |
| jboolean ret=0; | |
| const char *str_path = (*jenv)->GetStringUTFChars(jenv, arg0, 0); | |
| statRes = _stat( str_path, &buf ); | |
| if( statRes != 0 ) | |
| ret=0; | |
| else if((buf.st_mode&_S_IREAD)==_S_IREAD) | |
| ret=1; | |
| (*jenv)->ReleaseStringUTFChars(jenv, arg0, str_path); | |
| return ret; | |
| } | |
| #endif | |
| #ifndef NO_SetCreationTime | |
| JNIEXPORT jboolean JNICALL OS_NATIVE(SetCreationTime) | |
| (JNIEnv *jenv, jclass that, jstring arg0,jlong jtimestamp ) | |
| { | |
| jboolean result=0; | |
| LARGE_INTEGER timestamp; | |
| const char *str_path = (*jenv)->GetStringUTFChars(jenv, arg0, 0); | |
| FILETIME creation_time; | |
| HANDLE fhandle; | |
| __int64 TIMESTAMP_OFFSET = 11644473600000L; | |
| long TIMESTAMP_DIVIDER = 10000L; | |
| jtimestamp=(jtimestamp + TIMESTAMP_OFFSET) * TIMESTAMP_DIVIDER; | |
| fhandle = CreateFile(str_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL); | |
| if(fhandle == INVALID_HANDLE_VALUE) | |
| ; | |
| else{ | |
| timestamp.QuadPart = jtimestamp; | |
| creation_time.dwHighDateTime = timestamp.HighPart; | |
| creation_time.dwLowDateTime = timestamp.LowPart; | |
| if(SetFileTime((HANDLE)fhandle, &creation_time, NULL, NULL) == 0); | |
| else result = 1; | |
| } | |
| (*jenv)->ReleaseStringUTFChars(jenv, arg0, str_path); | |
| return result; | |
| } | |
| #endif | |