This repository has been archived on 2025-01-01. You can view files and clone it, but cannot push or open issues or pull requests.
ShiftOS_TheReturn/ShiftOS.Objects/ShiftFS.cs

454 lines
14 KiB
C#
Raw Normal View History

2017-01-08 10:17:07 -05:00
/*
* MIT License
*
* Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
2017-01-08 09:57:10 -05:00
using Newtonsoft.Json;
using System.Collections.Generic;
using static ShiftOS.Objects.ShiftFS.Utils;
using System.Text;
using System.Threading;
namespace ShiftOS.Objects.ShiftFS
{
public class File
{
public string Name;
public byte[] Data;
public byte[] HeaderData;
2017-01-08 09:57:10 -05:00
public bool ReadAccessToLowUsers;
public System.IO.Stream GetStream()
{
2017-07-13 21:30:04 -04:00
return new System.IO.MemoryStream(Data);
2017-01-08 09:57:10 -05:00
}
2017-07-13 21:30:04 -04:00
public File(string name, byte[] data, bool ReadAccess_to_low_users)
2017-01-08 09:57:10 -05:00
{
Name = name;
Data = data;
ReadAccessToLowUsers = ReadAccess_to_low_users;
}
}
public class Directory
{
public string Name;
public List<File> Files = new List<File>();
public List<Directory> Subdirectories = new List<Directory>();
public bool ReadAccessToLowUsers;
public void AddFile(File file)
{
2017-07-13 21:30:04 -04:00
Files.Add(file);
2017-01-08 09:57:10 -05:00
}
2017-07-13 21:30:04 -04:00
2017-01-08 09:57:10 -05:00
public void RemoveFile(string name)
{
2017-07-13 21:30:04 -04:00
Files.Remove(Files.Find(x => x.Name == name));
2017-01-08 09:57:10 -05:00
}
2017-07-13 21:30:04 -04:00
2017-01-08 09:57:10 -05:00
public void RemoveFile(File file)
{
2017-07-13 21:30:04 -04:00
Files.Remove(file);
2017-01-08 09:57:10 -05:00
}
2017-07-13 21:30:04 -04:00
2017-01-08 09:57:10 -05:00
public File FindFileByName(string name)
{
2017-07-13 21:30:04 -04:00
return Files.Find(x => x.Name == name);
2017-01-08 09:57:10 -05:00
}
2017-07-13 21:30:04 -04:00
2017-01-08 09:57:10 -05:00
public void AddDirectory(Directory dir)
{
2017-07-13 21:30:04 -04:00
Subdirectories.Add(dir);
2017-01-08 09:57:10 -05:00
}
2017-07-13 21:30:04 -04:00
2017-01-08 09:57:10 -05:00
public void RemoveDirectory(string name)
{
2017-07-13 21:30:04 -04:00
Subdirectories.Remove(Subdirectories.Find(x => x.Name == name));
2017-01-08 09:57:10 -05:00
}
2017-07-13 21:30:04 -04:00
2017-01-08 09:57:10 -05:00
public void RemoveDirectory(Directory dir)
{
2017-07-13 21:30:04 -04:00
Subdirectories.Remove(dir);
2017-01-08 09:57:10 -05:00
}
2017-07-13 21:30:04 -04:00
2017-01-08 09:57:10 -05:00
public Directory FindDirectoryByName(string name)
{
2017-07-13 21:30:04 -04:00
return Subdirectories.Find(x => x.Name == name);
2017-01-08 09:57:10 -05:00
}
}
public static class Utils
{
public static List<Directory> Mounts { get; set; }
static Utils()
{
if (Mounts == null)
Mounts = new List<Directory>();
}
public static void Mount(string json)
{
var dir = JsonConvert.DeserializeObject<Directory>(json);
Mounts.Add(dir);
}
public static void MountPersistent(string mfsFile)
{
var dir = JsonConvert.DeserializeObject<Directory>(ReadAllText(mfsFile));
Mounts.Add(dir);
string oldJson = JsonConvert.SerializeObject(dir);
var t = new Thread(new ThreadStart(() =>
{
while (Mounts != null)
{
if (oldJson != JsonConvert.SerializeObject(dir))
{
oldJson = JsonConvert.SerializeObject(dir);
WriteAllText(mfsFile, oldJson);
}
}
}));
t.IsBackground = true;
t.Start();
}
public static event Action<string> DirectoryCreated;
public static event Action<string> DirectoryDeleted;
public static event Action<string> FileWritten;
public static event Action<string> FileDeleted;
2017-06-30 11:04:27 -04:00
public static event Action<string> FileRead;
2017-01-08 09:57:10 -05:00
public static void CreateDirectory(string path)
{
if (!DirectoryExists(path))
{
string[] pathlist = path.Split('/');
int vol = Convert.ToInt32(pathlist[0].Replace(":", ""));
var dir = Mounts[vol];
for (int i = 1; i <= pathlist.Length - 2; i++)
{
dir = dir.FindDirectoryByName(pathlist[i]);
}
dir.AddDirectory(new Directory
{
Name = pathlist[pathlist.Length - 1],
2017-07-13 21:30:04 -04:00
2017-01-08 09:57:10 -05:00
});
DirectoryCreated?.Invoke(path);
2017-01-08 09:57:10 -05:00
}
else
{
throw new Exception("The directory \"" + path + "\" already exists.");
}
}
public static byte[] ReadAllBytes(string path)
{
string[] pathlist = path.Split('/');
int vol = Convert.ToInt32(pathlist[0].Replace(":", ""));
var dir = Mounts[vol];
for (int i = 1; i <= pathlist.Length - 2; i++)
{
dir = dir.FindDirectoryByName(pathlist[i]);
}
var file = dir.FindFileByName(pathlist[pathlist.Length - 1]);
2017-06-30 11:04:27 -04:00
FileRead?.Invoke(path);
2017-01-08 09:57:10 -05:00
return file.Data;
}
public static void WriteAllText(string path, string contents)
{
string[] pathlist = path.Split('/');
int vol = Convert.ToInt32(pathlist[0].Replace(":", ""));
var dir = Mounts[vol];
for (int i = 1; i <= pathlist.Length - 2; i++)
{
dir = dir.FindDirectoryByName(pathlist[i]);
}
if (!FileExists(path))
{
2017-05-02 20:52:10 -04:00
try
{
2017-07-13 21:30:04 -04:00
dir.AddFile(new File(pathlist[pathlist.Length - 1], Encoding.UTF8.GetBytes(contents), false));
2017-05-02 20:52:10 -04:00
}
catch { }
2017-01-08 09:57:10 -05:00
}
else
{
var f = dir.FindFileByName(pathlist[pathlist.Length - 1]);
f.Data = Encoding.UTF8.GetBytes(contents);
}
FileWritten?.Invoke(path);
2017-01-08 09:57:10 -05:00
}
public static void Delete(string path)
{
string[] pathlist = path.Split('/');
int vol = Convert.ToInt32(pathlist[0].Replace(":", ""));
var dir = Mounts[vol];
for (int i = 1; i <= pathlist.Length - 2; i++)
{
dir = dir.FindDirectoryByName(pathlist[i]);
}
if (FileExists(path))
{
dir.RemoveFile(pathlist[pathlist.Length - 1]);
FileDeleted?.Invoke(path);
2017-01-08 09:57:10 -05:00
}
else
{
dir.RemoveDirectory(pathlist[pathlist.Length - 1]);
DirectoryDeleted?.Invoke(path);
2017-01-08 09:57:10 -05:00
}
}
public static void WriteAllBytes(string path, byte[] contents)
{
string[] pathlist = path.Split('/');
int vol = Convert.ToInt32(pathlist[0].Replace(":", ""));
var dir = Mounts[vol];
for (int i = 1; i <= pathlist.Length - 2; i++)
{
dir = dir.FindDirectoryByName(pathlist[i]);
}
if (!FileExists(path))
{
2017-07-13 21:30:04 -04:00
dir.AddFile(new File(pathlist[pathlist.Length - 1], contents, false));
2017-01-08 09:57:10 -05:00
}
else
{
var f = dir.FindFileByName(pathlist[pathlist.Length - 1]);
f.Data = contents;
}
FileWritten?.Invoke(path);
2017-01-08 09:57:10 -05:00
}
public static string ExportMount(int index)
{
var dir = Mounts[index];
return JsonConvert.SerializeObject(dir, Formatting.Indented);
}
public static bool DirectoryExists(string path)
{
string[] pathlist = path.Split('/');
int vol = Convert.ToInt32(pathlist[0].Replace(":", ""));
2017-03-07 08:14:07 -05:00
if (Mounts[vol] == null)
Mounts[vol] = new Directory();
2017-01-08 09:57:10 -05:00
var dir = Mounts[vol];
2017-03-07 08:14:07 -05:00
2017-01-08 09:57:10 -05:00
for (int i = 1; i <= pathlist.Length - 1; i++)
{
2017-03-07 08:14:07 -05:00
dir = dir?.FindDirectoryByName(pathlist[i]);
2017-01-08 09:57:10 -05:00
}
return dir != null;
}
public static bool FileExists(string path)
{
string[] pathlist = path.Split('/');
int vol = Convert.ToInt32(pathlist[0].Replace(":", ""));
var dir = Mounts[vol];
for (int i = 1; i <= pathlist.Length - 2; i++)
{
dir = dir.FindDirectoryByName(pathlist[i]);
}
return dir.FindFileByName(pathlist[pathlist.Length - 1]) != null;
}
public static Directory GetDirectoryInfo(string path)
{
string[] pathlist = path.Split('/');
int vol = Convert.ToInt32(pathlist[0].Replace(":", ""));
var dir = Mounts[vol];
for (int i = 1; i <= pathlist.Length - 1; i++)
{
dir = dir.FindDirectoryByName(pathlist[i]);
}
if (path.EndsWith("/"))
path = path.Remove(path.Length - 1, 1);
return dir;
}
public static string ReadAllText(string path)
{
return Encoding.UTF8.GetString(ReadAllBytes(path));
}
public static File GetFileInfo(string path)
{
string[] pathlist = path.Split('/');
int vol = Convert.ToInt32(pathlist[0].Replace(":", ""));
var dir = Mounts[vol];
for (int i = 1; i <= pathlist.Length - 2; i++)
{
dir = dir.FindDirectoryByName(pathlist[i]);
}
return dir.FindFileByName(pathlist[pathlist.Length - 1]);
}
public static byte[] GetHeaderData(string filePath)
{
return GetFileInfo(filePath).HeaderData;
}
public static string GetHeaderText(string filePath)
{
byte[] header = GetHeaderData(filePath);
return (header == null) ? "" : Encoding.UTF8.GetString(header);
}
public static void SetHeaderData(string filePath, byte[] data)
{
GetFileInfo(filePath).HeaderData = data;
}
public static void SetHeaderText(string filePath, string text)
{
SetHeaderData(filePath, Encoding.UTF8.GetBytes(text));
}
2017-01-08 09:57:10 -05:00
public static string[] GetDirectories(string path)
{
string[] pathlist = path.Split('/');
int vol = Convert.ToInt32(pathlist[0].Replace(":", ""));
var dir = Mounts[vol];
for(int i = 1; i <= pathlist.Length - 1; i++)
{
dir = dir.FindDirectoryByName(pathlist[i]);
}
if (path.EndsWith("/"))
path = path.Remove(path.Length - 1, 1);
List<string> paths = new List<string>();
foreach(var subdir in dir.Subdirectories)
{
paths.Add(path + "/" + subdir.Name);
}
paths.Sort();
return paths.ToArray();
}
2017-04-21 19:42:41 -04:00
/// <summary>
/// Copies a file or directory from one path to another, deleting the original.
/// </summary>
/// <param name="path">THe input path, must be a valid directory or file.</param>
/// <param name="target">The output path.</param>
public static void Move(string path, string target)
{
if (FileExists(path))
{
WriteAllBytes(target, ReadAllBytes(path));
Delete(path);
}
else if (DirectoryExists(path))
{
if (!DirectoryExists(target))
CreateDirectory(target);
foreach (var file in GetFiles(path))
{
var name = GetFileInfo(file).Name;
Copy(file, target + "/" + name);
}
foreach (var dir in GetDirectories(path))
{
string name = GetDirectoryInfo(dir).Name;
Copy(dir, target + "/" + name);
}
Delete(path);
}
}
/// <summary>
/// Copies a file or directory from one path to another.
/// </summary>
/// <param name="path">The input path, must be a valid directory or file.</param>
/// <param name="target">The output path.</param>
public static void Copy(string path, string target)
{
if (FileExists(path))
WriteAllBytes(target, ReadAllBytes(path));
else if (DirectoryExists(path))
{
if (!DirectoryExists(target))
CreateDirectory(target);
foreach(var file in GetFiles(path))
{
var name = GetFileInfo(file).Name;
Copy(file, target + "/" + name);
}
foreach(var dir in GetDirectories(path))
{
string name = GetDirectoryInfo(dir).Name;
Copy(dir, target + "/" + name);
}
}
}
2017-01-08 09:57:10 -05:00
public static string[] GetFiles(string path)
{
string[] pathlist = path.Split('/');
int vol = Convert.ToInt32(pathlist[0].Replace(":", ""));
var dir = Mounts[vol];
for (int i = 1; i <= pathlist.Length - 1; i++)
{
dir = dir.FindDirectoryByName(pathlist[i]);
}
if (path.EndsWith("/"))
path = path.Remove(path.Length - 1, 1);
List<string> paths = new List<string>();
foreach (var subdir in dir.Files)
{
paths.Add(path + "/" + subdir.Name);
}
paths.Sort();
return paths.ToArray();
}
public static void WriteAllText(string v, object p)
{
throw new NotImplementedException();
}
}
}