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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use serde::{Deserialize, Serialize};
use crate::*;
/// Access mode
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
#[non_exhaustive]
pub enum FileAccessMode {
/// Opens the file in read-only mode.
///
/// FileDescriptor mode: "r"
Read,
/// Opens the file in write-only mode.
///
/// Until Android 10, this will always truncate existing contents.
/// Since Android 10, this may or may not truncate existing contents.
/// If the new file is smaller than the old one, **this may cause the file to become corrupted**.
/// <https://issuetracker.google.com/issues/180526528>
///
/// The reason this is marked as deprecated is because of that behavior,
/// and it is not scheduled to be removed in the future.
///
/// FileDescriptor mode: "w"
#[deprecated(note = "This may or may not truncate existing contents. If the new file is smaller than the old one, this may cause the file to become corrupted.")]
Write,
/// Opens the file in write-only mode.
/// The existing content is truncated (deleted), and new data is written from the beginning.
///
/// FileDescriptor mode: "wt"
WriteTruncate,
/// Opens the file in write-only mode.
/// The existing content is preserved, and new data is appended to the end of the file.
///
/// FileDescriptor mode: "wa"
WriteAppend,
/// Opens the file in read-write mode.
///
/// FileDescriptor mode: "rw"
ReadWrite,
/// Opens the file in read-write mode.
/// The existing content is truncated (deleted), and new data is written from the beginning.
///
/// FileDescriptor mode: "rwt"
ReadWriteTruncate,
}
#[allow(unused)]
#[allow(deprecated)]
impl FileAccessMode {
pub(crate) fn to_mode(&self) -> &'static str {
match self {
FileAccessMode::Read => "r",
FileAccessMode::Write => "w",
FileAccessMode::WriteTruncate => "wt",
FileAccessMode::WriteAppend => "wa",
FileAccessMode::ReadWriteTruncate => "rwt",
FileAccessMode::ReadWrite => "rw",
}
}
pub(crate) fn from_mode(mode: &str) -> Result<Self> {
match mode {
"r" => Ok(Self::Read),
"w" => Ok(Self::Write),
"wt" => Ok(Self::WriteTruncate),
"wa" => Ok(Self::WriteAppend),
"rwt" => Ok(Self::ReadWriteTruncate),
"rw" => Ok(Self::ReadWrite),
mode => Err(Error::with(format!("Illegal mode: {mode}")))
}
}
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
pub enum UriPermission {
/// Read access.
Read,
/// Write access.
Write,
/// Read-write access.
ReadAndWrite,
/// Read or write access.
ReadOrWrite,
}
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
pub enum PersistedUriPermissionState {
File {
uri: FileUri,
can_read: bool,
can_write: bool,
},
Dir {
uri: FileUri,
can_read: bool,
can_write: bool,
}
}
impl PersistedUriPermissionState {
pub fn uri(&self) -> &FileUri {
match self {
PersistedUriPermissionState::File { uri, .. } => uri,
PersistedUriPermissionState::Dir { uri, .. } => uri,
}
}
pub fn into_uri(self) -> FileUri {
match self {
PersistedUriPermissionState::File { uri, .. } => uri,
PersistedUriPermissionState::Dir { uri, .. } => uri,
}
}
pub fn can_read(&self) -> bool {
match self {
PersistedUriPermissionState::File { can_read, .. } => *can_read,
PersistedUriPermissionState::Dir { can_read, .. } => *can_read,
}
}
pub fn can_write(&self) -> bool {
match self {
PersistedUriPermissionState::File { can_write, .. } => *can_write,
PersistedUriPermissionState::Dir { can_write, .. } => *can_write,
}
}
pub fn is_file(&self) -> bool {
matches!(self, PersistedUriPermissionState::File { .. })
}
pub fn is_dir(&self) -> bool {
matches!(self, PersistedUriPermissionState::Dir { .. })
}
}