/// <summary> /// Removes the mapping's prefix from the specified path. /// </summary> /// <param name="aPath">The path to remove the prefix from.</param> /// <returns>The path without the prefix.</returns> public FOS_System.String RemoveMappingPrefix(FOS_System.String aPath) { return aPath.Substring(prefix.length, aPath.length - prefix.length); }
/// <summary> /// Copies the specified file. /// </summary> /// <param name="srcFile">The file to copy.</param> /// <param name="dst">The path to copy to.</param> private void CopyFile(File srcFile, FOS_System.String dst) { //If source file is null, it means it wasn't found by the caller (or some // other error but we will assume not found since that is the expected use // case from the other overload of CopyFile). if(srcFile == null) { console.WriteLine("Source file not found!"); return; } //Attempt to open the destination file. File dstFile = File.Open(dst); //If opening failed, the file was not found so either the path // was invalid or the file does not currently exist. We assume // the latter. If the path is invalid, it will be caught later. if (dstFile == null) { //console.WriteLine("Creating destination file..."); //If the path is invalid because of path mapping, it will be // caught here. FileSystemMapping mapping = FileSystemManager.GetMapping(dst); if (mapping == null) { console.WriteLine("Destination file system not found!"); return; } //+1 to include the slash in dir name int lastIdx = dst.LastIndexOf(FileSystemManager.PathDelimiter) + 1; FOS_System.String dstDir = dst.Substring(0, lastIdx); FOS_System.String dstName = dst.Substring(lastIdx, dst.length - lastIdx); //console.WriteLine("dstDir: " + dstDir); //console.WriteLine("dstName: " + dstName); //New directory either creates the directory, or returns the existing directory Directory parentDir = NewDirectory(dstDir); //If the parent dir is null, the path must be invalid so we cannot create the // dest file. if (parentDir != null) { dstFile = mapping.TheFileSystem.NewFile(dstName, parentDir); } //If we still failed to create the file, then the path was totally invalid if (dstFile == null) { console.WriteLine("Failed to create destination file!"); return; } console.WriteLine("Destination file created."); } else { console.WriteLine("Destination file already exists."); } //Get full path resolves the path of the file without using short-hands // such as ./ and .. which can be used in the arguments to this method. // So, GetFullPath allows us to do a consistent comparison of the paths. FOS_System.String srcFullPath = srcFile.GetFullPath(); FOS_System.String dstFullPath = dstFile.GetFullPath(); //If we are about to copy a file onto itself, well that wouldn't technically // give us an issue given our copy implementation, but it is pretty pointless. // Also, it would give a more sofisticated copy algorithm (e.g. block copying // for large files) a big problem! if (srcFullPath == dstFullPath) { console.WriteLine("Atempted to copy a file to itself! (" + srcFullPath + ")"); return; } else { console.WriteLine("Copying " + srcFullPath + " to " + dstFullPath); } //Get the streams to read from / write to FOS_System.IO.Streams.FileStream srcStr = srcFile.GetStream(); FOS_System.IO.Streams.FileStream dstStr = dstFile.GetStream(); //Temporary storage. Note: If the file is to big, this will just fail // as there won't be enough heap memory available byte[] data = new byte[(uint)srcFile.TheFileSystem.ThePartition.BlockSize]; //Force stream positions srcStr.Position = 0; dstStr.Position = 0; console.Write("["); int percentile = (int)(uint)FOS_System.Math.Divide(srcFile.Size, 78u); int dist = 0; while ((ulong)srcStr.Position < srcFile.Size) { //Read in source data. srcStr.Read(data, 0, data.Length); //Write out data to destination. dstStr.Write(data, 0, data.Length); dist += data.Length; if (dist >= percentile) { console.Write("."); dist -= percentile; } } console.WriteLine("]"); console.WriteLine("Copied successfully."); }
/// <summary> /// Creates a new directory (and parent directories). Used recursively. /// </summary> /// <param name="path">The full path of the directory (and parent directories) to create.</param> /// <returns>The new (or existing) directory.</returns> private Directory NewDirectory(FOS_System.String path) { //Output info to the user. console.WriteLine("Searching for directory: " + path); //Attempt to find the directory. If it already exists, we don't want to // accidentally re-create it! Directory theDir = Directory.Find(path); //If the directory does not exist: if (theDir == null) { //Output info to the user. console.WriteLine("Creating directory..."); //Attempt to get the file system mapping for the new directory FileSystemMapping mapping = FileSystemManager.GetMapping(path); //If the mapping was found: if(mapping != null) { //Remove trailing "/" if there is one else the code below would end // up with a blank "new directory name" if (path.EndsWith(FileSystemManager.PathDelimiter)) { path = path.Substring(0, path.length - 1); } // + 1 as we wish to include the path delimeter in parent dir name and // not in the new dir name. // Note: It is important to include the path delimeter at the end of the parent dir name // as the parent dir name may be a FS root which requires the trailing path delimeter. int lastIdx = path.LastIndexOf(FileSystemManager.PathDelimiter) + 1; FOS_System.String dirParentPath = path.Substring(0, lastIdx); FOS_System.String newDirName = path.Substring(lastIdx, path.length - lastIdx); console.WriteLine("Checking parent path: " + dirParentPath); //This causes NewDirectory to become a recursive, self-calling // method which could potentially overflow. However, if has // the benefit that the entire new directory tree can be created // in one call, rather than having to create each directory and // sub-directory one at a time. Directory parentDir = NewDirectory(dirParentPath); if (parentDir != null) { console.WriteLine("New dir name: " + newDirName); //Create the directory theDir = mapping.TheFileSystem.NewDirectory(newDirName, parentDir); console.WriteLine("Directory created."); } else { console.WriteLine("Failed to find or create parent directory."); } } else { console.WriteLine("File system mapping not found."); } } else { console.WriteLine("Directory already exists."); } return theDir; }
/// <summary> /// Gets the short name for the specified long name. /// </summary> /// <param name="longName">The long name to shorten.</param> /// <param name="isDirectory">Whether the long name is for a directory or not.</param> /// <returns>The short name parts. Directory=1 part, file=2 parts (name + extension).</returns> private static List GetShortName(FOS_System.String longName, bool isDirectory) { if (isDirectory) { List result = new List(1); result.Add(longName.Substring(0, 8).PadRight(11, ' ')); return result; } else { List result = new List(2); List nameParts = longName.Split('.'); if (nameParts.Count > 1) { result.Add(((FOS_System.String)nameParts[0]).Substring(0, 8).PadRight(8, ' ')); result.Add(((FOS_System.String)nameParts[1]).Substring(0, 3).PadRight(3, ' ')); } else { result.Add(longName.Substring(0, 8).PadRight(8, ' ')); result.Add(((FOS_System.String)"").PadRight(3, ' ')); } return result; } }