Java Code Examples for java.nio.file.Files.getAttribute()

The following are Jave code examples for showing how to use getAttribute() of the java.nio.file.Files class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: uavstack   File: ReliableTaildirEventReader.java   View Source Code Vote up 7 votes
private long getInode(File file) throws IOException {

        UserDefinedFileAttributeView view = null;
        // windows system and file customer Attribute
        if (OS_WINDOWS.equals(os)) {
            view = Files.getFileAttributeView(file.toPath(), UserDefinedFileAttributeView.class);// 把文件的内容属性值放置在view里面?
            try {
                ByteBuffer buffer = ByteBuffer.allocate(view.size(INODE));// view.size得到inode属性值大小
                view.read(INODE, buffer);// 把属性值放置在buffer中
                buffer.flip();
                return Long.parseLong(Charset.defaultCharset().decode(buffer).toString());// 返回编码后的inode的属性值

            }
            catch (NoSuchFileException e) {
                long winode = random.nextLong();
                view.write(INODE, Charset.defaultCharset().encode(String.valueOf(winode)));
                return winode;
            }
        }
        long inode = (long) Files.getAttribute(file.toPath(), "unix:ino");// 返回unix的inode的属性值
        return inode;
    }
 
Example 2
Project: fuse-nio-adapter   File: MacMirroringTest.java   View Source Code Vote up 6 votes
public static void main(String[] args) throws IOException {
	try (Scanner scanner = new Scanner(System.in)) {
		System.out.println("Enter path to the directory you want to mirror:");
		Path p = Paths.get(scanner.nextLine());
		int uid = (int) Files.getAttribute(p, "unix:uid");
		int gid = (int) Files.getAttribute(p, "unix:gid");
		System.out.println("Enter mount point:");
		Path m = Paths.get(scanner.nextLine());
		if (Files.isDirectory(p) && Files.isDirectory(m)) {
			try (FuseNioAdapter fs = AdapterFactory.createReadWriteAdapter(p)) {
				fs.mount(m, false, false, new String[]{"-ouid="+uid, "-ogid="+gid, "-ovolname=FUSE-NIO-Adapter", "-oauto_xattr", "-oatomic_o_trunc"});
				System.out.println("Mounted successfully. Enter anything to stop the server...");
				System.in.read();
				fs.umount();
				System.out.println("Unmounted successfully. Exiting...");
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			System.err.println("Invalid directory.");
		}
	}
}
 
Example 3
Project: fuse-nio-adapter   File: LinuxMirroringTest.java   View Source Code Vote up 6 votes
public static void main(String[] args) throws IOException {
	try (Scanner scanner = new Scanner(System.in)) {
		System.out.println("Enter path to the directory you want to mirror:");
		Path p = Paths.get(scanner.nextLine());
		int uid = (int) Files.getAttribute(p, "unix:uid");
		int gid = (int) Files.getAttribute(p, "unix:gid");
		System.out.println("Enter mount point:");
		Path m = Paths.get(scanner.nextLine());
		if (Files.isDirectory(p) && Files.isDirectory(m)) {
			try (FuseNioAdapter fs = AdapterFactory.createReadWriteAdapter(p)) {
				fs.mount(m, false, false, new String[]{"-ouid="+uid, "-ogid="+gid, "-oatomic_o_trunc"});
				System.out.println("Mounted successfully. Enter anything to stop the server...");
				System.in.read();
				fs.umount();
				System.out.println("Unmounted successfully. Exiting...");
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			System.err.println("Invalid directory.");
		}
	}
}
 
Example 4
Project: ats-framework   File: LocalFileSystemOperations.java   View Source Code Vote up 5 votes
/**
 *
 * @param filename the file name
 * @return the file user id
 * @throws FileSystemOperationException
 */
private long getUserId(
                        String filename ) {

    try {
        Integer uid = ( Integer ) Files.getAttribute( new File( filename ).toPath(), "unix:uid",
                                                      LinkOption.NOFOLLOW_LINKS );
        return uid.longValue();

    } catch( Exception e ) {
        throw new FileSystemOperationException( "Could not get UID for '" + filename + "'", e );
    }
}
 
Example 5
Project: ats-framework   File: LocalFileSystemOperations.java   View Source Code Vote up 5 votes
/**
 *
 * @param filename file name
 * @return the file group id
 * @throws FileSystemOperationException
 */
private long getGroupId(
                         String filename ) {

    try {
        Integer gid = ( Integer ) Files.getAttribute( new File( filename ).toPath(), "unix:gid",
                                                      LinkOption.NOFOLLOW_LINKS );
        return gid.longValue();

    } catch( Exception e ) {
        throw new FileSystemOperationException( "Could not get GID for '" + filename + "'", e );
    }
}
 
Example 6
Project: flume-release-1.7.0   File: ReliableTaildirEventReader.java   View Source Code Vote up 4 votes
private long getInode(File file) throws IOException {
  long inode = (long) Files.getAttribute(file.toPath(), "unix:ino");
  return inode;
}
 
Example 7
Project: OpenJSharp   File: DflCache.java   View Source Code Vote up 4 votes
private int loadAndCheck(Path p, AuthTimeWithHash time,
        KerberosTime currTime)
        throws IOException, KrbApErrException {
    int missed = 0;
    if (Files.isSymbolicLink(p)) {
        throw new IOException("Symlink not accepted");
    }
    try {
        Set<PosixFilePermission> perms =
                Files.getPosixFilePermissions(p);
        if (uid != -1 &&
                (Integer)Files.getAttribute(p, "unix:uid") != uid) {
            throw new IOException("Not mine");
        }
        if (perms.contains(PosixFilePermission.GROUP_READ) ||
                perms.contains(PosixFilePermission.GROUP_WRITE) ||
                perms.contains(PosixFilePermission.GROUP_EXECUTE) ||
                perms.contains(PosixFilePermission.OTHERS_READ) ||
                perms.contains(PosixFilePermission.OTHERS_WRITE) ||
                perms.contains(PosixFilePermission.OTHERS_EXECUTE)) {
            throw new IOException("Accessible by someone else");
        }
    } catch (UnsupportedOperationException uoe) {
        // No POSIX permissions? Ignore it.
    }
    chan = Files.newByteChannel(p, StandardOpenOption.WRITE,
            StandardOpenOption.READ);

    long timeLimit = currTime.getSeconds() - readHeader(chan);

    long pos = 0;
    boolean seeNewButNotSame = false;
    while (true) {
        try {
            pos = chan.position();
            AuthTime a = AuthTime.readFrom(chan);
            if (a instanceof AuthTimeWithHash) {
                if (time.equals(a)) {
                    // Exact match, must be a replay
                    throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
                } else if (time.isSameIgnoresHash(a)) {
                    // Two different authenticators in the same second.
                    // Remember it
                    seeNewButNotSame = true;
                }
            } else {
                if (time.isSameIgnoresHash(a)) {
                    // Two authenticators in the same second. Considered
                    // same if we haven't seen a new style version of it
                    if (!seeNewButNotSame) {
                        throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
                    }
                }
            }
            if (a.ctime < timeLimit) {
                missed++;
            } else {
                missed--;
            }
        } catch (BufferUnderflowException e) {
            // Half-written file?
            chan.position(pos);
            break;
        }
    }
    return missed;
}
 
Example 8
Project: jdk8u-jdk   File: DflCache.java   View Source Code Vote up 4 votes
private int loadAndCheck(Path p, AuthTimeWithHash time,
        KerberosTime currTime)
        throws IOException, KrbApErrException {
    int missed = 0;
    if (Files.isSymbolicLink(p)) {
        throw new IOException("Symlink not accepted");
    }
    try {
        Set<PosixFilePermission> perms =
                Files.getPosixFilePermissions(p);
        if (uid != -1 &&
                (Integer)Files.getAttribute(p, "unix:uid") != uid) {
            throw new IOException("Not mine");
        }
        if (perms.contains(PosixFilePermission.GROUP_READ) ||
                perms.contains(PosixFilePermission.GROUP_WRITE) ||
                perms.contains(PosixFilePermission.GROUP_EXECUTE) ||
                perms.contains(PosixFilePermission.OTHERS_READ) ||
                perms.contains(PosixFilePermission.OTHERS_WRITE) ||
                perms.contains(PosixFilePermission.OTHERS_EXECUTE)) {
            throw new IOException("Accessible by someone else");
        }
    } catch (UnsupportedOperationException uoe) {
        // No POSIX permissions? Ignore it.
    }
    chan = Files.newByteChannel(p, StandardOpenOption.WRITE,
            StandardOpenOption.READ);

    long timeLimit = currTime.getSeconds() - readHeader(chan);

    long pos = 0;
    boolean seeNewButNotSame = false;
    while (true) {
        try {
            pos = chan.position();
            AuthTime a = AuthTime.readFrom(chan);
            if (a instanceof AuthTimeWithHash) {
                if (time.equals(a)) {
                    // Exact match, must be a replay
                    throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
                } else if (time.isSameIgnoresHash(a)) {
                    // Two different authenticators in the same second.
                    // Remember it
                    seeNewButNotSame = true;
                }
            } else {
                if (time.isSameIgnoresHash(a)) {
                    // Two authenticators in the same second. Considered
                    // same if we haven't seen a new style version of it
                    if (!seeNewButNotSame) {
                        throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
                    }
                }
            }
            if (a.ctime < timeLimit) {
                missed++;
            } else {
                missed--;
            }
        } catch (BufferUnderflowException e) {
            // Half-written file?
            chan.position(pos);
            break;
        }
    }
    return missed;
}
 
Example 9
Project: uavstack   File: DoTestRuleFilterFactory.java   View Source Code Vote up 4 votes
@Test
public void getFileAttributes() throws IOException {

    Object obj = Files.getAttribute(new File("/Users/fathead/temp/file3").toPath(), "unix:ino");
    System.out.println(obj);
}
 
Example 10
Project: openjdk-jdk10   File: DflCache.java   View Source Code Vote up 4 votes
private int loadAndCheck(Path p, AuthTimeWithHash time,
        KerberosTime currTime)
        throws IOException, KrbApErrException {
    int missed = 0;
    if (Files.isSymbolicLink(p)) {
        throw new IOException("Symlink not accepted");
    }
    try {
        Set<PosixFilePermission> perms =
                Files.getPosixFilePermissions(p);
        if (uid != -1 &&
                (Integer)Files.getAttribute(p, "unix:uid") != uid) {
            throw new IOException("Not mine");
        }
        if (perms.contains(PosixFilePermission.GROUP_READ) ||
                perms.contains(PosixFilePermission.GROUP_WRITE) ||
                perms.contains(PosixFilePermission.GROUP_EXECUTE) ||
                perms.contains(PosixFilePermission.OTHERS_READ) ||
                perms.contains(PosixFilePermission.OTHERS_WRITE) ||
                perms.contains(PosixFilePermission.OTHERS_EXECUTE)) {
            throw new IOException("Accessible by someone else");
        }
    } catch (UnsupportedOperationException uoe) {
        // No POSIX permissions? Ignore it.
    }
    chan = Files.newByteChannel(p, StandardOpenOption.WRITE,
            StandardOpenOption.READ);

    long timeLimit = currTime.getSeconds() - readHeader(chan);

    long pos = 0;
    boolean seeNewButNotSame = false;
    while (true) {
        try {
            pos = chan.position();
            AuthTime a = AuthTime.readFrom(chan);
            if (a instanceof AuthTimeWithHash) {
                if (time.equals(a)) {
                    // Exact match, must be a replay
                    throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
                } else if (time.sameTimeDiffHash((AuthTimeWithHash)a)) {
                    // Two different authenticators in the same second.
                    // Remember it
                    seeNewButNotSame = true;
                }
            } else {
                if (time.isSameIgnoresHash(a)) {
                    // Two authenticators in the same second. Considered
                    // same if we haven't seen a new style version of it
                    if (!seeNewButNotSame) {
                        throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
                    }
                }
            }
            if (a.ctime < timeLimit) {
                missed++;
            } else {
                missed--;
            }
        } catch (BufferUnderflowException e) {
            // Half-written file?
            chan.position(pos);
            break;
        }
    }
    return missed;
}