Added: Add FileType.SOCKET support and add FileUtils.deleteSocketFile() function

This commit is contained in:
agnostic-apollo
2022-04-17 06:18:14 +05:00
parent 58c3d427e8
commit c81d9c3346
5 changed files with 32 additions and 8 deletions

View File

@@ -1116,6 +1116,21 @@ public class FileUtils {
return deleteFile(label, filePath, ignoreNonExistentFile, false, FileType.SYMLINK.getValue());
}
/**
* Delete socket file at path.
*
* This function is a wrapper for {@link #deleteFile(String, String, boolean, boolean, int)}.
*
* @param label The optional label for file to delete. This can optionally be {@code null}.
* @param filePath The {@code path} for file to delete.
* @param ignoreNonExistentFile The {@code boolean} that decides if it should be considered an
* error if file to deleted doesn't exist.
* @return Returns the {@code error} if deletion was not successful, otherwise {@code null}.
*/
public static Error deleteSocketFile(String label, final String filePath, final boolean ignoreNonExistentFile) {
return deleteFile(label, filePath, ignoreNonExistentFile, false, FileType.SOCKET.getValue());
}
/**
* Delete regular, directory or symlink file at path.
*

View File

@@ -202,6 +202,10 @@ public class FileAttributes {
return ((st_mode & UnixConstants.S_IFMT) == UnixConstants.S_IFIFO);
}
public boolean isSocket() {
return ((st_mode & UnixConstants.S_IFMT) == UnixConstants.S_IFSOCK);
}
public boolean isBlock() {
return ((st_mode & UnixConstants.S_IFMT) == UnixConstants.S_IFBLK);
}

View File

@@ -3,14 +3,15 @@ package com.termux.shared.file.filesystem;
/** The {@link Enum} that defines file types. */
public enum FileType {
NO_EXIST("no exist", 0), // 0000000
REGULAR("regular", 1), // 0000001
DIRECTORY("directory", 2), // 0000010
SYMLINK("symlink", 4), // 0000100
CHARACTER("character", 8), // 0001000
FIFO("fifo", 16), // 0010000
BLOCK("block", 32), // 0100000
UNKNOWN("unknown", 64); // 1000000
NO_EXIST("no exist", 0), // 00000000
REGULAR("regular", 1), // 00000001
DIRECTORY("directory", 2), // 00000010
SYMLINK("symlink", 4), // 00000100
SOCKET("socket", 8), // 00001000
CHARACTER("character", 16), // 00010000
FIFO("fifo", 32), // 00100000
BLOCK("block", 64), // 01000000
UNKNOWN("unknown", 128); // 10000000
private final String name;
private final int value;

View File

@@ -104,6 +104,8 @@ public class FileTypes {
return FileType.DIRECTORY;
else if (fileAttributes.isSymbolicLink())
return FileType.SYMLINK;
else if (fileAttributes.isSocket())
return FileType.SOCKET;
else if (fileAttributes.isCharacter())
return FileType.CHARACTER;
else if (fileAttributes.isFifo())

View File

@@ -88,6 +88,8 @@ public class UnixConstants {
static final int S_IFLNK = OsConstants.S_IFLNK;
static final int S_IFSOCK = OsConstants.S_IFSOCK;
static final int S_IFCHR = OsConstants.S_IFCHR;
static final int S_IFBLK = OsConstants.S_IFBLK;