java 使用 JSCH 设置目录权限 CHMOD

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13150122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:40:08  来源:igfitidea点击:

Set Directory Permissions CHMOD with JSCH

javajsch

提问by Greg Finzer

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.

在 Unix 中,如何使用 JSCH 设置目录权限?我正在寻找做 drwxrwxrwx。Filezilla 说它的整数是 775,但 JSCH 没有正确设置权限。JSCH设置权限后Filezilla说是407。

回答by Javier Asensio

This works to me:

这对我有用:

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

回答by sampson-chen

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.

Unix 中的文件权限代码(777例如)是八进制,而不是十进制。如:当您执行类似操作时chmod -R 777,数字将被解释为八进制输入而不是十进制输入。

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

该系统来自于有 3 个权限组的事实:

  • 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.

所以八进制足以代表一个组的所有可能的权限配置。3 个八进制数字分别对应一个权限组。

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

(进一步阅读: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)

回到 JSCH 的问题:十进制整数775的八进制表示是0o1407,我怀疑实际上发送的是十进制 775 而不是八进制 775,而 FileZilla 很可能会截断第三个最低有效数字左侧的内容的0o1407(因为它假设没有超过第三个最不重要的位并不是没有道理的)

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

现在,509是八进制的十进制表示775,请尝试将其与 JSCH 一起使用。

回答by Mang Jojot

it's all about server configurations.enter image description here

这都是关于服务器配置的。在此处输入图片说明

just untick Automatically rename existing files on overwrite

只需取消勾选覆盖时自动重命名现有文件

回答by benjch

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

这是一个简短而完整的示例,说明如何使用通常的方式来描述 CHMOD 权限,从而轻松地使用 Jsch 来更改 chmod

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

================================================== ======== 简短回答: 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);
    }
}