source: trunk/src/gcc/libjava/java/io/natFileWin32.cc@ 1213

Last change on this file since 1213 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 6.8 KB
Line 
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
7This software is copyrighted work licensed under the terms of the
8Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9details. */
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
29jboolean
30java::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
52jboolean
53java::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
74jlong
75java::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
99jstring
100java::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
116jboolean
117java::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
132void java::io::File::init_native() { }
133
134
135jobjectArray
136java::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