chmod2

@JvmOverloads
fun File.chmod2(mode: String, mustExist: Boolean = true): File(source)

Modifies file or directory permissiveness.

NOTE: On Windows this will only modify the read-only attribute of a regular file. If mode contains any owner write permissions, then the read-only attribute is removed. If mode does not contain any owner write permissions, then the read-only attribute is applied.

e.g.

// The default POSIX file permissions for a new file
myFile.chmod2("666")

POSIX permissions:

  • 7: READ | WRITE | EXECUTE

  • 6: READ | WRITE

  • 5: READ | EXECUTE

  • 4: READ

  • 3: WRITE | EXECUTE

  • 2: WRITE

  • 1: EXECUTE

  • 0: NONE

Mode char index (e.g. "740" index 0 is 7, index 1 is 4, index 2 is 0):

  • index 0: Owner(7) READ | WRITE | EXECUTE

  • index 1: Group(4) READ

  • index 2: Other(0) NONE

See chmod(2)

Return

The File for chaining operations.

Parameters

mode

The permissions to set. Must be 3 digits, each being between 0 and 7 (inclusive).

mustExist

If false, failure to apply permissions due to the file or directory's non-existence on the filesystem will return safely, instead of throwing FileNotFoundException. If true, then the FileNotFoundException will be thrown. Default true.

Throws

IllegalArgumentException

If mode is inappropriate.

If there was a failure to apply desired permissions such as non-existence, or a security exception.

UnsupportedOperationException

On Kotlin/JS-Browser.