Mercurial > hg4j
annotate src/org/tmatesoft/hg/util/RegularFileInfo.java @ 490:b3c16d1aede0
Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> | 
|---|---|
| date | Thu, 16 Aug 2012 17:08:34 +0200 | 
| parents | c02b5710d9ac | 
| children | 868b2ffdcd5c | 
| rev | line source | 
|---|---|
| 287 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 1 /* | 
| 413 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 2 * Copyright (c) 2011-2012 TMate Software Ltd | 
| 287 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 3 * | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 4 * This program is free software; you can redistribute it and/or modify | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 5 * it under the terms of the GNU General Public License as published by | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 6 * the Free Software Foundation; version 2 of the License. | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 7 * | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 8 * This program is distributed in the hope that it will be useful, | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 11 * GNU General Public License for more details. | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 12 * | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 13 * For information on how to redistribute this software under | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 14 * the terms of a license other than GNU General Public License | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 15 * contact TMate Software at support@hg4j.com | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 16 */ | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 17 package org.tmatesoft.hg.util; | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 18 | 
| 456 
909306e412e2
Refactor LogFacility and SessionContext, better API for both
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
425diff
changeset | 19 import static org.tmatesoft.hg.util.LogFacility.Severity.Info; | 
| 
909306e412e2
Refactor LogFacility and SessionContext, better API for both
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
425diff
changeset | 20 | 
| 287 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 21 import java.io.File; | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 22 import java.io.FileInputStream; | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 23 import java.io.FileNotFoundException; | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 24 import java.io.IOException; | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 25 import java.nio.ByteBuffer; | 
| 413 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 26 import java.nio.channels.ClosedChannelException; | 
| 287 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 27 import java.nio.channels.ReadableByteChannel; | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 28 | 
| 425 
48f993aa2f41
FIXMEs: exceptions, javadoc
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
413diff
changeset | 29 import org.tmatesoft.hg.core.SessionContext; | 
| 295 
981f9f50bb6c
Issue 11: Error log facility. SessionContext to share common facilities
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
287diff
changeset | 30 import org.tmatesoft.hg.internal.StreamLogFacility; | 
| 
981f9f50bb6c
Issue 11: Error log facility. SessionContext to share common facilities
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
287diff
changeset | 31 | 
| 287 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 32 /** | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 33 * | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 34 * @author Artem Tikhomirov | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 35 * @author TMate Software Ltd. | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 36 */ | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 37 public class RegularFileInfo implements FileInfo { | 
| 413 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 38 private final boolean supportsExec, supportsLink; | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 39 private final RegularFileStats fileFlagsHelper; // null if both supportsLink and supportExec are false | 
| 287 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 40 private File file; | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 41 | 
| 425 
48f993aa2f41
FIXMEs: exceptions, javadoc
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
413diff
changeset | 42 public RegularFileInfo(SessionContext ctx) { | 
| 
48f993aa2f41
FIXMEs: exceptions, javadoc
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
413diff
changeset | 43 this(ctx, false, false); | 
| 413 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 44 } | 
| 425 
48f993aa2f41
FIXMEs: exceptions, javadoc
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
413diff
changeset | 45 public RegularFileInfo(SessionContext ctx, boolean supportExecFlag, boolean supportSymlink) { | 
| 413 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 46 supportsLink = supportSymlink; | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 47 supportsExec = supportExecFlag; | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 48 if (supportSymlink || supportExecFlag) { | 
| 425 
48f993aa2f41
FIXMEs: exceptions, javadoc
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
413diff
changeset | 49 fileFlagsHelper = new RegularFileStats(ctx); | 
| 413 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 50 } else { | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 51 fileFlagsHelper = null; | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 52 } | 
| 287 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 53 } | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 54 | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 55 public void init(File f) { | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 56 file = f; | 
| 413 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 57 if (fileFlagsHelper != null) { | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 58 fileFlagsHelper.init(file); | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 59 } | 
| 287 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 60 } | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 61 | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 62 public boolean exists() { | 
| 413 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 63 // java.io.File for symlinks without proper target says it doesn't exist. | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 64 // since we found this symlink in directory listing, it's safe to say it exists just based on the fact it's link | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 65 return isSymlink() || (file.canRead() && file.isFile()); | 
| 287 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 66 } | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 67 | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 68 public int lastModified() { | 
| 413 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 69 // TODO post-1.0 for symlinks, this returns incorrect mtime of the target file, not that of link itself | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 70 // Besides, timestame if link points to non-existing file is 0. | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 71 // However, it result only in slowdown in WCStatusCollector, as it need to perform additional content check | 
| 287 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 72 return (int) (file.lastModified() / 1000); | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 73 } | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 74 | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 75 public long length() { | 
| 413 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 76 if (isSymlink()) { | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 77 return getLinkTargetBytes().length; | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 78 } | 
| 287 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 79 return file.length(); | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 80 } | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 81 | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 82 public ReadableByteChannel newInputChannel() { | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 83 try { | 
| 413 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 84 if (isSymlink()) { | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 85 return new ByteArrayReadableChannel(getLinkTargetBytes()); | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 86 } else { | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 87 return new FileInputStream(file).getChannel(); | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 88 } | 
| 287 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 89 } catch (FileNotFoundException ex) { | 
| 456 
909306e412e2
Refactor LogFacility and SessionContext, better API for both
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
425diff
changeset | 90 StreamLogFacility.newDefault().dump(getClass(), Info, ex, null); | 
| 287 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 91 // shall not happen, provided this class is used correctly | 
| 413 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 92 return new ByteArrayReadableChannel(null); | 
| 287 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 93 } | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 94 } | 
| 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 95 | 
| 413 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 96 public boolean isExecutable() { | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 97 return supportsExec && fileFlagsHelper.isExecutable(); | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 98 } | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 99 | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 100 public boolean isSymlink() { | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 101 return supportsLink && fileFlagsHelper.isSymlink(); | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 102 } | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 103 | 
| 476 
c02b5710d9ac
Defect: only first file in directory got flags initialized
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
456diff
changeset | 104 @Override | 
| 
c02b5710d9ac
Defect: only first file in directory got flags initialized
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
456diff
changeset | 105 public String toString() { | 
| 
c02b5710d9ac
Defect: only first file in directory got flags initialized
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
456diff
changeset | 106 char t = exists() ? (isExecutable() ? '*' : (isSymlink() ? '@' : '-')) : '!'; | 
| 
c02b5710d9ac
Defect: only first file in directory got flags initialized
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
456diff
changeset | 107 return String.format("RegularFileInfo[%s %c]", file.getPath(), t); | 
| 
c02b5710d9ac
Defect: only first file in directory got flags initialized
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
456diff
changeset | 108 } | 
| 
c02b5710d9ac
Defect: only first file in directory got flags initialized
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
456diff
changeset | 109 | 
| 413 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 110 private byte[] getLinkTargetBytes() { | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 111 assert isSymlink(); | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 112 // no idea what encoding Mercurial uses for link targets, assume platform native is ok | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 113 return fileFlagsHelper.getSymlinkTarget().getBytes(); | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 114 } | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 115 | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 116 | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 117 private static class ByteArrayReadableChannel implements ReadableByteChannel { | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 118 private final byte[] data; | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 119 private boolean closed = false; // initially open | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 120 private int firstAvailIndex = 0; | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 121 | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 122 ByteArrayReadableChannel(byte[] dataToStream) { | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 123 data = dataToStream; | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 124 } | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 125 | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 126 public boolean isOpen() { | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 127 return !closed; | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 128 } | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 129 | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 130 public void close() throws IOException { | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 131 closed = true; | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 132 } | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 133 | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 134 public int read(ByteBuffer dst) throws IOException { | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 135 if (closed) { | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 136 throw new ClosedChannelException(); | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 137 } | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 138 int remainingBytes = data.length - firstAvailIndex; | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 139 if (data == null || remainingBytes == 0) { | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 140 // EOF right away | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 141 return -1; | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 142 } | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 143 int x = Math.min(dst.remaining(), remainingBytes); | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 144 for (int i = firstAvailIndex, lim = firstAvailIndex + x; i < lim; i++) { | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 145 dst.put(data[i]); | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 146 } | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 147 firstAvailIndex += x; | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 148 return x; | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 149 } | 
| 
7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: 
295diff
changeset | 150 } | 
| 287 
ed6b74a58c66
Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
 Artem Tikhomirov <tikhomirov.artem@gmail.com> parents: diff
changeset | 151 } | 
