* MAINTAINERS (Write After Approval): Add myself.
[official-gcc.git] / libjava / java / io / natFileWin32.cc
bloba8e88789222f0cd7cbf36405b63a542ab8fdc9db
1 // natFileWin32.cc - Native part of File class for Win32.
3 /* Copyright (C) 1998, 1999, 2002, 2003, 2012 Free Software Foundation, Inc.
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 #include <config.h>
12 #include <platform.h>
14 #include <stdio.h>
15 #include <string.h>
17 #undef STRICT
19 #include <java/io/File.h>
20 #include <java/io/IOException.h>
21 #include <java/util/Vector.h>
22 #include <java/lang/String.h>
23 #include <java/io/FilenameFilter.h>
24 #include <java/io/FileFilter.h>
25 #include <java/lang/System.h>
27 // Java timestamps are milliseconds since the UNIX epoch (00:00:00 UTC on
28 // January 1, 1970) while Win32 file-times are 100-nanosecond intervals
29 // since the Win32 epoch (00:00:00 UTC on January 1, 1601). The following
30 // constant represents the number of milliseconds to be added to a
31 // Java timestamp to base it on the Win32 epoch.
32 //
33 // There were 369 years between 1601 and 1970, including 89 leap years
34 // (since 1700, 1800 and 1900 were not leap years):
36 // (89*366 + 280*365) days * 86400 seconds/day = 11644473600 seconds
38 #define WIN32_EPOCH_MILLIS 11644473600000LL
40 jboolean
41 java::io::File::access (jint query)
43 JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
44 if (!canon)
45 return false;
47 JvAssert (query == READ || query == WRITE || query == EXISTS
48 || query == EXEC);
50 // FIXME: Is it possible to differentiate between existing and reading?
51 // If the file exists but cannot be read because of the secuirty attributes
52 // on an NTFS disk this wont work (it reports it can be read but cant)
53 // Could we use something from the security API?
54 DWORD attributes = GetFileAttributes (canon);
55 // FIXME: handle EXEC
56 if (query == EXEC)
57 return false;
58 if ((query == EXISTS) || (query == READ))
59 return (attributes == 0xffffffff) ? false : true;
60 else
61 return ((attributes != 0xffffffff) &&
62 ((attributes & FILE_ATTRIBUTE_READONLY) == 0)) ? true : false;
65 jboolean
66 java::io::File::stat (jint query)
68 JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
69 if (!canon)
70 return false;
72 JvAssert (query == DIRECTORY || query == ISFILE);
74 DWORD attributes = GetFileAttributes (canon);
75 if (attributes == 0xffffffff)
76 return false;
78 if (query == DIRECTORY)
79 return attributes & FILE_ATTRIBUTE_DIRECTORY ? true : false;
80 else
81 return attributes & FILE_ATTRIBUTE_DIRECTORY ? false : true;
84 jlong
85 java::io::File::attr (jint query)
87 JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
88 if (!canon)
89 return false;
91 JvAssert (query == MODIFIED || query == LENGTH);
93 WIN32_FIND_DATA info;
94 HANDLE sHandle;
95 if ( ( sHandle = FindFirstFile( canon, &info)) == INVALID_HANDLE_VALUE)
96 return 0;
98 FindClose( sHandle);
100 if (query == LENGTH)
101 return ((long long)info.nFileSizeHigh) << 32
102 | (unsigned long long)info.nFileSizeLow;
103 else
105 // The file time as returned by Windows is in terms of the number
106 // of 100-nanosecond intervals since 00:00:00 UTC, January 1, 1601.
107 return (((((long long)info.ftLastWriteTime.dwHighDateTime) << 32)
108 | ((unsigned long long)info.ftLastWriteTime.dwLowDateTime))
109 - WIN32_EPOCH_MILLIS*10000LL) / 10000LL;
113 jstring
114 java::io::File::getCanonicalPath (void)
116 JV_TEMP_STRING_WIN32 (cpath, path);
118 // If the filename is blank, use the current directory.
119 LPCTSTR thepath = cpath.buf();
120 if (*thepath == 0)
121 thepath = _T(".");
123 LPTSTR unused;
124 TCHAR buf2[MAX_PATH];
125 if(!GetFullPathName(thepath, MAX_PATH, buf2, &unused))
126 throw new IOException (JvNewStringLatin1 ("GetFullPathName failed"));
128 return _Jv_Win32NewString (buf2);
131 jboolean
132 java::io::File::isAbsolute (void)
134 // See if the path represents a Windows UNC network path.
135 if (path->length () > 2
136 && (path->charAt (0) == '\\') && (path->charAt (1) == '\\'))
137 return true;
139 // Note that the path is not an absolute path even if it starts with
140 // a '/' or a '\' because it lacks a drive specifier.
142 if (path->length() < 3)
143 return false;
144 // Hard-code A-Za-z because Windows (I think) can't use non-ASCII
145 // letters as drive names.
146 if ((path->charAt(0) < 'a' || path->charAt(0) > 'z')
147 && (path->charAt(0) < 'A' || path->charAt(0) > 'Z'))
148 return false;
149 return (path->charAt(1) == ':'
150 && (path->charAt(2) == '/' || path->charAt(2) == '\\'));
153 void java::io::File::init_native ()
155 maxPathLen = MAX_PATH;
156 caseSensitive = false;
159 jobjectArray
160 java::io::File::performList (java::io::FilenameFilter *filter,
161 java::io::FileFilter *fileFilter,
162 java::lang::Class *clazz)
164 jstring canon = getCanonicalPath();
165 if (! canon)
166 return NULL;
168 int len = canon->length();
169 TCHAR buf[len + 5];
171 JV_TEMP_STRING_WIN32(canonstr, canon);
173 _tcscpy(buf, canonstr);
174 if (buf[len - 1] == _T('\\'))
175 _tcscpy (&buf[len], _T("*.*"));
176 else
177 _tcscpy (&buf[len], _T("\\*.*"));
179 WIN32_FIND_DATA data;
180 HANDLE handle = FindFirstFile (buf, &data);
181 if (handle == INVALID_HANDLE_VALUE)
182 return NULL;
184 java::util::Vector *vec = new java::util::Vector ();
188 if (_tcscmp (data.cFileName, _T(".")) &&
189 _tcscmp (data.cFileName, _T("..")))
191 jstring name = _Jv_Win32NewString (data.cFileName);
193 if (filter && !filter->accept(this, name))
194 continue;
195 if (clazz == &java::io::File::class$)
197 java::io::File *file = new java::io::File (this, name);
198 if (fileFilter && !fileFilter->accept(file))
199 continue;
200 vec->addElement (file);
202 else
203 vec->addElement (name);
206 while (FindNextFile (handle, &data));
208 if (GetLastError () != ERROR_NO_MORE_FILES)
209 return NULL;
211 FindClose (handle);
213 jobjectArray ret = JvNewObjectArray (vec->size(), clazz, NULL);
214 vec->copyInto (ret);
215 return ret;
218 jboolean
219 java::io::File::setFilePermissions (jboolean enable,
220 jboolean ownerOnly,
221 jint permissions)
223 JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
224 if (!canon)
225 return false;
227 DWORD attrs = GetFileAttributes (canon);
228 if (attrs != INVALID_FILE_ATTRIBUTES)
230 // FIXME: implement
231 return false;
233 else
234 return false;
237 jboolean
238 java::io::File::performMkdir (void)
240 JV_TEMP_STRING_WIN32 (cpath, path);
241 return (CreateDirectory(cpath, NULL)) ? true : false;
244 jboolean
245 java::io::File::performRenameTo (File *dest)
247 JV_TEMP_STRING_WIN32 (pathFrom, path);
248 JV_TEMP_STRING_WIN32 (pathTo, dest->path);
249 return (MoveFile(pathFrom, pathTo)) ? true : false;
252 jboolean
253 java::io::File::performDelete ()
255 JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
256 if (!canon)
257 return false;
259 DWORD attributes = GetFileAttributes (canon);
260 if (attributes == 0xffffffff)
261 return false;
263 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
264 return (RemoveDirectory (canon)) ? true : false;
265 else
266 return (DeleteFile (canon)) ? true : false;
269 jboolean java::io::File::performCreate (void)
271 JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
272 if (!canon)
273 return false;
275 HANDLE h = CreateFile (canon, 0, 0, NULL, CREATE_NEW,
276 FILE_ATTRIBUTE_NORMAL, NULL);
277 if (h != INVALID_HANDLE_VALUE)
279 CloseHandle (h);
280 return true;
282 else
284 if (GetLastError () == ERROR_ALREADY_EXISTS)
285 return false;
286 else
287 throw new IOException (JvNewStringLatin1 ("CreateFile failed"));
291 jboolean java::io::File::performSetReadOnly ()
293 JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
294 if (!canon)
295 return false;
297 DWORD attrs = GetFileAttributes (canon);
298 if (attrs != INVALID_FILE_ATTRIBUTES)
300 if (SetFileAttributes (canon, attrs | FILE_ATTRIBUTE_READONLY) != 0)
301 return true;
302 else
303 return false;
305 else
306 return false;
309 jboolean java::io::File::performSetLastModified (jlong time)
311 JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
312 if (!canon)
313 return false;
315 FILETIME modTime;
316 long long mTime100ns = ((long long) time /* Ha! */
317 + WIN32_EPOCH_MILLIS) * 10000LL;
319 modTime.dwLowDateTime = (DWORD) mTime100ns;
320 modTime.dwHighDateTime = (DWORD) (mTime100ns >> 32);
322 jboolean retVal = false;
323 HANDLE h = CreateFile (canon, FILE_WRITE_ATTRIBUTES,
324 FILE_SHARE_READ | FILE_SHARE_WRITE,
325 NULL, OPEN_EXISTING, 0, NULL);
327 if (h != INVALID_HANDLE_VALUE)
329 if (SetFileTime (h, NULL, &modTime, &modTime) != 0)
330 retVal = true;
332 CloseHandle (h);
335 return retVal;
338 JArray<java::io::File*>* java::io::File::performListRoots ()
340 DWORD drivesBitmap = GetLogicalDrives ();
341 DWORD mask;
343 // Possible drive letters are from ASCII 'A'-'Z'.
344 int numDrives = 0;
345 mask = 1;
346 for (int i = 0; i < 26; i++)
348 if ((drivesBitmap & mask) != 0)
349 numDrives++;
350 mask <<= 1;
353 JArray<java::io::File *> *roots
354 = reinterpret_cast <JArray<java::io::File *>*>
355 (JvNewObjectArray (numDrives, &java::io::File::class$, NULL));
357 ::java::io::File **rootsArray = elements (roots);
359 char aDriveRoot[] = {'A', ':', '\\', '\0'};
360 mask = 1;
361 for (int i = 0, j = 0; i < 26; i++)
363 if ((drivesBitmap & mask) != 0)
365 rootsArray[j]
366 = new ::java::io::File (JvNewStringLatin1 (aDriveRoot));
367 j++;
369 mask <<= 1;
370 aDriveRoot[0]++;
373 return roots;