OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [java/] [io/] [natFileWin32.cc] - Blame information for rev 775

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 758 jeremybenn
// 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
            || query == EXEC);
49
 
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;
63
}
64
 
65
jboolean
66
java::io::File::_stat (jint query)
67
{
68
  JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
69
  if (!canon)
70
    return false;
71
 
72
  JvAssert (query == DIRECTORY || query == ISFILE);
73
 
74
  DWORD attributes = GetFileAttributes (canon);
75
  if (attributes == 0xffffffff)
76
    return false;
77
 
78
  if (query == DIRECTORY)
79
    return attributes & FILE_ATTRIBUTE_DIRECTORY ? true : false;
80
  else
81
    return attributes & FILE_ATTRIBUTE_DIRECTORY ? false : true;
82
}
83
 
84
jlong
85
java::io::File::attr (jint query)
86
{
87
  JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
88
  if (!canon)
89
    return false;
90
 
91
  JvAssert (query == MODIFIED || query == LENGTH);
92
 
93
  WIN32_FIND_DATA info;
94
  HANDLE sHandle;
95
  if ( ( sHandle = FindFirstFile( canon, &info)) == INVALID_HANDLE_VALUE)
96
    return 0;
97
 
98
  FindClose( sHandle);
99
 
100
  if (query == LENGTH)
101
    return ((long long)info.nFileSizeHigh) << 32
102
           | (unsigned long long)info.nFileSizeLow;
103
  else
104
    {
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;
110
    }
111
}
112
 
113
jstring
114
java::io::File::getCanonicalPath (void)
115
{
116
  JV_TEMP_STRING_WIN32 (cpath, path);
117
 
118
  // If the filename is blank, use the current directory.
119
  LPCTSTR thepath = cpath.buf();
120
  if (*thepath == 0)
121
    thepath = _T(".");
122
 
123
  LPTSTR unused;
124
  TCHAR buf2[MAX_PATH];
125
  if(!GetFullPathName(thepath, MAX_PATH, buf2, &unused))
126
    throw new IOException (JvNewStringLatin1 ("GetFullPathName failed"));
127
 
128
  return _Jv_Win32NewString (buf2);
129
}
130
 
131
jboolean
132
java::io::File::isAbsolute (void)
133
{
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;
138
 
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.
141
 
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) == '\\'));
151
}
152
 
153
void java::io::File::init_native ()
154
{
155
  maxPathLen = MAX_PATH;
156
  caseSensitive = false;
157
}
158
 
159
jobjectArray
160
java::io::File::performList (java::io::FilenameFilter *filter,
161
           java::io::FileFilter *fileFilter,
162
           java::lang::Class *clazz)
163
{
164
  jstring canon = getCanonicalPath();
165
  if (! canon)
166
    return NULL;
167
 
168
  int len = canon->length();
169
  TCHAR buf[len + 5];
170
 
171
  JV_TEMP_STRING_WIN32(canonstr, canon);
172
 
173
  _tcscpy(buf, canonstr);
174
  if (buf[len - 1] == _T('\\'))
175
    _tcscpy (&buf[len], _T("*.*"));
176
  else
177
    _tcscpy (&buf[len], _T("\\*.*"));
178
 
179
  WIN32_FIND_DATA data;
180
  HANDLE handle = FindFirstFile (buf, &data);
181
  if (handle == INVALID_HANDLE_VALUE)
182
    return NULL;
183
 
184
  java::util::Vector *vec = new java::util::Vector ();
185
 
186
  do
187
    {
188
      if (_tcscmp (data.cFileName, _T(".")) &&
189
        _tcscmp (data.cFileName, _T("..")))
190
        {
191
          jstring name = _Jv_Win32NewString (data.cFileName);
192
 
193
          if (filter && !filter->accept(this, name))
194
            continue;
195
          if (clazz == &java::io::File::class$)
196
            {
197
              java::io::File *file = new java::io::File (this, name);
198
              if (fileFilter && !fileFilter->accept(file))
199
                continue;
200
              vec->addElement (file);
201
            }
202
          else
203
            vec->addElement (name);
204
        }
205
    }
206
  while (FindNextFile (handle, &data));
207
 
208
  if (GetLastError () != ERROR_NO_MORE_FILES)
209
    return NULL;
210
 
211
  FindClose (handle);
212
 
213
  jobjectArray ret = JvNewObjectArray (vec->size(), clazz, NULL);
214
  vec->copyInto (ret);
215
  return ret;
216
}
217
 
218
jboolean
219
java::io::File::setFilePermissions (jboolean enable,
220
                                    jboolean ownerOnly,
221
                                    jint permissions)
222
{
223
  JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
224
  if (!canon)
225
    return false;
226
 
227
  DWORD attrs = GetFileAttributes (canon);
228
  if (attrs != INVALID_FILE_ATTRIBUTES)
229
    {
230
      // FIXME: implement
231
      return false;
232
    }
233
  else
234
    return false;
235
}
236
 
237
jboolean
238
java::io::File::performMkdir (void)
239
{
240
  JV_TEMP_STRING_WIN32 (cpath, path);
241
  return (CreateDirectory(cpath, NULL)) ? true : false;
242
}
243
 
244
jboolean
245
java::io::File::performRenameTo (File *dest)
246
{
247
  JV_TEMP_STRING_WIN32 (pathFrom, path);
248
  JV_TEMP_STRING_WIN32 (pathTo, dest->path);
249
  return (MoveFile(pathFrom, pathTo)) ? true : false;
250
}
251
 
252
jboolean
253
java::io::File::performDelete ()
254
{
255
  JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
256
  if (!canon)
257
    return false;
258
 
259
  DWORD attributes = GetFileAttributes (canon);
260
  if (attributes == 0xffffffff)
261
    return false;
262
 
263
  if (attributes & FILE_ATTRIBUTE_DIRECTORY)
264
    return (RemoveDirectory (canon)) ? true : false;
265
  else
266
    return (DeleteFile (canon)) ? true : false;
267
}
268
 
269
jboolean java::io::File::performCreate (void)
270
{
271
  JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
272
  if (!canon)
273
    return false;
274
 
275
  HANDLE h = CreateFile (canon, 0, 0, NULL, CREATE_NEW,
276
                         FILE_ATTRIBUTE_NORMAL, NULL);
277
  if (h != INVALID_HANDLE_VALUE)
278
    {
279
      CloseHandle (h);
280
      return true;
281
    }
282
  else
283
    {
284
      if (GetLastError () == ERROR_ALREADY_EXISTS)
285
        return false;
286
      else
287
        throw new IOException (JvNewStringLatin1 ("CreateFile failed"));
288
    }
289
}
290
 
291
jboolean java::io::File::performSetReadOnly ()
292
{
293
  JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
294
  if (!canon)
295
    return false;
296
 
297
  DWORD attrs = GetFileAttributes (canon);
298
  if (attrs != INVALID_FILE_ATTRIBUTES)
299
    {
300
      if (SetFileAttributes (canon, attrs | FILE_ATTRIBUTE_READONLY) != 0)
301
        return true;
302
      else
303
        return false;
304
    }
305
  else
306
    return false;
307
}
308
 
309
jboolean java::io::File::performSetLastModified (jlong time)
310
{
311
  JV_TEMP_STRING_WIN32 (canon, getCanonicalPath());
312
  if (!canon)
313
    return false;
314
 
315
  FILETIME modTime;
316
  long long mTime100ns = ((long long) time        /* Ha! */
317
                          + WIN32_EPOCH_MILLIS) * 10000LL;
318
 
319
  modTime.dwLowDateTime = (DWORD) mTime100ns;
320
  modTime.dwHighDateTime = (DWORD) (mTime100ns >> 32);
321
 
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);
326
 
327
  if (h != INVALID_HANDLE_VALUE)
328
    {
329
      if (SetFileTime (h, NULL, &modTime, &modTime) != 0)
330
        retVal = true;
331
 
332
      CloseHandle (h);
333
    }
334
 
335
  return retVal;
336
}
337
 
338
JArray<java::io::File*>* java::io::File::performListRoots ()
339
{
340
  DWORD drivesBitmap = GetLogicalDrives ();
341
  DWORD mask;
342
 
343
  // Possible drive letters are from ASCII 'A'-'Z'.
344
  int numDrives = 0;
345
  mask = 1;
346
  for (int i = 0; i < 26; i++)
347
    {
348
      if ((drivesBitmap & mask) != 0)
349
        numDrives++;
350
      mask <<= 1;
351
    }
352
 
353
  JArray<java::io::File *> *roots
354
    = reinterpret_cast <JArray<java::io::File *>*>
355
        (JvNewObjectArray (numDrives, &java::io::File::class$, NULL));
356
 
357
  ::java::io::File **rootsArray = elements (roots);
358
 
359
  char aDriveRoot[] = {'A', ':', '\\', '\0'};
360
  mask = 1;
361
  for (int i = 0, j = 0; i < 26; i++)
362
    {
363
      if ((drivesBitmap & mask) != 0)
364
        {
365
          rootsArray[j]
366
            = new ::java::io::File (JvNewStringLatin1 (aDriveRoot));
367
          j++;
368
        }
369
      mask <<= 1;
370
      aDriveRoot[0]++;
371
    }
372
 
373
  return roots;
374
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.