forked from termux/termux-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NativeDispatcher.java
58 lines (43 loc) · 2.09 KB
/
NativeDispatcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.termux.shared.file.filesystem;
import android.system.ErrnoException;
import android.system.Os;
import java.io.File;
import java.io.FileDescriptor;
import java.io.IOException;
public class NativeDispatcher {
public static void stat(String filePath, FileAttributes fileAttributes) throws IOException {
validateFileExistence(filePath);
try {
fileAttributes.loadFromStructStat(Os.stat(filePath));
} catch (ErrnoException e) {
throw new IOException("Failed to run Os.stat() on file at path \"" + filePath + "\": " + e.getMessage());
}
}
public static void lstat(String filePath, FileAttributes fileAttributes) throws IOException {
validateFileExistence(filePath);
try {
fileAttributes.loadFromStructStat(Os.lstat(filePath));
} catch (ErrnoException e) {
throw new IOException("Failed to run Os.lstat() on file at path \"" + filePath + "\": " + e.getMessage());
}
}
public static void fstat(FileDescriptor fileDescriptor, FileAttributes fileAttributes) throws IOException {
validateFileDescriptor(fileDescriptor);
try {
fileAttributes.loadFromStructStat(Os.fstat(fileDescriptor));
} catch (ErrnoException e) {
throw new IOException("Failed to run Os.fstat() on file descriptor \"" + fileDescriptor.toString() + "\": " + e.getMessage());
}
}
public static void validateFileExistence(String filePath) throws IOException {
if (filePath == null || filePath.isEmpty()) throw new IOException("The path is null or empty");
File file = new File(filePath);
//if (!file.exists())
// throw new IOException("No such file or directory: \"" + filePath + "\"");
}
public static void validateFileDescriptor(FileDescriptor fileDescriptor) throws IOException {
if (fileDescriptor == null) throw new IOException("The file descriptor is null");
if (!fileDescriptor.valid())
throw new IOException("No such file descriptor: \"" + fileDescriptor.toString() + "\"");
}
}