/** * filetimes.c : jni methods in nativefiletimes.dll * * For instructions on using javah and the C/C++ compiles see: * http://mindprod.com/jgloss/jni.html#COMPILING * * Copyright: (c) 2004-2017 Roedy Green, Canadian Mind Products */ #include // JNI prototypes prepared by javah #include "filetimes.h" /* * prototypes */ HANDLE getDirReadHandle( jstring, JNIEnv *); HANDLE getDirWriteHandle( jstring, JNIEnv *); HANDLE getFileReadHandle( jstring, JNIEnv *); HANDLE getFileWriteHandle( jstring, JNIEnv *); /* * Class: com_mindprod_filetimes_FileTimes * Method: nativeSetFileTimes * Signature: (Ljava/lang/String;JJJ)Z */ JNIEXPORT jboolean JNICALL Java_com_mindprod_filetimes_FileTimes_nativeSetFileTimes ( JNIEnv * env, jclass callerclass, jstring filename, jlong timecreated, jlong timeaccessed, jlong timeupdated ) { // local vars // FILETIMES are just little-endian longs split in two. FILETIME fttimecreated; FILETIME fttimeaccessed; FILETIME fttimeupdated; HANDLE handle; jboolean result; fttimecreated.dwLowDateTime = (DWORD) (timecreated & 0xffffffff); fttimecreated.dwHighDateTime = (DWORD) ((timecreated >> 32) & 0xffffffff); fttimeaccessed.dwLowDateTime = (DWORD) (timeaccessed & 0xffffffff); fttimeaccessed.dwHighDateTime = (DWORD) ((timeaccessed >> 32) & 0xffffffff); fttimeupdated.dwLowDateTime = (DWORD) (timeupdated & 0xffffffff); fttimeupdated.dwHighDateTime = (DWORD) ((timeupdated >> 32) & 0xffffffff); handle = getFileWriteHandle ( filename, env ); // INVALID_HANDLE_VALUE is -1 cast to HANDLE i.e. pointer type ) if ( handle == INVALID_HANDLE_VALUE ) return 0; // return true on success. result = ( jboolean ) SetFileTime( handle, // identifies the file timecreated ? &fttimecreated : NULL, timeaccessed ? &fttimeaccessed : NULL, timeupdated ? &fttimeupdated : NULL ); // status of close is immaterial. File is now changed. CloseHandle ( handle ); return result; } // end function /* * Class: com_mindprod_filetimes_FileTimes * Method: nativeGetFileCreated * Signature: (Ljava/lang/String;)J */ JNIEXPORT jlong JNICALL Java_com_mindprod_filetimes_FileTimes_nativeGetFileCreated ( JNIEnv * env, jclass callerclass, jstring filename) { FILETIME fttimecreated; HANDLE handle; handle = getFileReadHandle( filename, env ); // INVALID_HANDLE_VALUE is -1 cast to HANDLE i.e. pointer type ) if ( handle == INVALID_HANDLE_VALUE ) return 0; // return true on success. GetFileTime( handle, // identifies the file &fttimecreated, NULL, NULL ); // status of close is immaterial. CloseHandle ( handle ); return(jlong)(fttimecreated.dwHighDateTime) << 32 | fttimecreated.dwLowDateTime; } /* * Class: com_mindprod_filetimes_FileTimes * Method: nativeGetFileLastAccessed * Signature: (Ljava/lang/String;)J */ JNIEXPORT jlong JNICALL Java_com_mindprod_filetimes_FileTimes_nativeGetFileLastAccessed ( JNIEnv * env, jclass callerclass, jstring filename) { FILETIME fttimelastAccessed; HANDLE handle; handle = getFileReadHandle( filename, env ); // INVALID_HANDLE_VALUE is -1 cast to HANDLE i.e. pointer type ) if ( handle == INVALID_HANDLE_VALUE ) return 0; // return true on success. GetFileTime( handle, // identifies the file NULL, &fttimelastAccessed, NULL ); // status of close is immaterial. Dir is now changed. CloseHandle ( handle ); return(jlong)(fttimelastAccessed.dwHighDateTime) << 32 | fttimelastAccessed.dwLowDateTime; } /* * Class: com_mindprod_filetimes_FileTimes * Method: nativeGetFileLastModified * Signature: (Ljava/lang/String;)J */ JNIEXPORT jlong JNICALL Java_com_mindprod_filetimes_FileTimes_nativeGetFileLastModified ( JNIEnv * env, jclass callerclass, jstring filename) { FILETIME fttimelastModified; HANDLE handle; handle = getFileReadHandle( filename, env ); // INVALID_HANDLE_VALUE is -1 cast to HANDLE i.e. pointer type ) if ( handle == INVALID_HANDLE_VALUE ) return 0; // return true on success. GetFileTime( handle, // identifies the file NULL, NULL, &fttimelastModified ); // status of close is immaterial. Dir is now changed. CloseHandle ( handle ); return(jlong)(fttimelastModified.dwHighDateTime) << 32 | fttimelastModified.dwLowDateTime; } /* * Class: com_mindprod_filetimes_FileTimes * Method: nativeSetDirTimes * Signature: (Ljava/lang/String;JJJ)Z */ JNIEXPORT jboolean JNICALL Java_com_mindprod_filetimes_FileTimes_nativeSetDirTimes ( JNIEnv * env, jclass callerclass, jstring dirname, jlong timecreated, jlong timeaccessed, jlong timeupdated ) { // local vars // FILETIMES are just little-endian longs split in two. FILETIME fttimecreated; FILETIME fttimeaccessed; FILETIME fttimeupdated; HANDLE handle; jboolean result; fttimecreated.dwLowDateTime = (DWORD) (timecreated & 0xffffffff); fttimecreated.dwHighDateTime = (DWORD) ((timecreated >> 32) & 0xffffffff); fttimeaccessed.dwLowDateTime = (DWORD) (timeaccessed & 0xffffffff); fttimeaccessed.dwHighDateTime = (DWORD) ((timeaccessed >> 32) & 0xffffffff); fttimeupdated.dwLowDateTime = (DWORD) (timeupdated & 0xffffffff); fttimeupdated.dwHighDateTime = (DWORD) ((timeupdated >> 32) & 0xffffffff); handle = getDirWriteHandle( dirname, env ); // INVALID_HANDLE_VALUE is -1 cast to HANDLE i.e. pointer type ) if ( handle == INVALID_HANDLE_VALUE ) return 0; // return true on success. result = ( jboolean ) SetFileTime( handle, // identifies the dir timecreated ? &fttimecreated : NULL, timeaccessed ? &fttimeaccessed : NULL, timeupdated ? &fttimeupdated : NULL ); // status of close is immaterial. Dir is now changed. CloseHandle ( handle ); return result; } // end function /* * Class: com_mindprod_dirtimes_DirTimes * Method: nativeGetDirCreated * Signature: (Ljava/lang/String;)J */ JNIEXPORT jlong JNICALL Java_com_mindprod_filetimes_FileTimes_nativeGetDirCreated ( JNIEnv * env, jclass callerclass, jstring dirname) { FILETIME fttimecreated; HANDLE handle; handle = getDirReadHandle( dirname, env ); // INVALID_HANDLE_VALUE is -1 cast to HANDLE i.e. pointer type ) if ( handle == INVALID_HANDLE_VALUE ) return 0; // return true on success. GetFileTime( handle, // identifies the dir &fttimecreated, NULL, NULL ); // status of close is immaterial. Dir is now changed. CloseHandle ( handle ); return(jlong)(fttimecreated.dwHighDateTime) << 32 | fttimecreated.dwLowDateTime; } /* * Class: com_mindprod_filetimes_FileTimes * Method: nativeGetDirLastAccessed * Signature: (Ljava/lang/String;)J */ JNIEXPORT jlong JNICALL Java_com_mindprod_filetimes_FileTimes_nativeGetDirLastAccessed ( JNIEnv * env, jclass callerclass, jstring dirname) { FILETIME fttimelastAccessed; HANDLE handle; handle = getDirReadHandle( dirname, env ); // INVALID_HANDLE_VALUE is -1 cast to HANDLE i.e. pointer type ) if ( handle == INVALID_HANDLE_VALUE ) return 0; // return true on success. GetFileTime( handle, // identifies the dir NULL, &fttimelastAccessed, NULL ); // status of close is immaterial. Dir is now changed. CloseHandle ( handle ); return(jlong)(fttimelastAccessed.dwHighDateTime) << 32 | fttimelastAccessed.dwLowDateTime; } /* * Class: com_mindprod_filetimes_FileTimes * Method: nativeGetDirLastModified * Signature: (Ljava/lang/String;)J */ JNIEXPORT jlong JNICALL Java_com_mindprod_filetimes_FileTimes_nativeGetDirLastModified ( JNIEnv * env, jclass callerclass, jstring dirname) { FILETIME fttimelastModified; HANDLE handle; handle = getDirReadHandle( dirname, env ); // INVALID_HANDLE_VALUE is -1 cast to HANDLE i.e. pointer type ) if ( handle == INVALID_HANDLE_VALUE ) return 0; // return true on success. GetFileTime( handle, // identifies the dir NULL, NULL, &fttimelastModified ); // status of close is immaterial. Dir is now changed. CloseHandle ( handle ); return(jlong)(fttimelastModified.dwHighDateTime) << 32 | fttimelastModified.dwLowDateTime; } /** * get handle to dir we are about to examine. */ HANDLE getDirReadHandle( jstring dirname, JNIEnv * env ) { const char * dirname8; HANDLE handle; // convert java 16 bit dirname string to 8-bit style dirname8 = (*env)->GetStringUTFChars( env, dirname, NULL ); if ( dirname8 == NULL ) { return INVALID_HANDLE_VALUE; } // get handle to dir handle = CreateFile( dirname8, // identifies the dir GENERIC_READ, // access read mode FILE_SHARE_READ, // share mode NULL, // address of security descriptor OPEN_EXISTING, // how to create FILE_FLAG_BACKUP_SEMANTICS, // Tell Windows we are doing a backup. Lets us access directory times. // We don't always do this in case they later tighten security. NULL // handle of dir with attributes to copy ); (*env)->ReleaseStringUTFChars( env, dirname, dirname8 ); return handle; } /** * get handle to file we are about to examine. */ HANDLE getDirWriteHandle( jstring dirname, JNIEnv * env ) { const char * dirname8; HANDLE handle; // convert java 16 bit filename string to 8-bit style dirname8 = (*env)->GetStringUTFChars( env, dirname, NULL ); if ( dirname8 == NULL ) { return INVALID_HANDLE_VALUE; } // get handle to file handle = CreateFile( dirname8, // identifies the file GENERIC_WRITE, // access (read-write) mode FILE_SHARE_READ|FILE_SHARE_WRITE, // share mode NULL, // address of security descriptor OPEN_EXISTING, // how to create FILE_FLAG_BACKUP_SEMANTICS, // Tell Windows we are restoring from backup. Lets us modifiy directory. // We don't always do this in case they later tighten security. NULL // handle of file with attributes to copy ); (*env)->ReleaseStringUTFChars( env, dirname, dirname8 ); return handle; } /** * get handle to file we are about to examine. */ HANDLE getFileReadHandle( jstring filename, JNIEnv * env ) { const char * filename8; HANDLE handle; // convert java 16 bit filename string to 8-bit style filename8 = (*env)->GetStringUTFChars( env, filename, NULL ); if ( filename8 == NULL ) { return INVALID_HANDLE_VALUE; } // get handle to file handle = CreateFile( filename8, // identifies the file GENERIC_READ, // access read mode FILE_SHARE_READ, // share mode NULL, // address of security descriptor OPEN_EXISTING, // how to create 0, // file attributes NULL // handle of file with attributes to copy ); (*env)->ReleaseStringUTFChars( env, filename, filename8 ); return handle; } /** * get handle to file we are about to examine. */ HANDLE getFileWriteHandle( jstring filename, JNIEnv * env ) { const char * filename8; HANDLE handle; // convert java 16 bit filename string to 8-bit style filename8 = (*env)->GetStringUTFChars( env, filename, NULL ); if ( filename8 == NULL ) { return INVALID_HANDLE_VALUE; } // get handle to file handle = CreateFile( filename8, // identifies the file GENERIC_WRITE, // access (read-write) mode FILE_SHARE_READ|FILE_SHARE_WRITE, // share mode NULL, // address of security descriptor OPEN_EXISTING, // how to create 0, // file attributes NULL // handle of file with attributes to copy ); (*env)->ReleaseStringUTFChars( env, filename, filename8 ); return handle; } // end nativefiletimes.c