natFileWin32.cc 9.4 KB

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