OSDN Git Service

2004-04-07 H.J. Lu <hongjiu.lu@intel.com>
[pf3gnuchains/gcc-fork.git] / libjava / java / io / natFileWin32.cc
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 #include <platform.h>
13
14 #include <stdio.h>
15 #include <string.h>
16
17 #undef STRICT
18
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>
26
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):
35 //
36 // (89*366 + 280*365) days * 86400 seconds/day = 11644473600 seconds
37 //
38 #define WIN32_EPOCH_MILLIS 11644473600000LL
39
40 jboolean
41 java::io::File::_access (jint query)
42 {
43   JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
44   if (!canon)
45     return false;
46
47   JvAssert (query == READ || query == WRITE || query == EXISTS);
48
49   // FIXME: Is it possible to differentiate between existing and reading?
50   // If the file exists but cannot be read because of the secuirty attributes
51   // on an NTFS disk this wont work (it reports it can be read but cant)
52   // Could we use something from the security API?
53   DWORD attributes = GetFileAttributes (canon);
54   if ((query == EXISTS) || (query == READ))
55     return (attributes == 0xffffffff) ? false : true;
56   else
57     return ((attributes != 0xffffffff) &&
58       ((attributes & FILE_ATTRIBUTE_READONLY) == 0)) ? true : false;
59 }
60
61 jboolean
62 java::io::File::_stat (jint query)
63 {
64   JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
65   if (!canon)
66     return false;
67
68   JvAssert (query == DIRECTORY || query == ISFILE);
69
70   DWORD attributes = GetFileAttributes (canon);
71   if (attributes == 0xffffffff)
72     return false;
73
74   if (query == DIRECTORY)
75     return attributes & FILE_ATTRIBUTE_DIRECTORY ? true : false;
76   else
77     return attributes & FILE_ATTRIBUTE_DIRECTORY ? false : true;
78 }
79
80 jlong
81 java::io::File::attr (jint query)
82 {
83   JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
84   if (!canon)
85     return false;
86
87   JvAssert (query == MODIFIED || query == LENGTH);
88
89   WIN32_FIND_DATA info;
90   HANDLE sHandle;
91   if ( ( sHandle = FindFirstFile( canon, &info)) == INVALID_HANDLE_VALUE)
92     return 0;
93   
94   FindClose( sHandle);
95   
96   if (query == LENGTH)
97     return ((long long)info.nFileSizeHigh) << 32 
98            | (unsigned long long)info.nFileSizeLow;
99   else 
100     {
101       // The file time as returned by Windows is in terms of the number
102       // of 100-nanosecond intervals since 00:00:00 UTC, January 1, 1601.
103       return (((((long long)info.ftLastWriteTime.dwHighDateTime) << 32) 
104                | ((unsigned long long)info.ftLastWriteTime.dwLowDateTime)) 
105               - WIN32_EPOCH_MILLIS*10000LL) / 10000LL;
106     }
107 }
108
109 jstring
110 java::io::File::getCanonicalPath (void)
111 {
112   JV_TEMP_STRING_WIN32 (cpath, path);
113   
114   // If the filename is blank, use the current directory.
115   LPCTSTR thepath = cpath.buf();
116   if (*thepath == 0)
117     thepath = _T(".");
118
119   LPTSTR unused;
120   TCHAR buf2[MAX_PATH];
121   if(!GetFullPathName(thepath, MAX_PATH, buf2, &unused))
122     throw new IOException (JvNewStringLatin1 ("GetFullPathName failed"));
123
124   return _Jv_Win32NewString (buf2);
125 }
126
127 jboolean
128 java::io::File::isAbsolute (void)
129 {
130   // See if the path represents a Windows UNC network path.
131   if (path->length () > 2
132       && (path->charAt (0) == '\\') && (path->charAt (1) == '\\'))
133     return true;
134
135   // Note that the path is not an absolute path even if it starts with
136   // a '/' or a '\' because it lacks a drive specifier.
137
138   if (path->length() < 3)
139     return false;
140   // Hard-code A-Za-z because Windows (I think) can't use non-ASCII
141   // letters as drive names.
142   if ((path->charAt(0) < 'a' || path->charAt(0) > 'z')
143       && (path->charAt(0) < 'A' || path->charAt(0) > 'Z'))
144     return false;
145   return (path->charAt(1) == ':'
146     && (path->charAt(2) == '/' || path->charAt(2) == '\\'));
147 }
148
149 void java::io::File::init_native () 
150 {
151   maxPathLen = MAX_PATH;
152   caseSensitive = false;
153 }
154
155 jobjectArray
156 java::io::File::performList (java::io::FilenameFilter *filter, 
157            java::io::FileFilter *fileFilter, 
158            java::lang::Class *clazz)
159 {
160   jstring canon = getCanonicalPath();
161   if (! canon)
162     return NULL;
163
164   int len = canon->length();
165   TCHAR buf[len + 5];
166   
167   JV_TEMP_STRING_WIN32(canonstr, canon);
168   
169   _tcscpy(buf, canonstr);
170   if (buf[len - 1] == _T('\\'))
171     _tcscpy (&buf[len], _T("*.*"));
172   else
173     _tcscpy (&buf[len], _T("\\*.*"));
174
175   WIN32_FIND_DATA data;
176   HANDLE handle = FindFirstFile (buf, &data);
177   if (handle == INVALID_HANDLE_VALUE)
178     return NULL;
179
180   java::util::Vector *vec = new java::util::Vector ();
181
182   do
183     {
184       if (_tcscmp (data.cFileName, _T(".")) &&
185         _tcscmp (data.cFileName, _T("..")))
186         {
187           jstring name = _Jv_Win32NewString (data.cFileName);
188
189           if (filter && !filter->accept(this, name))
190             continue;
191           if (clazz == &java::io::File::class$)
192             {
193               java::io::File *file = new java::io::File (this, name);
194               if (fileFilter && !fileFilter->accept(file))
195                 continue;
196               vec->addElement (file);
197             }
198           else
199             vec->addElement (name);
200         }
201     }
202   while (FindNextFile (handle, &data));
203
204   if (GetLastError () != ERROR_NO_MORE_FILES)
205     return NULL;
206
207   FindClose (handle);
208
209   jobjectArray ret = JvNewObjectArray (vec->size(), clazz, NULL);
210   vec->copyInto (ret);
211   return ret;
212 }
213
214 jboolean
215 java::io::File::performMkdir (void)
216 {
217   JV_TEMP_STRING_WIN32 (cpath, path);
218   return (CreateDirectory(cpath, NULL)) ? true : false;
219 }
220
221 jboolean
222 java::io::File::performRenameTo (File *dest)
223 {
224   JV_TEMP_STRING_WIN32 (pathFrom, path);
225   JV_TEMP_STRING_WIN32 (pathTo, dest->path);
226   return (MoveFile(pathFrom, pathTo)) ? true : false;
227 }
228
229 jboolean
230 java::io::File::performDelete ()
231 {
232   JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
233   if (!canon)
234     return false;
235
236   DWORD attributes = GetFileAttributes (canon);
237   if (attributes == 0xffffffff)
238     return false;
239
240   if (attributes & FILE_ATTRIBUTE_DIRECTORY)
241     return (RemoveDirectory (canon)) ? true : false;
242   else
243     return (DeleteFile (canon)) ? true : false;
244 }
245
246 jboolean java::io::File::performCreate (void) 
247 {
248   JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
249   if (!canon)
250     return false;
251
252   HANDLE h = CreateFile (canon, 0, 0, NULL, CREATE_NEW, 
253                          FILE_ATTRIBUTE_NORMAL, NULL);
254   if (h != INVALID_HANDLE_VALUE)
255     {
256       CloseHandle (h);
257       return true;
258     }
259   else
260     {
261       if (GetLastError () == ERROR_ALREADY_EXISTS)
262         return false;
263       else
264         throw new IOException (JvNewStringLatin1 ("CreateFile failed"));
265     }
266 }
267
268 jboolean java::io::File::performSetReadOnly ()
269 {
270   JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
271   if (!canon)
272     return false;
273
274   DWORD attrs = GetFileAttributes (canon);
275   if (attrs != INVALID_FILE_ATTRIBUTES)
276     {
277       if (SetFileAttributes (canon, attrs | FILE_ATTRIBUTE_READONLY) != 0)
278         return true;
279       else
280         return false;
281     }
282   else
283     return false;
284 }
285
286 jboolean java::io::File::performSetLastModified (jlong time)
287 {
288   JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
289   if (!canon)
290     return false;
291
292   FILETIME modTime;
293   long long mTime100ns = ((long long) time        /* Ha! */
294                           + WIN32_EPOCH_MILLIS) * 10000LL;
295   
296   modTime.dwLowDateTime = (DWORD) mTime100ns;
297   modTime.dwHighDateTime = (DWORD) (mTime100ns >> 32);
298
299   jboolean retVal = false;
300   HANDLE h = CreateFile (canon, FILE_WRITE_ATTRIBUTES, 
301                          FILE_SHARE_READ | FILE_SHARE_WRITE, 
302                          NULL, OPEN_EXISTING, 0, NULL);
303
304   if (h != INVALID_HANDLE_VALUE)
305     {
306       if (SetFileTime (h, NULL, &modTime, &modTime) != 0)
307         retVal = true;
308
309       CloseHandle (h);
310     }
311
312   return retVal;
313 }
314
315 JArray<java::io::File*>* java::io::File::performListRoots ()
316 {
317   DWORD drivesBitmap = GetLogicalDrives ();
318   DWORD mask;
319
320   // Possible drive letters are from ASCII 'A'-'Z'.
321   int numDrives = 0;
322   mask = 1;
323   for (int i = 0; i < 26; i++)
324     {
325       if ((drivesBitmap & mask) != 0)
326         numDrives++;
327       mask <<= 1;
328     }
329
330   JArray<java::io::File *> *roots
331     = reinterpret_cast <JArray<java::io::File *>*> 
332         (JvNewObjectArray (numDrives, &java::io::File::class$, NULL));
333
334   ::java::io::File **rootsArray = elements (roots);
335
336   char aDriveRoot[] = {'A', ':', '\\', '\0'};
337   mask = 1;
338   for (int i = 0, j = 0; i < 26; i++)
339     {
340       if ((drivesBitmap & mask) != 0)
341         {
342           rootsArray[j] 
343             = new ::java::io::File (JvNewStringLatin1 (aDriveRoot));
344           j++;
345         }
346       mask <<= 1;
347       aDriveRoot[0]++;
348     }
349
350   return roots;
351 }