简体   繁体   English

File.canRead()返回false

[英]File.canRead() returns false

I have a server client program, with server running on Raspberry Pi (running Linux) and client being a Android application. 我有一个服务器客户端程序,服务器在Raspberry Pi(运行Linux)上运行,客户端为Android应用程序。 I am writing a in application file browser, which simply sends a path to the server and in turn server returns the list of files using file.listFiles() (server is written in Java). 我正在编写一个应用程序内文件浏览器,该浏览器仅将路径发送到服务器,然后服务器使用file.listFiles()返回文件列表(服务器用Java编写)。

I run the server / Java program as sudo , hoping that this would give access to all the files across system, but to my surprise, this user does not have read permissions on /home directory of Pi. 我以sudo身份运行服务器/ Java程序,希望它可以访问整个系统中的所有文件,但是令我惊讶的是,该用户没有对Pi的/home目录的读取权限。

So here is what happens in sequence: 所以这是顺序发生的事情:

I request file.listFiles(new File("/")); 我请求file.listFiles(new File("/")); which is sent over socket from Pi to Android app. 通过套接字从Pi发送到Android应用。 In the app, before making the next request, I check if I can read the file using file.canRead(); 在应用程序中,在发出下一个请求之前,我检查是否可以使用file.canRead();读取文件file.canRead(); and for /home directory, this returns false. 对于/home目录,这将返回false。

File permissions on Pi: Pi的文件权限:

pi@raspberrypi ~ $ ls -l / | grep home
drwxr-xr-x   3 root root  4096 Dec 31  1969 home

How I run my Java server: 我如何运行Java服务器:

pi@raspberrypi ~ $ sudo java -cp "/usr/local/lib/bluecovelib/bluecove/target/bluecove-2.1.1-SNAPSHOT.jar:/usr/local/lib/bluecovelib/bluecove-gpl-2.1.1-SNAPSHOT/target/bluecove-gpl-2.1.1-SNAPSHOT.jar:/home/pi/severinteractionutils.jar:." AppConnect

Why does sudo user does not have access to /home directory from the Java program? 为什么sudo用户无法从Java程序访问/home目录?

EDIT: 编辑:

I ran a simple program on Pi to test if it is because the underlying OS has changed. 我在Pi上运行了一个简单的程序,以测试是否是因为底层操作系统已更改。 Apparently it does have an effect. 显然它确实有作用。

public class TestFileBrowser {
    public static void main(String[] args) { 
        System.out.println("Hello, World");
        File file = new File("/");
        File [] files = file.listFiles();
        for(int i=0; i<files.length; ++i) {
            if(files[i].getAbsolutePath().equals("/home")) {
                File homeDir = files[i];
                if(homeDir.canRead()) {
                    System.out.println("Can Read");
                    if (homeDir.isDirectory()) {
                        System.out.println("Is directory");
                    }
                }
            }
        }
    }
}

and here is the console snapshot: 这是控制台快照:

pi@raspberrypi ~ $ sudo javac TestFileBrowser.java 
pi@raspberrypi ~ $ sudo java TestFileBrowser
Hello, World
Can Read
Is directory

and for /home directory, this returns false. 对于/ home目录,这将返回false。

Correct, because you can't read from a directory using typical File Stream IO. 正确,因为您无法使用典型的文件流IO从目录读取。 You can call File.isDirectory() which (per the Javadoc) 您可以调用File.isDirectory() (根据Javadoc)

Tests whether the file denoted by this abstract pathname is a directory. 测试此抽象路径名表示的文件是否为目录。

Where it is required to distinguish an I/O exception from the case that the file is not a directory, or where several attributes of the same file are required at the same time, then the Files.readAttributes method may be used. 如果需要将I / O异常与文件不是目录的情况区分开,或者同时需要同一文件的多个属性,则可以使用Files.readAttributes方法。

And you can check permission to read with the attributes. 并且您可以检查读取属性的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM