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