| 1 | // natFileWin32.cc - Native part of File class for Win32.
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 1998, 1999, 2002, 2003 Free Software Foundation, Inc.
|
|---|
| 4 |
|
|---|
| 5 | This file is part of libgcj.
|
|---|
| 6 |
|
|---|
| 7 | This software is copyrighted work licensed under the terms of the
|
|---|
| 8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|---|
| 9 | details. */
|
|---|
| 10 |
|
|---|
| 11 | #include <config.h>
|
|---|
| 12 |
|
|---|
| 13 | #include <stdio.h>
|
|---|
| 14 | #include <string.h>
|
|---|
| 15 |
|
|---|
| 16 | #include <windows.h>
|
|---|
| 17 | #undef STRICT
|
|---|
| 18 |
|
|---|
| 19 | #include <gcj/cni.h>
|
|---|
| 20 | #include <jvm.h>
|
|---|
| 21 | #include <java/io/File.h>
|
|---|
| 22 | #include <java/io/IOException.h>
|
|---|
| 23 | #include <java/util/Vector.h>
|
|---|
| 24 | #include <java/lang/String.h>
|
|---|
| 25 | #include <java/io/FilenameFilter.h>
|
|---|
| 26 | #include <java/io/FileFilter.h>
|
|---|
| 27 | #include <java/lang/System.h>
|
|---|
| 28 |
|
|---|
| 29 | // Java timestamps are milliseconds since the UNIX epoch (00:00:00 UTC on
|
|---|
| 30 | // January 1, 1970) while Win32 file-times are 100-nanosecond intervals
|
|---|
| 31 | // since the Win32 epoch (00:00:00 UTC on January 1, 1601). The following
|
|---|
| 32 | // constant represents the number of milliseconds to be added to a
|
|---|
| 33 | // Java timestamp to base it on the Win32 epoch.
|
|---|
| 34 | //
|
|---|
| 35 | // There were 369 years between 1601 and 1970, including 89 leap years
|
|---|
| 36 | // (since 1700, 1800 and 1900 were not leap years):
|
|---|
| 37 | //
|
|---|
| 38 | // (89*366 + 280*365) days * 86400 seconds/day = 11644473600 seconds
|
|---|
| 39 | //
|
|---|
| 40 | #define WIN32_EPOCH_MILLIS 11644473600000LL
|
|---|
| 41 |
|
|---|
| 42 | jboolean
|
|---|
| 43 | java::io::File::_access (jint query)
|
|---|
| 44 | {
|
|---|
| 45 | jstring canon = getCanonicalPath();
|
|---|
| 46 | if (! canon)
|
|---|
| 47 | return false;
|
|---|
| 48 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (canon) + 1);
|
|---|
| 49 | jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
|
|---|
| 50 | buf[total] = '\0';
|
|---|
| 51 |
|
|---|
| 52 | JvAssert (query == READ || query == WRITE || query == EXISTS);
|
|---|
| 53 |
|
|---|
| 54 | // FIXME: Is it possible to differentiate between existing and reading?
|
|---|
| 55 | // If the file exists but cannot be read because of the secuirty attributes
|
|---|
| 56 | // on an NTFS disk this wont work (it reports it can be read but cant)
|
|---|
| 57 | // Could we use something from the security API?
|
|---|
| 58 | DWORD attributes = GetFileAttributes (buf);
|
|---|
| 59 | if ((query == EXISTS) || (query == READ))
|
|---|
| 60 | return (attributes == 0xffffffff) ? false : true;
|
|---|
| 61 | else
|
|---|
| 62 | return ((attributes != 0xffffffff) && ((attributes & FILE_ATTRIBUTE_READONLY) == 0)) ? true : false;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | jboolean
|
|---|
| 66 | java::io::File::_stat (jint query)
|
|---|
| 67 | {
|
|---|
| 68 | jstring canon = getCanonicalPath();
|
|---|
| 69 | if (! canon)
|
|---|
| 70 | return false;
|
|---|
| 71 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (canon) + 1);
|
|---|
| 72 | jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
|
|---|
| 73 | buf[total] = '\0';
|
|---|
| 74 |
|
|---|
| 75 | JvAssert (query == DIRECTORY || query == ISFILE);
|
|---|
| 76 |
|
|---|
| 77 | DWORD attributes = GetFileAttributes (buf);
|
|---|
| 78 | if (attributes == 0xffffffff)
|
|---|
| 79 | return false;
|
|---|
| 80 |
|
|---|
| 81 | if (query == DIRECTORY)
|
|---|
| 82 | return attributes & FILE_ATTRIBUTE_DIRECTORY ? true : false;
|
|---|
| 83 | else
|
|---|
| 84 | return attributes & FILE_ATTRIBUTE_DIRECTORY ? false : true;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | jlong
|
|---|
| 88 | java::io::File::attr (jint query)
|
|---|
| 89 | {
|
|---|
| 90 | jstring canon = getCanonicalPath();
|
|---|
| 91 | if (! canon)
|
|---|
| 92 | return false;
|
|---|
| 93 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (canon) + 1);
|
|---|
| 94 | jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
|
|---|
| 95 | buf[total] = '\0';
|
|---|
| 96 |
|
|---|
| 97 | JvAssert (query == MODIFIED || query == LENGTH);
|
|---|
| 98 |
|
|---|
| 99 | WIN32_FIND_DATA info;
|
|---|
| 100 | HANDLE sHandle;
|
|---|
| 101 | if ( ( sHandle = FindFirstFile( buf, &info)) == INVALID_HANDLE_VALUE)
|
|---|
| 102 | return 0;
|
|---|
| 103 |
|
|---|
| 104 | FindClose( sHandle);
|
|---|
| 105 |
|
|---|
| 106 | if (query == LENGTH)
|
|---|
| 107 | return ((long long)info.nFileSizeHigh) << 32
|
|---|
| 108 | | (unsigned long long)info.nFileSizeLow;
|
|---|
| 109 | else
|
|---|
| 110 | {
|
|---|
| 111 | // The file time as returned by Windows is in terms of the number
|
|---|
| 112 | // of 100-nanosecond intervals since 00:00:00 UTC, January 1, 1601.
|
|---|
| 113 | return (((((long long)info.ftLastWriteTime.dwHighDateTime) << 32)
|
|---|
| 114 | | ((unsigned long long)info.ftLastWriteTime.dwLowDateTime))
|
|---|
| 115 | - WIN32_EPOCH_MILLIS*10000LL) / 10000LL;
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | jstring
|
|---|
| 120 | java::io::File::getCanonicalPath (void)
|
|---|
| 121 | {
|
|---|
| 122 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
|---|
| 123 | jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
|---|
| 124 | buf[total] = '\0';
|
|---|
| 125 |
|
|---|
| 126 | LPTSTR unused;
|
|---|
| 127 | char buf2[MAX_PATH];
|
|---|
| 128 | if(!GetFullPathName(buf, MAX_PATH, buf2, &unused))
|
|---|
| 129 | throw new IOException (JvNewStringLatin1 ("GetFullPathName failed"));
|
|---|
| 130 |
|
|---|
| 131 | // FIXME: what encoding to assume for file names? This affects many
|
|---|
| 132 | // calls.
|
|---|
| 133 | return JvNewStringUTF(buf2);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | jboolean
|
|---|
| 137 | java::io::File::isAbsolute (void)
|
|---|
| 138 | {
|
|---|
| 139 | // See if the path represents a Windows UNC network path.
|
|---|
| 140 | if (path->length () > 2
|
|---|
| 141 | && (path->charAt (0) == '\\') && (path->charAt (1) == '\\'))
|
|---|
| 142 | return true;
|
|---|
| 143 |
|
|---|
| 144 | // Note that the path is not an absolute path even if it starts with
|
|---|
| 145 | // a '/' or a '\' because it lacks a drive specifier.
|
|---|
| 146 |
|
|---|
| 147 | if (path->length() < 3)
|
|---|
| 148 | return false;
|
|---|
|
|---|