Struct std::old_io::fs::FileUnstable [-] [+] [src]

pub struct File {
    // some fields omitted
}

Unconstrained file access type that exposes read and write operations

Can be constructed via File::open(), File::create(), and File::open_mode().

Error

This type will return errors as an IoResult<T> if operations are attempted against it for which its underlying file descriptor was not configured at creation time, via the FileAccess parameter to File::open_mode().

Methods

impl File

fn open_mode(path: &Path, mode: FileMode, access: FileAccess) -> IoResult<File>

Open a file at path in the mode specified by the mode and access arguments

Example

fn main() { use std::old_io::{File, Open, ReadWrite}; let p = Path::new("/some/file/path.txt"); let file = match File::open_mode(&p, Open, ReadWrite) { Ok(f) => f, Err(e) => panic!("file error: {}", e), }; // do some stuff with that file // the file will be closed at the end of this block }
use std::old_io::{File, Open, ReadWrite};

let p = Path::new("/some/file/path.txt");

let file = match File::open_mode(&p, Open, ReadWrite) {
    Ok(f) => f,
    Err(e) => panic!("file error: {}", e),
};
// do some stuff with that file

// the file will be closed at the end of this block

FileMode and FileAccess provide information about the permissions context in which a given stream is created. More information about them can be found in std::io's docs. If a file is opened with Write or ReadWrite access, then it will be created if it does not already exist.

Note that, with this function, a File is returned regardless of the access-limitations indicated by FileAccess (e.g. calling write on a File opened as Read will return an error at runtime).

Error

This function will return an error under a number of different circumstances, to include but not limited to:

  • Opening a file that does not exist with Read access.
  • Attempting to open a file with a FileAccess that the user lacks permissions for
  • Filesystem-level errors (full disk, etc)

fn open(path: &Path) -> IoResult<File>

Attempts to open a file in read-only mode. This function is equivalent to File::open_mode(path, Open, Read), and will raise all of the same errors that File::open_mode does.

For more information, see the File::open_mode function.

Example

fn main() { use std::old_io::File; let contents = File::open(&Path::new("foo.txt")).read_to_end(); }
use std::old_io::File;

let contents = File::open(&Path::new("foo.txt")).read_to_end();

fn create(path: &Path) -> IoResult<File>

Attempts to create a file in write-only mode. This function is equivalent to File::open_mode(path, Truncate, Write), and will raise all of the same errors that File::open_mode does.

For more information, see the File::open_mode function.

Example

fn main() { #![allow(unused_must_use)] use std::old_io::File; let mut f = File::create(&Path::new("foo.txt")); f.write(b"This is a sample file"); drop(f); ::std::old_io::fs::unlink(&Path::new("foo.txt")); }
use std::old_io::File;

let mut f = File::create(&Path::new("foo.txt"));
f.write(b"This is a sample file");

fn path<'a>(&'a self) -> &'a Path

Returns the original path that was used to open this file.

fn fsync(&mut self) -> IoResult<()>

Synchronizes all modifications to this file to its permanent storage device. This will flush any internal buffers necessary to perform this operation.

fn datasync(&mut self) -> IoResult<()>

This function is similar to fsync, except that it may not synchronize file metadata to the filesystem. This is intended for use cases that must synchronize content, but don't need the metadata on disk. The goal of this method is to reduce disk operations.

fn truncate(&mut self, size: i64) -> IoResult<()>

Either truncates or extends the underlying file, updating the size of this file to become size. This is equivalent to unix's truncate function.

If the size is less than the current file's size, then the file will be shrunk. If it is greater than the current file's size, then the file will be extended to size and have all of the intermediate data filled in with 0s.

fn eof(&self) -> bool

Returns true if the stream has reached the end of the file.

If true, then this file will no longer continue to return data via read.

Note that the operating system will not return an EOF indicator until you have attempted to read past the end of the file, so if you've read exactly the number of bytes in the file, this will return false, not true.

fn stat(&self) -> IoResult<FileStat>

Queries information about the underlying file.

Trait Implementations

impl Reader for File

fn read(&mut self, buf: &mut [u8]) -> IoResult<usize>

fn read_at_least(&mut self, min: usize, buf: &mut [u8]) -> IoResult<usize>

fn read_byte(&mut self) -> IoResult<u8>

fn push(&mut self, len: usize, buf: &mut Vec<u8>) -> IoResult<usize>

fn push_at_least(&mut self, min: usize, len: usize, buf: &mut Vec<u8>) -> IoResult<usize>

fn read_exact(&mut self, len: usize) -> IoResult<Vec<u8>>

fn read_to_end(&mut self) -> IoResult<Vec<u8>>

fn read_to_string(&mut self) -> IoResult<String>

fn read_le_uint_n(&mut self, nbytes: usize) -> IoResult<u64>

fn read_le_int_n(&mut self, nbytes: usize) -> IoResult<i64>

fn read_be_uint_n(&mut self, nbytes: usize) -> IoResult<u64>

fn read_be_int_n(&mut self, nbytes: usize) -> IoResult<i64>

fn read_le_uint(&mut self) -> IoResult<usize>

fn read_le_int(&mut self) -> IoResult<isize>

fn read_be_uint(&mut self) -> IoResult<usize>

fn read_be_int(&mut self) -> IoResult<isize>

fn read_be_u64(&mut self) -> IoResult<u64>

fn read_be_u32(&mut self) -> IoResult<u32>

fn read_be_u16(&mut self) -> IoResult<u16>

fn read_be_i64(&mut self) -> IoResult<i64>

fn read_be_i32(&mut self) -> IoResult<i32>

fn read_be_i16(&mut self) -> IoResult<i16>

fn read_be_f64(&mut self) -> IoResult<f64>

fn read_be_f32(&mut self) -> IoResult<f32>

fn read_le_u64(&mut self) -> IoResult<u64>

fn read_le_u32(&mut self) -> IoResult<u32>

fn read_le_u16(&mut self) -> IoResult<u16>

fn read_le_i64(&mut self) -> IoResult<i64>

fn read_le_i32(&mut self) -> IoResult<i32>

fn read_le_i16(&mut self) -> IoResult<i16>

fn read_le_f64(&mut self) -> IoResult<f64>

fn read_le_f32(&mut self) -> IoResult<f32>

fn read_u8(&mut self) -> IoResult<u8>

fn read_i8(&mut self) -> IoResult<i8>

impl Writer for File

fn write_all(&mut self, buf: &[u8]) -> IoResult<()>

fn write(&mut self, buf: &[u8]) -> IoResult<()>

fn flush(&mut self) -> IoResult<()>

fn write_fmt(&mut self, fmt: Arguments) -> IoResult<()>

fn write_str(&mut self, s: &str) -> IoResult<()>

fn write_line(&mut self, s: &str) -> IoResult<()>

fn write_char(&mut self, c: char) -> IoResult<()>

fn write_int(&mut self, n: isize) -> IoResult<()>

fn write_uint(&mut self, n: usize) -> IoResult<()>

fn write_le_uint(&mut self, n: usize) -> IoResult<()>

fn write_le_int(&mut self, n: isize) -> IoResult<()>

fn write_be_uint(&mut self, n: usize) -> IoResult<()>

fn write_be_int(&mut self, n: isize) -> IoResult<()>

fn write_be_u64(&mut self, n: u64) -> IoResult<()>

fn write_be_u32(&mut self, n: u32) -> IoResult<()>

fn write_be_u16(&mut self, n: u16) -> IoResult<()>

fn write_be_i64(&mut self, n: i64) -> IoResult<()>

fn write_be_i32(&mut self, n: i32) -> IoResult<()>

fn write_be_i16(&mut self, n: i16) -> IoResult<()>

fn write_be_f64(&mut self, f: f64) -> IoResult<()>

fn write_be_f32(&mut self, f: f32) -> IoResult<()>

fn write_le_u64(&mut self, n: u64) -> IoResult<()>

fn write_le_u32(&mut self, n: u32) -> IoResult<()>

fn write_le_u16(&mut self, n: u16) -> IoResult<()>

fn write_le_i64(&mut self, n: i64) -> IoResult<()>

fn write_le_i32(&mut self, n: i32) -> IoResult<()>

fn write_le_i16(&mut self, n: i16) -> IoResult<()>

fn write_le_f64(&mut self, f: f64) -> IoResult<()>

fn write_le_f32(&mut self, f: f32) -> IoResult<()>

fn write_u8(&mut self, n: u8) -> IoResult<()>

fn write_i8(&mut self, n: i8) -> IoResult<()>

impl Seek for File

fn tell(&self) -> IoResult<u64>

fn seek(&mut self, pos: i64, style: SeekStyle) -> IoResult<()>

impl AsRawFd for File

fn as_raw_fd(&self) -> Fd

impl AsRawFd for File

fn as_raw_fd(&self) -> Fd