Set permissions to the CHMOD directory with AOH

On Unix, how to set directory permissions using AOH? I am looking to make drwxrwxrwx. Filezilla says the integer for this is 775, but JSCH incorrectly sets permissions. After JSCH sets permissions, Filezilla says it's 407.

+4
source share
3 answers

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

This system is based on the fact that there are 3 permission groups:

  • owner
  • groups
  • peace

and each group has an on / off bit for:

  • read
  • records
  • execute

therefore, the octal base is sufficient to represent all possible permissions configurations for the group. Every 3 octal digits correspond to a permission group.

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

Let's 0o1407 back to your caller ID problem: the octal representation of the decimal integer is 775 0o1407 , my suspicion is that the decimal 775 is actually sent instead of the octal 775, and FileZilla may very well trim the material to the left of the 3rd least significant digit 0o1407 (because for it it was not unreasonable to assume that nothing happened in the 3rd least significant bit)

Now 509 is a decimal representation of the octal 775 , try using this instead of AKX.

+5
source

This works for me:

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

here is a short and complete example of how you can easily use Jsch to change chmod to using the usual method of allocating 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); } } 
-2
source

Source: https://habr.com/ru/post/1443129/


All Articles