|
| 1 | +use crate::ffi::{CString, OsString}; |
| 2 | +use crate::fmt; |
| 3 | +use crate::hash::Hash; |
| 4 | +use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom}; |
| 5 | +use crate::path::{Path, PathBuf}; |
| 6 | +use crate::sys::time::SystemTime; |
| 7 | +use crate::sys::unsupported; |
| 8 | + |
| 9 | +struct Fd(*mut vex_sdk::FIL); |
| 10 | + |
| 11 | +pub struct File(Fd); |
| 12 | + |
| 13 | +//TODO: We may be able to get some of this info |
| 14 | +#[derive(Clone)] |
| 15 | +pub struct FileAttr; |
| 16 | + |
| 17 | +pub struct ReadDir(!); |
| 18 | + |
| 19 | +pub struct DirEntry(!); |
| 20 | + |
| 21 | +#[derive(Clone, Debug)] |
| 22 | +pub struct OpenOptions { |
| 23 | + read: bool, |
| 24 | + write: bool, |
| 25 | + append: bool, |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Copy, Clone, Debug, Default)] |
| 29 | +pub struct FileTimes {} |
| 30 | + |
| 31 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 32 | +pub struct FilePermissions; |
| 33 | + |
| 34 | +#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash)] |
| 35 | +pub struct FileType { |
| 36 | + is_dir: bool, |
| 37 | +} |
| 38 | + |
| 39 | +#[derive(Debug)] |
| 40 | +pub struct DirBuilder {} |
| 41 | + |
| 42 | +impl FileAttr { |
| 43 | + pub fn size(&self) -> u64 { |
| 44 | + todo!() |
| 45 | + } |
| 46 | + |
| 47 | + pub fn perm(&self) -> FilePermissions { |
| 48 | + todo!() |
| 49 | + } |
| 50 | + |
| 51 | + pub fn file_type(&self) -> FileType { |
| 52 | + todo!() |
| 53 | + } |
| 54 | + |
| 55 | + pub fn modified(&self) -> io::Result<SystemTime> { |
| 56 | + todo!() |
| 57 | + } |
| 58 | + |
| 59 | + pub fn accessed(&self) -> io::Result<SystemTime> { |
| 60 | + todo!() |
| 61 | + } |
| 62 | + |
| 63 | + pub fn created(&self) -> io::Result<SystemTime> { |
| 64 | + todo!() |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl FilePermissions { |
| 69 | + pub fn readonly(&self) -> bool { |
| 70 | + false |
| 71 | + } |
| 72 | + |
| 73 | + pub fn set_readonly(&mut self, _readonly: bool) { |
| 74 | + panic!("Perimissions do not exist") |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl FileTimes { |
| 79 | + pub fn set_accessed(&mut self, _t: SystemTime) {} |
| 80 | + pub fn set_modified(&mut self, _t: SystemTime) {} |
| 81 | +} |
| 82 | + |
| 83 | +impl FileType { |
| 84 | + pub fn is_dir(&self) -> bool { |
| 85 | + self.is_dir |
| 86 | + } |
| 87 | + |
| 88 | + pub fn is_file(&self) -> bool { |
| 89 | + !self.is_dir |
| 90 | + } |
| 91 | + |
| 92 | + pub fn is_symlink(&self) -> bool { |
| 93 | + false |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +impl fmt::Debug for ReadDir { |
| 98 | + fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 99 | + self.0 |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl Iterator for ReadDir { |
| 104 | + type Item = io::Result<DirEntry>; |
| 105 | + |
| 106 | + fn next(&mut self) -> Option<io::Result<DirEntry>> { |
| 107 | + self.0 |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +impl DirEntry { |
| 112 | + pub fn path(&self) -> PathBuf { |
| 113 | + self.0 |
| 114 | + } |
| 115 | + |
| 116 | + pub fn file_name(&self) -> OsString { |
| 117 | + self.0 |
| 118 | + } |
| 119 | + |
| 120 | + pub fn metadata(&self) -> io::Result<FileAttr> { |
| 121 | + self.0 |
| 122 | + } |
| 123 | + |
| 124 | + pub fn file_type(&self) -> io::Result<FileType> { |
| 125 | + self.0 |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +impl OpenOptions { |
| 130 | + pub fn new() -> OpenOptions { |
| 131 | + OpenOptions { read: false, write: false, append: false } |
| 132 | + } |
| 133 | + |
| 134 | + pub fn read(&mut self, read: bool) { |
| 135 | + self.read = read; |
| 136 | + } |
| 137 | + pub fn write(&mut self, write: bool) { |
| 138 | + self.write = write; |
| 139 | + } |
| 140 | + pub fn append(&mut self, append: bool) { |
| 141 | + self.append = append; |
| 142 | + } |
| 143 | + pub fn truncate(&mut self, _truncate: bool) {} |
| 144 | + pub fn create(&mut self, create: bool) { |
| 145 | + self.write = create; |
| 146 | + } |
| 147 | + pub fn create_new(&mut self, _create_new: bool) {} |
| 148 | +} |
| 149 | + |
| 150 | +impl File { |
| 151 | + pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> { |
| 152 | + let path = CString::new(path.as_os_str().as_encoded_bytes()).map_err(|_| { |
| 153 | + io::Error::new(io::ErrorKind::InvalidData, "Path contained a null byte") |
| 154 | + })?; |
| 155 | + |
| 156 | + let file = if opts.read && !opts.write { |
| 157 | + // The second argument to this function is ignored. |
| 158 | + // Open in read only mode |
| 159 | + unsafe { vex_sdk::vexFileOpen(path.as_ptr(), c"".as_ptr()) } |
| 160 | + } else if opts.write && opts.append { |
| 161 | + // Open in read/write and append mode |
| 162 | + unsafe { vex_sdk::vexFileOpenWrite(path.as_ptr()) } |
| 163 | + } else if opts.write { |
| 164 | + // Open in read/write mode |
| 165 | + unsafe { vex_sdk::vexFileOpenCreate(path.as_ptr()) } |
| 166 | + } else { |
| 167 | + return Err(io::Error::new( |
| 168 | + io::ErrorKind::InvalidInput, |
| 169 | + "Files cannot be opened without read or write access", |
| 170 | + )); |
| 171 | + }; |
| 172 | + |
| 173 | + if file.is_null() { |
| 174 | + Err(io::Error::new(io::ErrorKind::NotFound, "Could not open file")) |
| 175 | + } else { |
| 176 | + Ok(Self(Fd(file))) |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + pub fn file_attr(&self) -> io::Result<FileAttr> { |
| 181 | + todo!() |
| 182 | + } |
| 183 | + |
| 184 | + pub fn fsync(&self) -> io::Result<()> { |
| 185 | + todo!() |
| 186 | + } |
| 187 | + |
| 188 | + pub fn datasync(&self) -> io::Result<()> { |
| 189 | + todo!() |
| 190 | + } |
| 191 | + |
| 192 | + pub fn truncate(&self, _size: u64) -> io::Result<()> { |
| 193 | + todo!() |
| 194 | + } |
| 195 | + |
| 196 | + pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> { |
| 197 | + let len = buf.len() as _; |
| 198 | + let buf_ptr = buf.as_mut_ptr(); |
| 199 | + let read = unsafe { vex_sdk::vexFileRead(buf_ptr.cast(), 1, len, self.0.0) }; |
| 200 | + if read < 0 { |
| 201 | + Err(io::Error::new( |
| 202 | + io::ErrorKind::Other, |
| 203 | + "Could not read from file", |
| 204 | + )) |
| 205 | + } else { |
| 206 | + Ok(read as usize) |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + pub fn read_vectored(&self, _bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> { |
| 211 | + todo!() |
| 212 | + } |
| 213 | + |
| 214 | + pub fn is_read_vectored(&self) -> bool { |
| 215 | + todo!() |
| 216 | + } |
| 217 | + |
| 218 | + pub fn read_buf(&self, _cursor: BorrowedCursor<'_>) -> io::Result<()> { |
| 219 | + todo!() |
| 220 | + } |
| 221 | + |
| 222 | + pub fn write(&self, _buf: &[u8]) -> io::Result<usize> { |
| 223 | + todo!() |
| 224 | + } |
| 225 | + |
| 226 | + pub fn write_vectored(&self, _bufs: &[IoSlice<'_>]) -> io::Result<usize> { |
| 227 | + todo!() |
| 228 | + } |
| 229 | + |
| 230 | + pub fn is_write_vectored(&self) -> bool { |
| 231 | + todo!() |
| 232 | + } |
| 233 | + |
| 234 | + pub fn flush(&self) -> io::Result<()> { |
| 235 | + todo!() |
| 236 | + } |
| 237 | + |
| 238 | + pub fn seek(&self, _pos: SeekFrom) -> io::Result<u64> { |
| 239 | + todo!() |
| 240 | + } |
| 241 | + |
| 242 | + pub fn duplicate(&self) -> io::Result<File> { |
| 243 | + todo!() |
| 244 | + } |
| 245 | + |
| 246 | + pub fn set_permissions(&self, _perm: FilePermissions) -> io::Result<()> { |
| 247 | + todo!() |
| 248 | + } |
| 249 | + |
| 250 | + pub fn set_times(&self, _times: FileTimes) -> io::Result<()> { |
| 251 | + todo!() |
| 252 | + } |
| 253 | +} |
| 254 | + |
| 255 | +impl DirBuilder { |
| 256 | + pub fn new() -> DirBuilder { |
| 257 | + DirBuilder {} |
| 258 | + } |
| 259 | + |
| 260 | + pub fn mkdir(&self, _p: &Path) -> io::Result<()> { |
| 261 | + unsupported() |
| 262 | + } |
| 263 | +} |
| 264 | + |
| 265 | +impl fmt::Debug for File { |
| 266 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 267 | + f.debug_struct("File").finish_non_exhaustive() |
| 268 | + } |
| 269 | +} |
| 270 | + |
| 271 | +pub fn readdir(_p: &Path) -> io::Result<ReadDir> { |
| 272 | + unsupported() |
| 273 | +} |
| 274 | + |
| 275 | +pub fn unlink(_p: &Path) -> io::Result<()> { |
| 276 | + unsupported() |
| 277 | +} |
| 278 | + |
| 279 | +pub fn rename(_old: &Path, _new: &Path) -> io::Result<()> { |
| 280 | + unsupported() |
| 281 | +} |
| 282 | + |
| 283 | +pub fn set_perm(_p: &Path, _perm: FilePermissions) -> io::Result<()> { |
| 284 | + unsupported() |
| 285 | +} |
| 286 | + |
| 287 | +pub fn rmdir(_p: &Path) -> io::Result<()> { |
| 288 | + unsupported() |
| 289 | +} |
| 290 | + |
| 291 | +pub fn remove_dir_all(_path: &Path) -> io::Result<()> { |
| 292 | + unsupported() |
| 293 | +} |
| 294 | + |
| 295 | +pub fn try_exists(_path: &Path) -> io::Result<bool> { |
| 296 | + unsupported() |
| 297 | +} |
| 298 | + |
| 299 | +pub fn readlink(_p: &Path) -> io::Result<PathBuf> { |
| 300 | + unsupported() |
| 301 | +} |
| 302 | + |
| 303 | +pub fn symlink(_original: &Path, _link: &Path) -> io::Result<()> { |
| 304 | + unsupported() |
| 305 | +} |
| 306 | + |
| 307 | +pub fn link(_src: &Path, _dst: &Path) -> io::Result<()> { |
| 308 | + unsupported() |
| 309 | +} |
| 310 | + |
| 311 | +pub fn stat(_p: &Path) -> io::Result<FileAttr> { |
| 312 | + unsupported() |
| 313 | +} |
| 314 | + |
| 315 | +pub fn lstat(_p: &Path) -> io::Result<FileAttr> { |
| 316 | + unsupported() |
| 317 | +} |
| 318 | + |
| 319 | +pub fn canonicalize(_p: &Path) -> io::Result<PathBuf> { |
| 320 | + unsupported() |
| 321 | +} |
| 322 | + |
| 323 | +pub fn copy(_from: &Path, _to: &Path) -> io::Result<u64> { |
| 324 | + unsupported() |
| 325 | +} |
0 commit comments