| 1 | // natFileWin32.cc - Native part of File class.
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 1998, 1999, 2002 Red Hat, 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 | jboolean
|
|---|
| 30 | java::io::File::_access (jint query)
|
|---|
| 31 | {
|
|---|
| 32 | jstring canon = getCanonicalPath();
|
|---|
| 33 | if (! canon)
|
|---|
| 34 | return false;
|
|---|
| 35 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (canon) + 1);
|
|---|
| 36 | jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
|
|---|
| 37 | buf[total] = '\0';
|
|---|
| 38 |
|
|---|
| 39 | JvAssert (query == READ || query == WRITE || query == EXISTS);
|
|---|
| 40 |
|
|---|
| 41 | // FIXME: Is it possible to differentiate between existing and reading?
|
|---|
| 42 | // If the file exists but cannot be read because of the secuirty attributes
|
|---|
| 43 | // on an NTFS disk this wont work (it reports it can be read but cant)
|
|---|
| 44 | // Could we use something from the security API?
|
|---|
| 45 | DWORD attributes = GetFileAttributes (buf);
|
|---|
| 46 | if ((query == EXISTS) || (query == READ))
|
|---|
| 47 | return (attributes == 0xffffffff) ? false : true;
|
|---|
| 48 | else
|
|---|
| 49 | return ((attributes != 0xffffffff) && ((attributes & FILE_ATTRIBUTE_READONLY) == 0)) ? true : false;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | jboolean
|
|---|
| 53 | java::io::File::_stat (jint query)
|
|---|
| 54 | {
|
|---|
| 55 | jstring canon = getCanonicalPath();
|
|---|
| 56 | if (! canon)
|
|---|
| 57 | return false;
|
|---|
| 58 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (canon) + 1);
|
|---|
| 59 | jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
|
|---|
| 60 | buf[total] = '\0';
|
|---|
| 61 |
|
|---|
| 62 | JvAssert (query == DIRECTORY || query == ISFILE);
|
|---|
| 63 |
|
|---|
| 64 | DWORD attributes = GetFileAttributes (buf);
|
|---|
| 65 | if (attributes == 0xffffffff)
|
|---|
| 66 | return false;
|
|---|
| 67 |
|
|---|
| 68 | if (query == DIRECTORY)
|
|---|
| 69 | return attributes & FILE_ATTRIBUTE_DIRECTORY ? true : false;
|
|---|
| 70 | else
|
|---|
| 71 | return attributes & FILE_ATTRIBUTE_DIRECTORY ? false : true;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | jlong
|
|---|
| 75 | java::io::File::attr (jint query)
|
|---|
| 76 | {
|
|---|
| 77 | jstring canon = getCanonicalPath();
|
|---|
| 78 | if (! canon)
|
|---|
| 79 | return false;
|
|---|
| 80 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (canon) + 1);
|
|---|
| 81 | jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
|
|---|
| 82 | buf[total] = '\0';
|
|---|
| 83 |
|
|---|
| 84 | JvAssert (query == MODIFIED || query == LENGTH);
|
|---|
| 85 |
|
|---|
| 86 | WIN32_FILE_ATTRIBUTE_DATA info;
|
|---|
| 87 | if (! GetFileAttributesEx(buf, GetFileExInfoStandard, &info))
|
|---|
| 88 | return 0;
|
|---|
| 89 |
|
|---|
| 90 | if (query == LENGTH)
|
|---|
| 91 | return ((long long)info.nFileSizeHigh) << 32 | (unsigned long long)info.nFileSizeLow;
|
|---|
| 92 | else {
|
|---|
| 93 | // FIXME? This is somewhat compiler dependant (the LL constant suffix)
|
|---|
| 94 | // The file time as return by windows is the number of 100-nanosecond intervals since January 1, 1601
|
|---|
| 95 | return (((((long long)info.ftLastWriteTime.dwHighDateTime) << 32) | ((unsigned long long)info.ftLastWriteTime.dwLowDateTime)) - 116444736000000000LL) / 10000LL;
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | jstring
|
|---|
| 100 | java::io::File::getCanonicalPath (void)
|
|---|
| 101 | {
|
|---|
| 102 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
|---|
| 103 | jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
|---|
| 104 | buf[total] = '\0';
|
|---|
| 105 |
|
|---|
| 106 | LPTSTR unused;
|
|---|
| 107 | char buf2[MAX_PATH];
|
|---|
| 108 | if(!GetFullPathName(buf, MAX_PATH, buf2, &unused))
|
|---|
| 109 | throw new IOException (JvNewStringLatin1 ("GetFullPathName failed"));
|
|---|
| 110 |
|
|---|
| 111 | // FIXME: what encoding to assume for file names? This affects many
|
|---|
| 112 | // calls.
|
|---|
| 113 | return JvNewStringUTF(buf2);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | jboolean
|
|---|
| 117 | java::io::File::isAbsolute (void)
|
|---|
| 118 | {
|
|---|
| 119 | if (path->charAt(0) == '/' || path->charAt(0) == '\\')
|
|---|
| 120 | return true;
|
|---|
| 121 | if (path->length() < 3)
|
|---|
| 122 | return false;
|
|---|
| 123 | // Hard-code A-Za-z because Windows (I think) can't use non-ASCII
|
|---|
| 124 | // letters as drive names.
|
|---|
| 125 | if ((path->charAt(0) < 'a' || path->charAt(0) > 'z')
|
|---|
| 126 | && (path->charAt(0) < 'A' || path->charAt(0) > 'Z'))
|
|---|
| 127 | return false;
|
|---|
| 128 | return (path->charAt(1) == ':'
|
|---|
| 129 | && (path->charAt(2) == '/' || path->charAt(2) == '\\'));
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | void java::io::File::init_native() { }
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 | jobjectArray
|
|---|
| 136 | java::io::File::performList (java::io::FilenameFilter *filter,
|
|---|
| 137 | java::io::FileFilter *fileFilter,
|
|---|
| 138 | java::lang::Class *clazz)
|
|---|
| 139 | {
|
|---|
| 140 | jstring canon = getCanonicalPath();
|
|---|
| 141 | if (! canon)
|
|---|
| 142 | return NULL;
|
|---|
| 143 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (canon) + 5);
|
|---|
| 144 | jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
|
|---|
| 145 | // FIXME?
|
|---|
| 146 | strcpy(&buf[total], "\\*.*");
|
|---|
| 147 |
|
|---|
| 148 | WIN32_FIND_DATA data;
|
|---|
| 149 | HANDLE handle = FindFirstFile (buf, &data);
|
|---|
| 150 | if (handle == INVALID_HANDLE_VALUE)
|
|---|
| 151 | return NULL;
|
|---|
| 152 |
|
|---|
| 153 | java::util::Vector *vec = new java::util::Vector ();
|
|---|
| 154 |
|
|---|
| 155 | do
|
|---|
| 156 | {
|
|---|
| 157 | if (strcmp (data.cFileName, ".") && strcmp (data.cFileName, ".."))
|
|---|
| 158 | {
|
|---|
| 159 | jstring name = JvNewStringUTF (data.cFileName);
|
|---|
| 160 |
|
|---|
| 161 | if (filter && !filter->accept(this, name))
|
|---|
| 162 | continue;
|
|---|
| 163 | if (clazz == &java::io::File::class$)
|
|---|
| 164 | {
|
|---|
| 165 | java::io::File *file = new java::io::File (this, name);
|
|---|
| 166 | if (fileFilter && !fileFilter->accept(file))
|
|---|
| 167 | continue;
|
|---|
| 168 | vec->addElement (file);
|
|---|
| 169 | }
|
|---|
| 170 | else
|
|---|
| 171 | vec->addElement (name);
|
|---|
| 172 | }
|
|---|
| 173 | }
|
|---|
| 174 | while (FindNextFile (handle, &data));
|
|---|
| 175 |
|
|---|
| 176 | if (GetLastError () != ERROR_NO_MORE_FILES)
|
|---|
| 177 | return NULL;
|
|---|
| 178 |
|
|---|
| 179 | FindClose (handle);
|
|---|
| 180 |
|
|---|
| 181 | jobjectArray ret = JvNewObjectArray (vec->size(), clazz, NULL);
|
|---|
| 182 | vec->copyInto (ret);
|
|---|
| 183 | return ret;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 | jboolean
|
|---|
| 188 | java::io::File::performMkdir (void)
|
|---|
| 189 | {
|
|---|
| 190 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
|---|
| 191 | jsize total = JvGetStringUTFRegion(path, 0, path->length(), buf);
|
|---|
| 192 | buf[total] = '\0';
|
|---|
| 193 |
|
|---|
| 194 | return (CreateDirectory(buf, NULL)) ? true : false;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | jboolean
|
|---|
| 198 | java::io::File::performRenameTo (File *dest)
|
|---|
| 199 | {
|
|---|
| 200 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 1);
|
|---|
| 201 | jsize total = JvGetStringUTFRegion(path, 0, path->length(), buf);
|
|---|
| 202 | buf[total] = '\0';
|
|---|
| 203 | char *buf2 = (char *) __builtin_alloca (JvGetStringUTFLength (dest->path)
|
|---|
| 204 | + 1);
|
|---|
| 205 | total = JvGetStringUTFRegion(dest->path, 0, dest->path->length(), buf2);
|
|---|
| 206 | buf2[total] = '\0';
|
|---|
| 207 |
|
|---|
| 208 | return (MoveFile(buf, buf2)) ? true : false;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | jboolean
|
|---|
| 212 | java::io::File::performDelete ()
|
|---|
| 213 | {
|
|---|
| 214 | jstring canon = getCanonicalPath();
|
|---|
| 215 | char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (canon) + 1);
|
|---|
| 216 | jsize total = JvGetStringUTFRegion(canon, 0, canon->length(), buf);
|
|---|
| 217 | buf[total] = '\0';
|
|---|
| 218 |
|
|---|
| 219 | DWORD attributes = GetFileAttributes (buf);
|
|---|
| 220 | if (attributes == 0xffffffff)
|
|---|
| 221 | return false;
|
|---|
| 222 |
|
|---|
| 223 | if (attributes & FILE_ATTRIBUTE_DIRECTORY)
|
|---|
| 224 | return (RemoveDirectory (buf)) ? true : false;
|
|---|
| 225 | else
|
|---|
| 226 | return (DeleteFile (buf)) ? true : false;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | jboolean java::io::File::performCreate (void) { JvFail("unimplemented\n"); }
|
|---|
| 230 | jboolean java::io::File::performSetReadOnly() { JvFail("unimplemented"); }
|
|---|
| 231 | jboolean java::io::File::performSetLastModified(jlong time) { JvFail("unimplemented"); }
|
|---|
| 232 | JArray<java::io::File*>* java::io::File::performListRoots() { JvFail("unimplemented"); }
|
|---|