This discussion is archived
7 Replies Latest reply: Oct 31, 2011 12:59 AM by user12833994 RSS

File System Security API  : bug,  Cannot  set 777 in linux????

user12833994 Newbie
Currently Being Moderated
Evn jdk 1.6 u27, jdk 1.6 u29
Case 1:
import java.io.File;

public class Main {
public static void main(String[] args) {
File dir = new File("/data/test");
if (dir.exists()) {
dir.delete();
}
System.out.println("Create...");
dir.setReadable(true, false);
dir.setWritable(true, false);
dir.mkdirs();
System.out.println("Create!");
}
}



Case 2:
import java.io.File;

public class Main {
public static void main(String[] args) {
File dir = new File("/data/test");
if (dir.exists()) {
dir.delete();
}
System.out.println("Create...");
dir.setReadable(true, false);
dir.setWritable(false, false);
dir.mkdirs();
System.out.println("Create!");
}
}


Case 3:
import java.io.File;

public class Main {
public static void main(String[] args) {
File dir = new File("/data/test");
if (dir.exists()) {
dir.delete();
}
System.out.println("Create...");
dir.setReadable(false, false);
dir.setWritable(false, false);
dir.mkdirs();
System.out.println("Create!");
}
}





The results are the same....
drwxr-xr-x 2 root root

Edited by: user12833994 on 2011-10-27 上午4:49
  • 1. Re: File System Security API  : bug,  Cannot  set 777 in linux????
    baftos Expert
    Currently Being Moderated
    It should work if you move dir.mkdirs() before setting permissions.
    I think the documentation is not clear about this matter.
  • 2. Re: File System Security API  : bug,  Cannot  set 777 in linux????
    user12833994 Newbie
    Currently Being Moderated
    Ok!
    After modified:
    Case 1:
    import java.io.File;

    public class Main {
    public static void main(String[] args) {
    File dir = new File("/data/test");
    if (dir.exists()) {
    dir.delete();
    }
    System.out.println("Create...");
    dir.mkdirs();
    dir.setReadable(true, false);
    dir.setWritable(true, false);
    System.out.println("Create!");
    }
    }

    Case 2:
    import java.io.File;

    public class Main {
    public static void main(String[] args) {
    File dir = new File("/data/test");
    if (dir.exists()) {
    dir.delete();
    }
    System.out.println("Create...");
    dir.mkdirs();
    dir.setReadable(true, false);
    dir.setWritable(false, false);
    System.out.println("Create!");
    }
    }
    Case 3:
    import java.io.File;

    public class Main {
    public static void main(String[] args) {
    File dir = new File("/data/test");
    if (dir.exists()) {
    dir.delete();
    }
    System.out.println("Create...");
    dir.mkdirs();
    dir.setReadable(false, false);
    dir.setWritable(false, false);
    System.out.println("Create!");
    }
    }

    The same results!
  • 3. Re: File System Security API  : bug,  Cannot  set 777 in linux????
    baftos Expert
    Currently Being Moderated
    Honestly, I tried in Windows and your original code did not change the attributes, but mine did. No idea why it does not work in Linux...
  • 4. Re: File System Security API  : bug,  Cannot  set 777 in linux????
    user12833994 Newbie
    Currently Being Moderated
    I do server programming.
  • 5. Re: File System Security API  : bug,  Cannot  set 777 in linux????
    sabre150 Expert
    Currently Being Moderated
    Since I work mainly with Linux I have extended your example and find I can create and set a directory to 777. My code is
    import java.io.File;
    import java.io.InputStream;
    import java.io.PrintStream;
    
    public class Sabre20111028
    {
        static void executeCommand(String message, PrintStream ps, String... command) throws Exception
        {
            ps.println(message);
            final ProcessBuilder pb = new ProcessBuilder(command);
            pb.redirectErrorStream(true);
            final Process process = pb.start();
            final InputStream is = process.getInputStream();
            final byte[] buffer = new byte[1024];
            for (int count; (count = is.read(buffer)) != -1;)
            {
                ps.write(buffer, 0, count);
            }
            process.waitFor();
        }
    
        public static void main(String[] args) throws Exception
        {
            final boolean[] range =
            {
                false, true
            };
            final String[] properties =
            {
                "os.name", "os.arch", "os.version", "java.version", "java.vendor"
            };
            for (String property : properties)
            {
                System.out.println(property + " : " + System.getProperty(property));
            }
            final File dir = new File("/tmp/test");
            for (boolean ownerOnly : range)
            {
                for (boolean readable : range)
                {
                    for (boolean writeable : range)
                    {
                        System.out.println("===========================================================================");
                        if (dir.exists() && !dir.delete())
                        {
                            executeCommand("!!!!!!!!!! problem deleting test directory",
                                    System.out, "bash", "-c", "ls -ald " + dir.getAbsolutePath());
                        }
                        dir.mkdirs();
                        executeCommand("After creation",
                                System.out, "bash", "-c", "ls -ald " + dir.getAbsolutePath());
                        dir.setReadable(readable, ownerOnly);
                        executeCommand("After setReadable(" + readable + "," + ownerOnly + ")",
                                System.out, "bash", "-c", "ls -ald " + dir.getAbsolutePath());
                        dir.setWritable(writeable, ownerOnly);
                        executeCommand("After setWriteable(" + writeable + "," + ownerOnly + ")",
                                System.out, "bash", "-c", "ls -ald " + dir.getAbsolutePath());
                    }
                }
            }
        }
    
    }
    and the output is

    os.name : Linux
    os.arch : i386
    os.version : 2.6.38-12-generic
    java.version : 1.6.0_27
    java.vendor : Sun Microsystems Inc.
    ===========================================================================
    After creation
    drwxr-xr-x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    After setReadable(false,false)
    d-wx--x--x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    After setWriteable(false,false)
    d--x--x--x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    ===========================================================================
    After creation
    drwxr-xr-x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    After setReadable(false,false)
    d-wx--x--x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    After setWriteable(true,false)
    d-wx-wx-wx 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    ===========================================================================
    After creation
    drwxr-xr-x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    After setReadable(true,false)
    drwxr-xr-x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    After setWriteable(false,false)
    dr-xr-xr-x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    ===========================================================================
    After creation
    drwxr-xr-x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    After setReadable(true,false)
    drwxr-xr-x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    After setWriteable(true,false)
    drwxrwxrwx 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    ===========================================================================
    After creation
    drwxr-xr-x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    After setReadable(false,true)
    d-wxr-xr-x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    After setWriteable(false,true)
    d--xr-xr-x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    ===========================================================================
    After creation
    drwxr-xr-x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    After setReadable(false,true)
    d-wxr-xr-x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    After setWriteable(true,true)
    d-wxr-xr-x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    ===========================================================================
    After creation
    drwxr-xr-x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    After setReadable(true,true)
    drwxr-xr-x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    After setWriteable(false,true)
    dr-xr-xr-x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    ===========================================================================
    After creation
    drwxr-xr-x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    After setReadable(true,true)
    drwxr-xr-x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test
    After setWriteable(true,true)
    drwxr-xr-x 2 sabre sabre 4096 2011-10-28 20:18 /tmp/test

    So run this example and post the output.
  • 6. Re: File System Security API  : bug,  Cannot  set 777 in linux????
    user12833994 Newbie
    Currently Being Moderated
    [root@localhost ~]# cat /proc/version
    Linux version 2.6.18-238.el5 (mockbuild@builder10.centos.org) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-48))

    Why is not the same with mine?


    Post the results by using Command 'ls' outer in OS. ?
  • 7. Re: File System Security API  : bug,  Cannot  set 777 in linux????
    user12833994 Newbie
    Currently Being Moderated
    import java.io.File;

    public class Main {
    public static void main(String[] args) {
    File dir = null;
    dir = new File("/data/test1");
    if (dir.exists()) {
    dir.delete();
    }
    System.out.println("Create...");
    dir.mkdirs();
    dir.setReadable(true, false);
    dir.setWritable(true, false);
    dir.setExecutable(false, false);
    System.out.println("Create!");

    dir = new File("/data/test2");
    if (dir.exists()) {
    dir.delete();
    }
    System.out.println("Create...");
    dir.mkdirs();
    dir.setReadable(false, false);
    dir.setWritable(false, false);
    dir.setExecutable(false, false);
    System.out.println("Create!");

    dir = new File("/data/test3");
    if (dir.exists()) {
    dir.delete();
    }
    System.out.println("Create...");
    dir.mkdirs();
    dir.setReadable(true, true);
    dir.setWritable(true, true);
    dir.setExecutable(false, false);
    System.out.println("Create!");

    dir = new File("/data/test4");
    if (dir.exists()) {
    dir.delete();
    }
    System.out.println("Create...");
    dir.mkdirs();
    dir.setReadable(false, true);
    dir.setWritable(false, true);
    dir.setExecutable(false, false);
    System.out.println("Create!");

    dir = new File("/data/test5");
    if (dir.exists()) {
    dir.delete();
    }
    System.out.println("Create...");
    dir.mkdirs();
    dir.setReadable(true, true);
    dir.setWritable(true, false);
    dir.setExecutable(false, false);
    System.out.println("Create!");

    dir = new File("/data/test6");
    if (dir.exists()) {
    dir.delete();
    }
    System.out.println("Create...");
    dir.mkdirs();
    dir.setReadable(true, false);
    dir.setWritable(true, true);
    dir.setExecutable(false, false);
    System.out.println("Create!");

    dir = new File("/data/test7");
    if (dir.exists()) {
    dir.delete();
    }
    System.out.println("Create...");
    dir.mkdirs();
    dir.setReadable(false, false);
    dir.setWritable(false, false);
    dir.setReadable(true, true);
    dir.setWritable(true, true);
    dir.setExecutable(false, false);
    System.out.println("Create!");

    }
    }

    drw-rw-rw- 2 root root 4096 10-31 15:53 test1
    d--------- 2 root root 4096 10-31 15:53 test2
    drw-r--r-- 2 root root 4096 10-31 15:53 test3
    d---r--r-- 2 root root 4096 10-31 15:53 test4
    drw-rw-rw- 2 root root 4096 10-31 15:53 test5
    drw-r--r-- 2 root root 4096 10-31 15:53 test6
    drw------- 2 root root 4096 10-31 15:53 test7



    java version "1.6.0_27"
    Java(TM) SE Runtime Environment (build 1.6.0_27-b07)
    Java HotSpot(TM) 64-Bit Server VM (build 20.2-b06, mixed mode)

    Linux version 2.6.18-238.el5 (mockbuild@builder10.centos.org) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-48))



    And why ???

    Edited by: user12833994 on 2011-10-31 上午12:59

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points