AltaFile
A class in Alta.IO, Alta.Utilities.
A file on disk with cached typed content (IAltaFileFormat), background reads, and saves that go through a _temp file and keep a _backup copy. Get one from AltaFolder.GetFile.
- Listed in Other Alta assemblies
- Inherits or implements
IAltaFile
Constructors
| Member | What it is |
|---|---|
AltaFile(IAltaFolder parent, FileInfo info) | Creates an AltaFile from parent and info. |
Fields
12 private and internal fields
| Member | What it is |
|---|---|
private static HashSet<Task> allWriting | Static set of every save in progress, used by FinishAllWriting and FinishAllAsync. |
private FileInfo backup | The name_backup.ext file that holds the previous version after a save. |
private IAltaFileFormat content | Backing field for Content. |
private FileInfo fileInfo | The FileInfo for the file on disk. |
private bool isUnloading | Set when an unload was asked for during a save; the content is disposed when the save ends. |
private bool isWaitingForWriteCancel | True while WriteAsync waits for a cancelled earlier save, so further save calls only cancel and return. |
private static Logger logger | Class logger. |
private IAltaFolder parent | The folder to unregister from on delete. The constructor never assigns it, so it stays null. |
private Task readingTask | The read in progress, or null. |
private FileInfo temp | The name_temp.ext file a save writes to before swapping it in. |
private CancellationTokenSource writeCancel | Cancels the save in progress when a newer save starts. |
private Task writingTask | The save in progress, or null. |
Properties
| Member | What it is |
|---|---|
IAltaFileFormat Content { get; set; } | The loaded content. Setting it disposes the old content; logs an error if set while a read is running. |
string FullName { get; } | Full path of the file on disk. |
bool HasChanged { get; private set; } | True once a save has replaced the file on disk, until ResetHasChanged is called. |
bool IsExisting { get; } | Whether the file exists, from the cached FileInfo (call Refresh for a fresh answer). |
bool IsReading { get; } | True while a background read is running. |
bool IsWriting { get; } | True while a background save is running. |
DateTime LastAccess { get; } | Last access time of the file, in UTC. |
string Name { get; } | File name with extension. |
long Size { get; } | File size in bytes. |
Methods
| Member | What it is |
|---|---|
void Delete(bool isDeletingBackup = false) | Deletes the file from disk and drops its content. Pass true to delete the _backup copy as well. |
async Task DeleteAsync(bool isDeletingBackup = false) | Same as Delete, but the disk work runs on a background task. |
static async Task FinishAllAsync() | Static: waits for every save still running across all files, logging any failure. |
static void FinishAllWriting() | Static: blocks until every save still running across all files has finished. |
void FinishReading() | Blocks until the current read finishes. |
void FinishWriting() | Blocks until the current save finishes. |
void MoveTo(IAltaFolder folder) | Moves the file, and its _backup copy if there is one, into another folder. |
void QueueUnload() | Disposes the loaded content now, or after the current save if one is running. Refused while reading. |
async Task QueueUnloadAsync() | Like QueueUnload, but waits for the current save before returning. |
Task<T> ReadAsync<T>() | Reads and parses the file on a background task, or returns the loaded content if it is already that type. Gives null if the file is missing. |
async Task<T> ReadAsync<T>(Func<T> create) | Reads and parses the file on a background task, or returns the loaded content if it is already that type. Gives null if the file is missing. |
void Refresh() | Refreshes the cached file info from disk. |
void ResetHasChanged() | Clears HasChanged. |
async void WriteAsync() | Saves a clone of the content in the background. A newer save cancels an older one still pending. Does nothing without content. |
3 private and internal methods
| Member | What it is |
|---|---|
private FileInfo GetAlternative(string suffix) | Builds the path of a sibling file named name_suffix.ext. Used for the _backup and _temp files. |
private void ReadingTask<T>(Func<T> create) | Background body of ReadAsync: opens the file, creates the format object and reads into it. Logs errors instead of throwing. |
private async void WritingTaskAsync(Task waitFor, CancellationToken token, IAltaFileFormat writingClone) | Background body of WriteAsync: writes the _temp file, then swaps it in and keeps the old file as _backup. |