Set Directory Permissions CHMOD with JSCH

懵懂的女人 提交于 2019-12-01 03:56:52

问题


In Unix, how do I set the directory permissions with JSCH? I am looking to do drwxrwxrwx. Filezilla says the integer for that is 775 but JSCH is not setting the permissions correctly. After JSCH sets the permissions Filezilla says that it is 407.


回答1:


The file permissions code in Unix (777, for example) are octal, not decimal. As in: when you do something like chmod -R 777, the digits are interpreted as octal input instead of decimal input.

This system comes from the fact that there are 3 permission groups:

  • owner
  • group
  • world

and each group has an "on/off bit" for:

  • read
  • write
  • execute

so octal-base is sufficient to represent all possible permission configurations for a group. The 3 octal-digits each correspond to a permission group.

(For further reading on this: http://www.december.com/unix/ref/chmod.html)

Back to your problem with JSCH: the decimal integer 775's octal representation is 0o1407, my suspicion is that the decimal 775 is actually sent instead of the octal 775, and FileZilla may very well be truncating the stuff to the left of the 3rd least significant digit of 0o1407 (because it's not unreasonable for it to assume there's nothing past the 3rd least significant bit)

Now, 509 is the decimal representation of octal 775, try using that with JSCH instead.




回答2:


This works to me:

sftp.chmod(Integer.parseInt(permissionStringInDecimal,8), str_Directory+fileName);



回答3:


it's all about server configurations.

just untick Automatically rename existing files on overwrite




回答4:


here is a short and a full example of how to can easyly use Jsch to change a chmod by using the usual way to decrib a CHMOD permission

========================================================= short answer : int chmodInt = Integer.parseInt(chmod, 8); channel.chmod(chmodInt, fileLinux);

========================================================= Full Example :

package example;

import java.io.IOException;
import java.util.Date;

import main.services.ServiceSSH;

import org.junit.Test;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class ExampleChmod {

    @Test
    public void testchmod() throws JSchException, SftpException, IOException {
        Session session = ServiceSSH.getSession(); // Use your own session Factory
        Date dateStart = new Date();
        chmod("/home/user/launcher.sh", "777", session);
        Date dateEnd = new Date();
        session.disconnect();
        System.out.println(dateEnd.getTime() - dateStart.getTime() + "ms");
    }

    public static void chmod(String fileLinux, String chmod, Session session) throws JSchException, SftpException {
        ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
        channel.connect();
        chmod(fileLinux, chmod, channel);
        channel.disconnect();

    }

    private static void chmod(String fileLinux, String chmod, ChannelSftp channel) throws SftpException {
        int chmodInt = Integer.parseInt(chmod, 8);
        channel.chmod(chmodInt, fileLinux);
    }
}


来源:https://stackoverflow.com/questions/13150122/set-directory-permissions-chmod-with-jsch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!