2
Copyright (C) 2011 Jeroen Frijters
4
This software is provided 'as-is', without any express or implied
5
warranty. In no event will the authors be held liable for any damages
6
arising from the use of this software.
8
Permission is granted to anyone to use this software for any purpose,
9
including commercial applications, and to alter it and redistribute it
10
freely, subject to the following restrictions:
12
1. The origin of this software must not be misrepresented; you must not
13
claim that you wrote the original software. If you use this software
14
in a product, an acknowledgment in the product documentation would be
15
appreciated but is not required.
16
2. Altered source versions must be plainly marked as such, and must not be
17
misrepresented as being the original software.
18
3. This notice may not be removed or altered from any source distribution.
27
import ikvm.internal.NotYetImplementedError;
28
import static ikvm.internal.Util.WINDOWS;
29
import cli.System.IO.Directory;
30
import cli.System.IO.DirectoryInfo;
31
import cli.System.IO.DriveInfo;
32
import cli.System.IO.File;
33
import cli.System.IO.FileAttributes;
34
import cli.System.IO.FileInfo;
35
import cli.System.IO.FileMode;
36
import cli.System.IO.FileShare;
37
import cli.System.IO.FileStream;
38
import cli.System.IO.FileOptions;
39
import cli.System.Security.AccessControl.FileSystemRights;
40
import com.sun.nio.file.ExtendedOpenOption;
41
import java.io.FileDescriptor;
42
import java.io.IOException;
44
import java.nio.channels.*;
45
import java.nio.file.*;
46
import java.nio.file.attribute.*;
47
import java.nio.file.spi.FileSystemProvider;
48
import java.util.concurrent.ExecutorService;
49
import java.util.Iterator;
50
import java.util.HashMap;
52
import java.util.NoSuchElementException;
54
import sun.nio.ch.WindowsAsynchronousFileChannelImpl;
55
import sun.nio.ch.FileChannelImpl;
56
import sun.nio.ch.ThreadPool;
58
final class NetFileSystemProvider extends AbstractFileSystemProvider
60
private final NetFileSystem fs = new NetFileSystem(this);
61
private final HashMap<String, FileStore> stores = new HashMap<String, FileStore>();
63
final synchronized FileStore getFileStore(DriveInfo drive) throws IOException
65
String name = drive.get_Name().toLowerCase();
66
FileStore fs = stores.get(name);
69
fs = new NetFileStore(drive);
75
public String getScheme()
80
public FileSystem newFileSystem(URI uri, Map<String, ?> env) throws IOException
82
throw new FileSystemAlreadyExistsException();
85
public FileSystem getFileSystem(URI uri)
90
public Path getPath(URI uri)
94
return WindowsUriSupport.fromUri(fs, uri);
98
return UnixUriUtils.fromUri(fs, uri);
102
public AsynchronousFileChannel newAsynchronousFileChannel(Path path, Set<? extends OpenOption> opts, ExecutorService executor, FileAttribute<?>... attrs) throws IOException
104
NetPath npath = NetPath.from(path);
105
for (FileAttribute<?> attr : attrs)
109
throw new NotYetImplementedError();
111
int mode = FileMode.Open;
112
int share = FileShare.ReadWrite | FileShare.Delete;
113
int options = FileOptions.Asynchronous;
114
boolean read = false;
115
boolean write = false;
116
boolean truncate = false;
117
for (OpenOption opt : opts)
119
if (opt instanceof StandardOpenOption)
121
switch ((StandardOpenOption)opt)
124
mode = FileMode.Create;
127
mode = FileMode.CreateNew;
129
case DELETE_ON_CLOSE:
130
options |= FileOptions.DeleteOnClose;
133
options |= FileOptions.WriteThrough;
141
options |= FileOptions.WriteThrough;
143
case TRUNCATE_EXISTING:
150
throw new UnsupportedOperationException();
153
else if (opt instanceof ExtendedOpenOption)
155
switch ((ExtendedOpenOption)opt)
158
share &= ~FileShare.Read;
161
share &= ~FileShare.Write;
164
share &= ~FileShare.Delete;
167
throw new UnsupportedOperationException();
174
throw new UnsupportedOperationException();
185
if (mode == FileMode.Open)
187
mode = FileMode.Truncate;
194
rights |= FileSystemRights.Read;
198
rights |= FileSystemRights.Write;
202
if (executor == null)
208
pool = ThreadPool.wrap(executor, 0);
211
return WindowsAsynchronousFileChannelImpl.open(open(npath.path, mode, rights, share, options), read, write, pool);
214
public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> opts, FileAttribute<?>... attrs) throws IOException
216
return newFileChannel(path, opts, attrs);
219
public FileChannel newFileChannel(Path path, Set<? extends OpenOption> opts, FileAttribute<?>... attrs) throws IOException
221
NetPath npath = NetPath.from(path);
222
for (FileAttribute<?> attr : attrs)
226
throw new NotYetImplementedError();
228
int mode = FileMode.Open;
229
int share = FileShare.ReadWrite | FileShare.Delete;
230
int options = FileOptions.None;
231
boolean read = false;
232
boolean write = false;
233
boolean append = false;
234
boolean truncate = false;
235
for (OpenOption opt : opts)
237
if (opt instanceof StandardOpenOption)
239
switch ((StandardOpenOption)opt)
244
mode = FileMode.Append;
247
mode = FileMode.Create;
250
mode = FileMode.CreateNew;
252
case DELETE_ON_CLOSE:
253
options |= FileOptions.DeleteOnClose;
256
options |= FileOptions.WriteThrough;
264
options |= FileOptions.WriteThrough;
266
case TRUNCATE_EXISTING:
273
throw new UnsupportedOperationException();
276
else if (opt instanceof ExtendedOpenOption)
278
switch ((ExtendedOpenOption)opt)
281
share &= ~FileShare.Read;
284
share &= ~FileShare.Write;
287
share &= ~FileShare.Delete;
290
throw new UnsupportedOperationException();
297
throw new UnsupportedOperationException();
308
throw new IllegalArgumentException("READ + APPEND not allowed");
315
throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");
317
if (mode == FileMode.Open)
319
mode = FileMode.Truncate;
326
// for atomic append to work, we can't set FileSystemRights.Write
327
rights |= FileSystemRights.AppendData;
333
rights |= FileSystemRights.Read;
337
rights |= FileSystemRights.Write;
341
return FileChannelImpl.open(open(npath.path, mode, rights, share, options), read, write, append, null);
344
private static FileDescriptor open(String path, int mode, int rights, int share, int options) throws IOException
346
SecurityManager sm = System.getSecurityManager();
349
if ((rights & FileSystemRights.Read) != 0)
353
if ((rights & (FileSystemRights.Write | FileSystemRights.AppendData)) != 0)
357
if ((options & FileOptions.DeleteOnClose) != 0)
359
sm.checkDelete(path);
365
if (false) throw new cli.System.ArgumentException();
366
if (false) throw new cli.System.IO.FileNotFoundException();
367
if (false) throw new cli.System.IO.DirectoryNotFoundException();
368
if (false) throw new cli.System.PlatformNotSupportedException();
369
if (false) throw new cli.System.IO.IOException();
370
if (false) throw new cli.System.Security.SecurityException();
371
if (false) throw new cli.System.UnauthorizedAccessException();
372
return FileDescriptor.fromStream(new FileStream(path, FileMode.wrap(mode), FileSystemRights.wrap(rights), FileShare.wrap(share), 8, FileOptions.wrap(options)));
374
catch (cli.System.ArgumentException x)
376
throw new FileSystemException(path, null, x.getMessage());
378
catch (cli.System.IO.FileNotFoundException _)
380
throw new NoSuchFileException(path);
382
catch (cli.System.IO.DirectoryNotFoundException _)
384
throw new NoSuchFileException(path);
386
catch (cli.System.PlatformNotSupportedException x)
388
throw new UnsupportedOperationException(x.getMessage());
390
catch (cli.System.IO.IOException x)
392
if (mode == FileMode.CreateNew && File.Exists(path))
394
throw new FileAlreadyExistsException(path);
396
throw new FileSystemException(path, null, x.getMessage());
398
catch (cli.System.Security.SecurityException _)
400
throw new AccessDeniedException(path);
402
catch (cli.System.UnauthorizedAccessException _)
404
throw new AccessDeniedException(path);
408
public DirectoryStream<Path> newDirectoryStream(Path dir, final DirectoryStream.Filter<? super Path> filter) throws IOException
410
final String ndir = NetPath.from(dir).path;
414
SecurityManager sm = System.getSecurityManager();
422
if (false) throw new cli.System.ArgumentException();
423
if (false) throw new cli.System.IO.DirectoryNotFoundException();
424
if (false) throw new cli.System.IO.IOException();
425
if (false) throw new cli.System.Security.SecurityException();
426
if (false) throw new cli.System.UnauthorizedAccessException();
427
final String[] files = Directory.GetFileSystemEntries(ndir);
428
return new DirectoryStream<Path>() {
429
private boolean closed;
430
public Iterator<Path> iterator() {
432
throw new IllegalStateException();
435
return new Iterator<Path>() {
437
private Path filtered;
438
public boolean hasNext() {
439
if (filtered == null) {
440
while (pos != files.length) {
441
Path p = new NetPath(fs, cli.System.IO.Path.Combine(ndir, files[pos++]));
443
if (filter.accept(p)) {
447
} catch (IOException x) {
448
throw new DirectoryIteratorException(x);
452
return filtered != null;
456
throw new NoSuchElementException();
462
public void remove() {
463
throw new UnsupportedOperationException();
467
public void close() {
472
catch (cli.System.ArgumentException
473
| cli.System.IO.IOException
474
| cli.System.Security.SecurityException
475
| cli.System.UnauthorizedAccessException x)
477
if (File.Exists(ndir))
479
throw new NotDirectoryException(ndir);
481
throw new IOException(x.getMessage());
485
public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException
487
NetPath ndir = NetPath.from(dir);
488
for (FileAttribute<?> attr : attrs)
492
throw new NotYetImplementedError();
494
SecurityManager sm = System.getSecurityManager();
497
sm.checkWrite(ndir.path);
501
if (false) throw new cli.System.ArgumentException();
502
if (false) throw new cli.System.IO.IOException();
503
if (false) throw new cli.System.Security.SecurityException();
504
if (false) throw new cli.System.UnauthorizedAccessException();
505
Directory.CreateDirectory(ndir.path);
507
catch (cli.System.ArgumentException
508
| cli.System.IO.IOException
509
| cli.System.Security.SecurityException
510
| cli.System.UnauthorizedAccessException x)
512
if (File.Exists(ndir.path))
514
throw new FileAlreadyExistsException(ndir.path);
516
throw new IOException(x.getMessage());
520
public void copy(Path source, Path target, CopyOption... options) throws IOException
522
NetPath nsource = NetPath.from(source);
523
NetPath ntarget = NetPath.from(target);
524
boolean overwrite = false;
525
boolean copyAttribs = false;
526
for (CopyOption opt : options)
528
if (opt == StandardCopyOption.REPLACE_EXISTING)
532
else if (opt == StandardCopyOption.COPY_ATTRIBUTES)
540
throw new UnsupportedOperationException("Unsupported copy option");
543
SecurityManager sm = System.getSecurityManager();
546
sm.checkRead(nsource.path);
547
sm.checkWrite(ntarget.path);
551
if (false) throw new cli.System.ArgumentException();
552
if (false) throw new cli.System.IO.FileNotFoundException();
553
if (false) throw new cli.System.IO.DirectoryNotFoundException();
554
if (false) throw new cli.System.IO.IOException();
555
if (false) throw new cli.System.Security.SecurityException();
556
if (false) throw new cli.System.UnauthorizedAccessException();
557
if (File.Exists(ntarget.path))
561
throw new FileAlreadyExistsException(ntarget.path);
563
File.Delete(ntarget.path);
565
if (Directory.Exists(ntarget.path))
569
throw new FileAlreadyExistsException(ntarget.path);
573
if (false) throw new cli.System.IO.IOException();
574
Directory.Delete(ntarget.path);
576
catch (cli.System.IO.IOException _)
578
// HACK we assume that the IOException is caused by the directory not being empty
579
throw new DirectoryNotEmptyException(ntarget.path);
582
if (Directory.Exists(nsource.path))
584
Directory.CreateDirectory(ntarget.path);
588
File.Copy(nsource.path, ntarget.path, overwrite);
592
if (Directory.Exists(ntarget.path))
594
File.SetAttributes(ntarget.path, File.GetAttributes(nsource.path));
595
Directory.SetCreationTimeUtc(ntarget.path, File.GetCreationTimeUtc(nsource.path));
596
Directory.SetLastAccessTimeUtc(ntarget.path, File.GetLastAccessTimeUtc(nsource.path));
597
Directory.SetLastWriteTimeUtc(ntarget.path, File.GetLastWriteTimeUtc(nsource.path));
601
File.SetAttributes(ntarget.path, File.GetAttributes(nsource.path));
602
File.SetCreationTimeUtc(ntarget.path, File.GetCreationTimeUtc(nsource.path));
603
File.SetLastAccessTimeUtc(ntarget.path, File.GetLastAccessTimeUtc(nsource.path));
604
File.SetLastWriteTimeUtc(ntarget.path, File.GetLastWriteTimeUtc(nsource.path));
608
catch (cli.System.IO.FileNotFoundException x)
610
throw new NoSuchFileException(x.get_FileName());
612
catch (cli.System.IO.DirectoryNotFoundException x)
614
throw new NoSuchFileException(nsource.path, ntarget.path, x.getMessage());
616
catch (cli.System.IO.IOException | cli.System.ArgumentException x)
618
throw new FileSystemException(nsource.path, ntarget.path, x.getMessage());
620
catch (cli.System.Security.SecurityException | cli.System.UnauthorizedAccessException x)
622
throw new AccessDeniedException(nsource.path, ntarget.path, x.getMessage());
626
public void move(Path source, Path target, CopyOption... options) throws IOException
628
NetPath nsource = NetPath.from(source);
629
NetPath ntarget = NetPath.from(target);
630
boolean overwrite = false;
631
for (CopyOption opt : options)
633
if (opt == StandardCopyOption.REPLACE_EXISTING)
637
else if (opt == StandardCopyOption.ATOMIC_MOVE)
639
throw new AtomicMoveNotSupportedException(nsource.path, ntarget.path, "Unsupported copy option");
645
throw new UnsupportedOperationException("Unsupported copy option");
648
SecurityManager sm = System.getSecurityManager();
651
sm.checkRead(nsource.path);
652
sm.checkWrite(ntarget.path);
656
if (false) throw new cli.System.ArgumentException();
657
if (false) throw new cli.System.IO.FileNotFoundException();
658
if (false) throw new cli.System.IO.DirectoryNotFoundException();
659
if (false) throw new cli.System.IO.IOException();
660
if (false) throw new cli.System.Security.SecurityException();
661
if (false) throw new cli.System.UnauthorizedAccessException();
662
if (File.Exists(ntarget.path))
666
throw new FileAlreadyExistsException(ntarget.path);
668
File.Delete(ntarget.path);
670
if (Directory.Exists(ntarget.path))
674
throw new FileAlreadyExistsException(ntarget.path);
678
if (false) throw new cli.System.IO.IOException();
679
Directory.Delete(ntarget.path);
681
catch (cli.System.IO.IOException _)
683
// HACK we assume that the IOException is caused by the directory not being empty
684
throw new DirectoryNotEmptyException(ntarget.path);
687
if (Directory.Exists(nsource.path))
689
Directory.Move(nsource.path, ntarget.path);
693
File.Move(nsource.path, ntarget.path);
696
catch (cli.System.IO.FileNotFoundException x)
698
throw new NoSuchFileException(x.get_FileName());
700
catch (cli.System.IO.DirectoryNotFoundException x)
702
throw new NoSuchFileException(nsource.path, ntarget.path, x.getMessage());
704
catch (cli.System.IO.IOException | cli.System.ArgumentException x)
706
throw new FileSystemException(nsource.path, ntarget.path, x.getMessage());
708
catch (cli.System.Security.SecurityException | cli.System.UnauthorizedAccessException x)
710
throw new AccessDeniedException(nsource.path, ntarget.path, x.getMessage());
714
public boolean isSameFile(Path path, Path path2) throws IOException
716
if (path.equals(path2))
720
if (!(path instanceof NetPath && path2 instanceof NetPath))
726
return path.toRealPath().equals(path2.toRealPath());
729
public boolean isHidden(Path path) throws IOException
731
String npath = NetPath.from(path).path;
732
SecurityManager sm = System.getSecurityManager();
739
if (false) throw new cli.System.ArgumentException();
740
if (false) throw new cli.System.IO.FileNotFoundException();
741
if (false) throw new cli.System.IO.IOException();
742
return (File.GetAttributes(npath).Value & (cli.System.IO.FileAttributes.Hidden | cli.System.IO.FileAttributes.Directory)) == cli.System.IO.FileAttributes.Hidden;
744
catch (cli.System.IO.FileNotFoundException x)
746
throw new NoSuchFileException(npath);
748
catch (cli.System.IO.IOException | cli.System.ArgumentException x)
750
throw new IOException(x.getMessage());
754
private static class NetFileStore extends FileStore
756
private final DriveInfo info;
757
private final String name;
758
private final String type;
760
NetFileStore(DriveInfo info) throws IOException
765
if (false) throw new cli.System.IO.IOException();
766
name = info.get_VolumeLabel();
767
type = info.get_DriveFormat();
769
catch (cli.System.IO.IOException x)
771
throw new IOException(x.getMessage());
775
public Object getAttribute(String attribute) throws IOException
780
return getTotalSpace();
781
case "unallocatedSpace":
782
return getUnallocatedSpace();
784
return getUsableSpace();
786
throw new UnsupportedOperationException();
790
public <V extends FileStoreAttributeView> V getFileStoreAttributeView(Class<V> type)
795
public long getTotalSpace() throws IOException
799
if (false) throw new cli.System.IO.IOException();
800
return info.get_TotalSize();
802
catch (cli.System.IO.IOException x)
804
throw new IOException(x.getMessage());
808
public long getUnallocatedSpace() throws IOException
812
if (false) throw new cli.System.IO.IOException();
813
return info.get_TotalFreeSpace();
815
catch (cli.System.IO.IOException x)
817
throw new IOException(x.getMessage());
821
public long getUsableSpace() throws IOException
825
if (false) throw new cli.System.IO.IOException();
826
return info.get_AvailableFreeSpace();
828
catch (cli.System.IO.IOException x)
830
throw new IOException(x.getMessage());
834
public boolean isReadOnly()
844
public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type)
848
return type == BasicFileAttributeView.class || type == DosFileAttributeView.class;
851
public boolean supportsFileAttributeView(String name)
853
return name.equals("basic") || name.equals("dos");
861
public String toString()
863
return name + " (" + info.get_Name().charAt(0) + ":)";
867
public FileStore getFileStore(Path path) throws IOException
869
NetPath npath = NetPath.from(path.toAbsolutePath());
870
SecurityManager sm = System.getSecurityManager();
873
sm.checkRead(npath.path);
875
return getFileStore(new DriveInfo(npath.path));
878
public void checkAccess(Path path, AccessMode... modes) throws IOException
880
String npath = NetPath.from(path).path;
881
SecurityManager sm = System.getSecurityManager();
884
if (modes.length == 0)
888
for (AccessMode m : modes)
896
sm.checkWrite(npath);
902
throw new UnsupportedOperationException();
908
if (false) throw new cli.System.ArgumentException();
909
if (false) throw new cli.System.IO.DirectoryNotFoundException();
910
if (false) throw new cli.System.IO.FileNotFoundException();
911
if (false) throw new cli.System.IO.IOException();
912
if (false) throw new cli.System.NotSupportedException();
913
if (false) throw new cli.System.Security.SecurityException();
914
if (false) throw new cli.System.UnauthorizedAccessException();
915
// note that File.GetAttributes() works for directories as well
916
int attr = File.GetAttributes(npath).Value;
917
for (AccessMode m : modes)
925
if ((attr & (cli.System.IO.FileAttributes.ReadOnly | cli.System.IO.FileAttributes.Directory)) == cli.System.IO.FileAttributes.ReadOnly)
927
throw new AccessDeniedException(npath, null, "file has read-only attribute set");
929
if (getFileStore(path).isReadOnly())
931
throw new AccessDeniedException(npath, null, "volume is read-only");
935
throw new UnsupportedOperationException();
939
catch (cli.System.IO.FileNotFoundException | cli.System.IO.DirectoryNotFoundException _)
941
throw new NoSuchFileException(npath);
943
catch (cli.System.Security.SecurityException | cli.System.UnauthorizedAccessException x)
945
throw new AccessDeniedException(npath, null, x.getMessage());
947
catch (cli.System.ArgumentException | cli.System.IO.IOException | cli.System.NotSupportedException x)
949
throw new IOException(x.getMessage());
953
private static class BasicFileAttributesViewImpl extends AbstractBasicFileAttributeView
955
protected final String path;
957
BasicFileAttributesViewImpl(String path)
962
public BasicFileAttributes readAttributes() throws IOException
964
return DosFileAttributesViewImpl.readAttributesImpl(path);
967
public void setTimes(FileTime lastModifiedTime, FileTime lastAccessTime, FileTime createTime) throws IOException
969
SecurityManager sm = System.getSecurityManager();
976
if (false) throw new cli.System.ArgumentException();
977
if (false) throw new cli.System.IO.IOException();
978
if (false) throw new cli.System.NotSupportedException();
979
if (false) throw new cli.System.Security.SecurityException();
980
if (false) throw new cli.System.UnauthorizedAccessException();
981
if (File.Exists(path))
983
if (lastModifiedTime != null)
985
File.SetLastWriteTimeUtc(path, toDateTime(lastModifiedTime));
987
if (lastAccessTime != null)
989
File.SetLastAccessTimeUtc(path, toDateTime(lastAccessTime));
991
if (createTime != null)
993
File.SetCreationTimeUtc(path, toDateTime(createTime));
996
else if (Directory.Exists(path))
998
if (lastModifiedTime != null)
1000
Directory.SetLastWriteTimeUtc(path, toDateTime(lastModifiedTime));
1002
if (lastAccessTime != null)
1004
Directory.SetLastAccessTimeUtc(path, toDateTime(lastAccessTime));
1006
if (createTime != null)
1008
Directory.SetCreationTimeUtc(path, toDateTime(createTime));
1013
throw new NoSuchFileException(path);
1016
catch (cli.System.ArgumentException
1017
| cli.System.IO.IOException
1018
| cli.System.NotSupportedException
1019
| cli.System.Security.SecurityException
1020
| cli.System.UnauthorizedAccessException x)
1022
throw new IOException(x.getMessage());
1027
private static class DosFileAttributesViewImpl extends BasicFileAttributesViewImpl implements DosFileAttributeView
1029
private static final String READONLY_NAME = "readonly";
1030
private static final String ARCHIVE_NAME = "archive";
1031
private static final String SYSTEM_NAME = "system";
1032
private static final String HIDDEN_NAME = "hidden";
1033
private static final String ATTRIBUTES_NAME = "attributes";
1034
private static final Set<String> dosAttributeNames = Util.newSet(basicAttributeNames, READONLY_NAME, ARCHIVE_NAME, SYSTEM_NAME, HIDDEN_NAME, ATTRIBUTES_NAME);
1036
DosFileAttributesViewImpl(String path)
1041
public String name()
1046
public DosFileAttributes readAttributes() throws IOException
1048
return readAttributesImpl(path);
1051
private static class DosFileAttributesImpl implements DosFileAttributes
1053
private final FileInfo info;
1055
DosFileAttributesImpl(FileInfo info)
1062
return info.get_Attributes().Value;
1065
public FileTime creationTime()
1067
return toFileTime(info.get_CreationTimeUtc());
1070
public Object fileKey()
1075
public boolean isDirectory()
1077
return (info.get_Attributes().Value & cli.System.IO.FileAttributes.Directory) != 0;
1080
public boolean isOther()
1085
public boolean isRegularFile()
1087
return (info.get_Attributes().Value & cli.System.IO.FileAttributes.Directory) == 0;
1090
public boolean isSymbolicLink()
1095
public FileTime lastAccessTime()
1097
return toFileTime(info.get_LastAccessTimeUtc());
1100
public FileTime lastModifiedTime()
1102
return toFileTime(info.get_LastWriteTimeUtc());
1107
return info.get_Length();
1110
public boolean isArchive()
1112
return (info.get_Attributes().Value & cli.System.IO.FileAttributes.Archive) != 0;
1115
public boolean isHidden()
1117
return (info.get_Attributes().Value & cli.System.IO.FileAttributes.Hidden) != 0;
1120
public boolean isReadOnly()
1122
return (info.get_Attributes().Value & cli.System.IO.FileAttributes.ReadOnly) != 0;
1125
public boolean isSystem()
1127
return (info.get_Attributes().Value & cli.System.IO.FileAttributes.System) != 0;
1131
static DosFileAttributesImpl readAttributesImpl(String path) throws IOException
1133
SecurityManager sm = System.getSecurityManager();
1140
if (false) throw new cli.System.ArgumentException();
1141
if (false) throw new cli.System.IO.FileNotFoundException();
1142
if (false) throw new cli.System.IO.IOException();
1143
return new DosFileAttributesImpl(new FileInfo(path));
1145
catch (cli.System.IO.FileNotFoundException _)
1147
throw new NoSuchFileException(path);
1149
catch (cli.System.IO.IOException | cli.System.ArgumentException x)
1151
throw new IOException(x.getMessage());
1155
public void setArchive(boolean value) throws IOException
1157
setAttribute(cli.System.IO.FileAttributes.Archive, value);
1160
public void setHidden(boolean value) throws IOException
1162
setAttribute(cli.System.IO.FileAttributes.Hidden, value);
1165
public void setReadOnly(boolean value) throws IOException
1167
setAttribute(cli.System.IO.FileAttributes.ReadOnly, value);
1170
public void setSystem(boolean value) throws IOException
1172
setAttribute(cli.System.IO.FileAttributes.System, value);
1175
private void setAttribute(int attr, boolean value) throws IOException
1177
SecurityManager sm = System.getSecurityManager();
1180
sm.checkWrite(path);
1184
if (false) throw new cli.System.ArgumentException();
1185
if (false) throw new cli.System.IO.IOException();
1186
FileInfo info = new FileInfo(path);
1189
info.set_Attributes(cli.System.IO.FileAttributes.wrap(info.get_Attributes().Value | attr));
1193
info.set_Attributes(cli.System.IO.FileAttributes.wrap(info.get_Attributes().Value & ~attr));
1196
catch (cli.System.ArgumentException
1197
| cli.System.IO.IOException x)
1199
throw new IOException(x.getMessage());
1203
public Map<String,Object> readAttributes(String[] attributes) throws IOException
1205
AttributesBuilder builder = AttributesBuilder.create(dosAttributeNames, attributes);
1206
DosFileAttributesImpl attrs = readAttributesImpl(path);
1207
addRequestedBasicAttributes(attrs, builder);
1208
if (builder.match(READONLY_NAME))
1210
builder.add(READONLY_NAME, attrs.isReadOnly());
1212
if (builder.match(ARCHIVE_NAME))
1214
builder.add(ARCHIVE_NAME, attrs.isArchive());
1216
if (builder.match(SYSTEM_NAME))
1218
builder.add(SYSTEM_NAME, attrs.isSystem());
1220
if (builder.match(HIDDEN_NAME))
1222
builder.add(HIDDEN_NAME, attrs.isHidden());
1224
if (builder.match(ATTRIBUTES_NAME))
1226
builder.add(ATTRIBUTES_NAME, attrs.attributes());
1228
return builder.unmodifiableMap();
1231
public void setAttribute(String attribute, Object value) throws IOException
1236
setReadOnly((Boolean)value);
1239
setArchive((Boolean)value);
1242
setSystem((Boolean)value);
1245
setHidden((Boolean)value);
1248
super.setAttribute(attribute, value);
1254
private static void validateLinkOption(LinkOption... options)
1256
for (LinkOption option : options)
1258
if (option == LinkOption.NOFOLLOW_LINKS)
1266
throw new UnsupportedOperationException();
1271
public <V extends FileAttributeView> V getFileAttributeView(Path path, Class<V> type, LinkOption... options)
1273
String npath = NetPath.from(path).path;
1274
validateLinkOption(options);
1275
if (type == BasicFileAttributeView.class)
1277
return (V)new BasicFileAttributesViewImpl(npath);
1279
else if (type == DosFileAttributeView.class)
1281
return (V)new DosFileAttributesViewImpl(npath);
1291
public <A extends BasicFileAttributes> A readAttributes(Path path, Class<A> type, LinkOption... options) throws IOException
1293
String npath = NetPath.from(path).path;
1296
validateLinkOption(options);
1297
if (type != BasicFileAttributes.class && type != DosFileAttributes.class)
1299
throw new UnsupportedOperationException();
1301
SecurityManager sm = System.getSecurityManager();
1304
sm.checkRead(npath);
1306
return (A)DosFileAttributesViewImpl.readAttributesImpl(npath);
1309
DynamicFileAttributeView getFileAttributeView(Path file, String name, LinkOption... options)
1311
validateLinkOption(options);
1312
if (name.equals("basic"))
1314
return new BasicFileAttributesViewImpl(NetPath.from(file).path);
1316
else if (name.equals("dos"))
1318
return new DosFileAttributesViewImpl(NetPath.from(file).path);
1326
boolean implDelete(Path file, boolean failIfNotExists) throws IOException
1328
String path = NetPath.from(file).path;
1329
SecurityManager sm = System.getSecurityManager();
1332
sm.checkDelete(path);
1336
if (false) throw new cli.System.ArgumentException();
1337
if (false) throw new cli.System.IO.FileNotFoundException();
1338
if (false) throw new cli.System.IO.DirectoryNotFoundException();
1339
if (false) throw new cli.System.IO.IOException();
1340
if (false) throw new cli.System.Security.SecurityException();
1341
if (false) throw new cli.System.UnauthorizedAccessException();
1342
int attr = cli.System.IO.File.GetAttributes(path).Value;
1343
if ((attr & cli.System.IO.FileAttributes.Directory) != 0)
1347
if (false) throw new cli.System.IO.IOException();
1348
cli.System.IO.Directory.Delete(path);
1350
catch (cli.System.IO.IOException _)
1352
// HACK we assume that the IOException is caused by the directory not being empty
1353
throw new DirectoryNotEmptyException(path);
1359
cli.System.IO.File.Delete(path);
1363
catch (cli.System.ArgumentException x)
1365
throw new FileSystemException(path, null, x.getMessage());
1367
catch (cli.System.IO.FileNotFoundException _)
1369
if (failIfNotExists)
1371
throw new NoSuchFileException(path);
1378
catch (cli.System.IO.DirectoryNotFoundException _)
1380
if (failIfNotExists)
1382
throw new NoSuchFileException(path);
1389
catch (cli.System.IO.IOException x)
1391
throw new FileSystemException(path, null, x.getMessage());
1393
catch (cli.System.Security.SecurityException _)
1395
throw new AccessDeniedException(path);
1397
catch (cli.System.UnauthorizedAccessException _)
1399
throw new AccessDeniedException(path);
1403
static FileTime toFileTime(cli.System.DateTime dateTime)
1405
return FileTime.from((dateTime.get_Ticks() - 621355968000000000L) / 10, java.util.concurrent.TimeUnit.MICROSECONDS);
1408
static cli.System.DateTime toDateTime(FileTime fileTime)
1410
return new cli.System.DateTime(fileTime.to(java.util.concurrent.TimeUnit.MICROSECONDS) * 10 + 621355968000000000L);