Example #1
0
		// Upload partial file
		private IEnumerable<FilesStatus> UploadPartialFile(string fileName, HttpContext context, IFileSystem fs, SelectionUtility selection)
		{
			if (context.Request.Files.Count != 1) throw new HttpRequestValidationException("Attempt to upload chunked file containing more than one fragment per request");

			var fileUpload = context.Request.Files[0];
			var inputStream = fileUpload.InputStream;
			var virtualPath = Url.Combine(selection.SelectedItem.Url, fileName);

			if (!IsFilenameTrusted(fileName))
			{
				yield return new FilesStatus(virtualPath, 0) { error = "Unsafe filename" };
			}
			else
			{

				using (var s = fs.OpenFile(virtualPath))
				{
					var buffer = new byte[1024];

					var l = inputStream.Read(buffer, 0, 1024);
					while (l > 0)
					{
						s.Write(buffer, 0, l);
						l = inputStream.Read(buffer, 0, 1024);
					}
					s.Flush();
					s.Close();
				}
				yield return new FilesStatus(virtualPath, fileUpload.ContentLength);
			}
		}
        public void Move(IFileSystem source, FileSystemPath sourcePath, IFileSystem destination, FileSystemPath destinationPath)
        {
            bool isFile;
            if ((isFile = sourcePath.IsFile) != destinationPath.IsFile)
                throw new ArgumentException("The specified destination-path is of a different type than the source-path.");

            if (isFile)
            {
                using (var sourceStream = source.OpenFile(sourcePath, FileAccess.Read))
                {
                    using (var destinationStream = destination.CreateFile(destinationPath))
                    {
                        byte[] buffer = new byte[BufferSize];
                        int readBytes;
                        while ((readBytes = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
                            destinationStream.Write(buffer, 0, readBytes);
                    }
                }
                source.Delete(sourcePath);
            }
            else
            {
                destination.CreateDirectory(destinationPath);
                foreach (var ep in source.GetEntities(sourcePath).ToArray())
                {
                    var destinationEntityPath = ep.IsFile
                                                    ? destinationPath.AppendFile(ep.EntityName)
                                                    : destinationPath.AppendDirectory(ep.EntityName);
                    Move(source, ep, destination, destinationEntityPath);
                }
                if (!sourcePath.IsRoot)
                    source.Delete(sourcePath);
            }
        }
Example #3
0
 internal static XDocument GetDocument(IFileSystem fileSystem, string path)
 {
     using (Stream configStream = fileSystem.OpenFile(path))
     {
         return XmlUtility.LoadSafe(configStream, LoadOptions.PreserveWhitespace);
     }
 }
Example #4
0
 private static XDocument GetDocument(IFileSystem fileSystem, string path)
 {
     using (Stream configStream = fileSystem.OpenFile(path))
     {
         return XDocument.Load(configStream, LoadOptions.PreserveWhitespace);
     }
 }
Example #5
0
 private static XDocument GetDocument(IFileSystem fileSystem, string path)
 {
     using (Stream configStream = fileSystem.OpenFile(path))
     {
         return XDocument.Load(configStream);
     }
 }
Example #6
0
        public StreamFile(IFileSystem fileSystem, string path, bool readOnly)
        {
            file = fileSystem.FileExists(path) ? fileSystem.OpenFile(path, readOnly) : fileSystem.CreateFile(path);

            endPointer = file.Length;
            fileStream = new StreamFileStream(this);

            ownsFile = true;
        }
 private static string ReadDocument(IFileSystem fileSystem, string path)
 {
     string document;
     using (Stream stream = fileSystem.OpenFile(path))
     using (StreamReader sr = new StreamReader(stream))
     {
         document = sr.ReadToEnd();
     }
     return document;
 }
Example #8
0
        private Stream GetInputStream(IFileSystem fs, string url)
        {
            byte[] image;
            using (var s = fs.OpenFile(url))
            {
                image = new byte[s.Length];
                s.Read(image, 0, image.Length);
            }

            return new MemoryStream(image);
        }
Example #9
0
 internal static XDocument GetOrCreateDocument(XName rootName, IFileSystem fileSystem, string path)
 {
     if (fileSystem.FileExists(path)) {
         try {
             using (Stream configSream = fileSystem.OpenFile(path)) {
                 return XDocument.Load(configSream);
             }
         }
         catch (FileNotFoundException) {
             return CreateDocument(rootName, fileSystem, path);
         }
     }
     return CreateDocument(rootName, fileSystem, path);
 }
 private static string SafeResolveRefreshPath(IFileSystem fileSystem, string file)
 {
     string relativePath;
     try
     {
         using (var stream = fileSystem.OpenFile(file))
         {
             relativePath = stream.ReadToEnd();
         }
         return fileSystem.GetFullPath(relativePath);
     }
     catch 
     {
         // Ignore the .refresh file if it cannot be read.
     }
     return null;
 }
Example #11
0
 public Solution(IFileSystem fileSystem, string solutionFileName)
 {
     var solutionParser = _solutionParserType.GetConstructor(
         BindingFlags.Instance | BindingFlags.NonPublic, 
         binder: null, types: Type.EmptyTypes, modifiers: null).Invoke(null);
     using (var streamReader = new StreamReader(fileSystem.OpenFile(solutionFileName)))
     {
         _solutionReaderProperty.SetValue(solutionParser, streamReader, index: null);
         _parseSolutionMethod.Invoke(solutionParser, parameters: null);
     }
     var projects = new List<ProjectInSolution>();
     foreach (var proj in (object[])_projectsProperty.GetValue(solutionParser, index: null))
     {
         projects.Add(new ProjectInSolution(proj));
     }
     this.Projects = projects;
 }
Example #12
0
        public override void Intro( params object [] args )
        {
            variableTable = new VariableTable ();

            fileSystem = FileSystemManager.GetFileSystem ( "LocalFileSystem" );
            if ( fileSystem.IsFileExist ( "vartab.dat" ) )
                variableTable.Load ( fileSystem.OpenFile ( "vartab.dat" ) );
            else
            {
                variableTable.AddPackageVariableTable ( myGuid, 3 );
                variableTable.SetVariable ( myGuid, 0, 0 );
                variableTable.SetVariable ( myGuid, 1, 0.0 );
                variableTable.SetVariable ( myGuid, 2, false );
            }

            base.Intro ( args );
        }
Example #13
0
        public Logger(IFileSystem fileSystem)
        {
            //Create the StreamWriter which the logger will use for outputting to file
            _fileSystem = fileSystem;

            if (!fileSystem.FileExists(_LOG_FILE_PATH))
            {
                _logFileStream = fileSystem.CreateFile(_LOG_FILE_PATH);
            }
            else
            {
                _logFileStream = fileSystem.OpenFile(_LOG_FILE_PATH, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
            }

            _logFileStreamWriter = new StreamWriter(_logFileStream);
            //Since we are logging, set autoflush to true for immediate writes
            _logFileStreamWriter.AutoFlush = true;
            _logFileStreamWriter.WriteLine("------------ New Buttercup Session (" + DateTime.Now + ") ------------" + Environment.NewLine);
        }
Example #14
0
        public void Write(IFileSystem fs, ContentItem item, XmlTextWriter writer)
        {
            string url = item[Name] as string;
            if(!string.IsNullOrEmpty(url))
            {
                string path = url;
                if(fs.FileExists(path))
                {
                    using(ElementWriter ew = new ElementWriter("file", writer))
                    {
                        ew.WriteAttribute("url", url);

                        byte[] fileContents = ReadFully(fs.OpenFile(path));
                        string base64representation = Convert.ToBase64String(fileContents);
                        ew.Write(base64representation);
                    }
                }
            }
        }
Example #15
0
        public NcaInfo(IStorage ncaIStorage)
        {
            Nca = new Nca(Keys.Keyset, ncaIStorage);

            using (IFileSystem exefs = TryOpenFileSystemSection(NcaSectionType.Code))
            {
                if (exefs != null && exefs.FileExists("main.npdm"))
                {
                    IFile npdmFile;
                    if (exefs.OpenFile(out npdmFile, "main.npdm".ToU8String(), OpenMode.Read) == Result.Success)
                    {
                        Npdm = new NpdmBinary(npdmFile.AsStream());
                    }
                    else
                    {
                        Npdm = null;
                    }
                }
            }
        }
Example #16
0
        public Task Save(IFileSystem fileSystem, UPath savePath, SaveContext saveContext)
        {
            var fileStream = fileSystem.OpenFile(savePath, FileMode.Create, FileAccess.Write);

            // Create new APK's where files are changed
            var apkStreams = new List <(UPath, Stream)>();

            foreach (var path in _arc.ApkPaths)
            {
                var isChanged = Files.Where(x => x.FilePath.IsInDirectory(path.ToAbsolute(), true)).Any(x => x.ContentChanged);
                if (isChanged)
                {
                    apkStreams.Add((path, fileSystem.OpenFile(savePath.GetDirectory() / path.GetName(), FileMode.Create, FileAccess.Write)));
                }
            }

            _arc.Save(fileStream, apkStreams, Files);

            return(Task.CompletedTask);
        }
Example #17
0
 /// <summary>
 /// Zips the given directory
 /// </summary>
 /// <param name="inputDir">Directory to zip</param>
 /// <param name="zipFilename">Path of the target zip file</param>
 /// <param name="provider">I/O provider containing the zip file and the input directory</param>
 public static async Task ZipDir(string inputDir, string zipFilename, IFileSystem provider)
 {
     using (var archive = provider.OpenFile(zipFilename))
     {
         using (var zip = new ZipArchive(archive, ZipArchiveMode.Create))
         {
             foreach (var filename in provider.GetFiles(inputDir, "*", false))
             {
                 using (var file = provider.OpenFileReadOnly(filename))
                 {
                     var entry = zip.CreateEntry(FileSystem.MakeRelativePath(filename, inputDir), CompressionLevel.Optimal);
                     using (var entryStream = entry.Open())
                     {
                         await file.CopyToAsync(entryStream);
                     }
                 }
             }
         }
     }
 }
 /// <summary>
 ///     Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
 /// </summary>
 /// <param name="fs">The filesystem.</param>
 /// <param name="path">The path of the file to open for reading.</param>
 /// <param name="encoding">The encoding to use to decode the text from <paramref name="path" />.</param>
 /// <remarks>
 ///     This method attempts to automatically detect the encoding of a file based on the presence of byte order marks.
 ///     Encoding formats UTF-8 and UTF-32 (both big-endian and little-endian) can be detected.
 /// </remarks>
 /// <returns>An array of strings containing all lines of the file.</returns>
 public static string[] ReadAllLines(this IFileSystem fs, UPath path, Encoding encoding)
 {
     if (encoding == null)
     {
         throw new ArgumentNullException(nameof(encoding));
     }
     var stream = fs.OpenFile(path, FileMode.Open, FileAccess.Read, FileShare.Read);
     {
         using (var reader = new StreamReader(stream, encoding))
         {
             var    lines = new List <string>();
             string line;
             while ((line = reader.ReadLine()) != null)
             {
                 lines.Add(line);
             }
             return(lines.ToArray());
         }
     }
 }
Example #19
0
        private FileStreamResult GetFileStreamResult(string url)
        {
            IFileSystem fs = FileSystemProviderManager.Current.GetUnderlyingFileSystemProvider("media");

            if (!fs.FileExists(url))
            {
                throw new FileNotFoundException(url);
            }

            string           fileName = fs.GetFileName(url);
            string           mimeType = MimeMapping.GetMimeMapping(fs.GetFileName(url));
            FileStreamResult result   = new FileStreamResult(fs.OpenFile(url), mimeType);

            if (mimeType.StartsWith("application/"))
            {
                result.FileDownloadName = fileName;
            }

            return(result);
        }
        private byte[] GetImageBytesFromPath(TImage tImage, IFileSystem fs, string path)
        {
            byte[] imageBytes;
            var    fileSystem = _fileSystemProviderRepository.GetFileSystem();

            if (fileSystem != null)
            {
                if (fileSystem.Type.Contains("PhysicalFileSystem"))
                {
                    path = fs.GetRelativePath(tImage.AbsoluteUrl);
                }
            }

            using (var file = fs.OpenFile(path))
            {
                imageBytes = SolutionExtensions.ReadFully(file);
            }

            return(imageBytes);
        }
Example #21
0
        public void Move(IFileSystem source, FileSystemPath sourcePath, IFileSystem destination, FileSystemPath destinationPath)
        {
            bool isFile;

            if ((isFile = sourcePath.IsFile) != destinationPath.IsFile)
            {
                throw new ArgumentException("The specified destination-path is of a different type than the source-path.");
            }

            if (isFile)
            {
                using (var sourceStream = source.OpenFile(sourcePath, FileAccess.Read))
                {
                    using (var destinationStream = destination.CreateFile(destinationPath))
                    {
                        byte[] buffer = new byte[BufferSize];
                        int    readBytes;
                        while ((readBytes = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            destinationStream.Write(buffer, 0, readBytes);
                        }
                    }
                }
                source.Delete(sourcePath);
            }
            else
            {
                destination.CreateDirectory(destinationPath);
                foreach (var ep in source.GetEntities(sourcePath).ToArray())
                {
                    var destinationEntityPath = ep.IsFile
                                                    ? destinationPath.AppendFile(ep.EntityName)
                                                    : destinationPath.AppendDirectory(ep.EntityName);
                    Move(source, ep, destination, destinationEntityPath);
                }
                if (!sourcePath.IsRoot)
                {
                    source.Delete(sourcePath);
                }
            }
        }
Example #22
0
        public static void ExtractTickets(PartitionFileSystem pfs, string outDirPath, Keyset keyset, Output Out)
        {
            var         OutDirFs   = new LocalFileSystem(outDirPath);
            IDirectory  sourceRoot = pfs.OpenDirectory("/", OpenDirectoryMode.All);
            IDirectory  destRoot   = OutDirFs.OpenDirectory("/", OpenDirectoryMode.All);
            IFileSystem sourceFs   = sourceRoot.ParentFileSystem;
            IFileSystem destFs     = destRoot.ParentFileSystem;

            foreach (var entry in FileIterator(sourceRoot))
            {
                if (entry.Name.EndsWith(".tik") || entry.Name.EndsWith(".cert"))
                {
                    destFs.CreateFile(entry.Name, entry.Size, CreateFileOptions.None);
                    using (IFile srcFile = sourceFs.OpenFile(entry.Name, OpenMode.Read))
                        using (IFile dstFile = destFs.OpenFile(entry.Name, OpenMode.Write))
                        {
                            srcFile.CopyTo(dstFile);
                        }
                }
            }
        }
Example #23
0
        public static void ExtractTickets(Xci xci, string outDirPath, Keyset keyset, Output Out)
        {
            var         OutDirFs = new LocalFileSystem(outDirPath);
            IDirectory  destRoot = OutDirFs.OpenDirectory("/", OpenDirectoryMode.All);
            IFileSystem destFs   = destRoot.ParentFileSystem;

            foreach (var entry in FileIterator(xci, keyset, Out))
            {
                var fileName = entry.subPfsFile.Name;
                Out.Log($"{fileName}\r\n");
                if (fileName.EndsWith(".tik") || fileName.EndsWith(".cert"))
                {
                    destFs.CreateFile(fileName, entry.subPfsFile.Size, CreateFileOptions.None);
                    using (IFile srcFile = entry.subPfs.OpenFile(fileName, OpenMode.Read))
                        using (IFile dstFile = destFs.OpenFile(fileName, OpenMode.Write))
                        {
                            srcFile.CopyTo(dstFile);
                        }
                }
            }
        }
Example #24
0
        private void ReplaceFilesInAdapter(IArchiveAdapter parentAdapter, IFileSystem physicalFS, string root)
        {
            // Loop through all directories
            foreach (var dir in physicalFS.EnumerateDirectories(true))
            {
                ReplaceFilesInAdapter(parentAdapter, physicalFS.GetDirectory(dir), root);
            }

            // Update files of this directory
            foreach (var file in physicalFS.EnumerateFiles())
            {
                var openedFile = physicalFS.OpenFile(file.Remove(0, root.Length + 1));
                var afi        = parentAdapter.Files.FirstOrDefault(x => UnifyPathDelimiters(x.FileName) == UnifyPathDelimiters(file.Remove(0, root.Length + 1)));
                if (afi != null)
                {
                    afi.FileData.Dispose();
                    afi.FileData = openedFile;
                    afi.State    = ArchiveFileState.Replaced;
                }
            }
        }
Example #25
0
        // fs must contain AOC nca files in its root
        public void AddAocData(IFileSystem fs, string containerPath, ulong aocBaseId, IntegrityCheckLevel integrityCheckLevel)
        {
            _virtualFileSystem.ImportTickets(fs);

            foreach (var ncaPath in fs.EnumerateEntries("*.cnmt.nca", SearchOptions.Default))
            {
                using var ncaFile = new UniqueRef <IFile>();

                fs.OpenFile(ref ncaFile.Ref(), ncaPath.FullPath.ToU8Span(), OpenMode.Read);
                var nca = new Nca(_virtualFileSystem.KeySet, ncaFile.Get.AsStorage());
                if (nca.Header.ContentType != NcaContentType.Meta)
                {
                    Logger.Warning?.Print(LogClass.Application, $"{ncaPath} is not a valid metadata file");

                    continue;
                }

                using var pfs0     = nca.OpenFileSystem(0, integrityCheckLevel);
                using var cnmtFile = new UniqueRef <IFile>();

                pfs0.OpenFile(ref cnmtFile.Ref(), pfs0.EnumerateEntries().Single().FullPath.ToU8Span(), OpenMode.Read);

                var cnmt = new Cnmt(cnmtFile.Get.AsStream());

                if (cnmt.Type != ContentMetaType.AddOnContent || (cnmt.TitleId & 0xFFFFFFFFFFFFE000) != aocBaseId)
                {
                    continue;
                }

                string ncaId = BitConverter.ToString(cnmt.ContentEntries[0].NcaId).Replace("-", "").ToLower();
                if (!_aocData.TryAdd(cnmt.TitleId, new AocItem(containerPath, $"{ncaId}.nca", true)))
                {
                    Logger.Warning?.Print(LogClass.Application, $"Duplicate AddOnContent detected. TitleId {cnmt.TitleId:X16}");
                }
                else
                {
                    Logger.Info?.Print(LogClass.Application, $"Found AddOnContent with TitleId {cnmt.TitleId:X16}");
                }
            }
        }
Example #26
0
 public string ReadString(string filename, StorageLocation storageLocation = StorageLocation.Documents)
 {
     try {
         var stream = _fileSystem.OpenFile(filename, storageLocation);
         if (stream == null)
         {
             return(null);
         }
         using (var sr = new StreamReader(stream)) {
             var text = sr.ReadToEnd();
             return(text);
         }
     } catch (Exception) {
         return(null);
     }
 }
Example #27
0
        private void Load()
        {
            _syncLock.EnterWriteLock();
            try
            {
                if (_fileSystem.FileExists(_fileName))
                {
                    _packages.Clear();

                    try
                    {
                        using (var stream = _fileSystem.OpenFile(_fileName))
                        {
                            var deserializedPackages = _packagesSerializer.Deserialize(stream);
                            if (deserializedPackages != null)
                            {
                                _packages.AddRange(deserializedPackages);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        _log.Error("Error reading package file", ex);
                        if (ex is JsonException || ex is SerializationException)
                        {
                            // In case this happens, remove the file
                            _fileSystem.DeleteFile(_fileName);
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }
            finally
            {
                _syncLock.ExitWriteLock();
            }
        }
Example #28
0
        internal static void DeleteFileSafe(this IFileSystem fileSystem, string path, Func <Stream> streamFactory)
        {
            // Only delete the file if it exists and the checksum is the same
            if (fileSystem.FileExists(path))
            {
                bool contentEqual;
                using (Stream stream = streamFactory(),
                       fileStream = fileSystem.OpenFile(path)) {
                    contentEqual = stream.ContentEquals(fileStream);
                }

                if (contentEqual)
                {
                    fileSystem.DeleteFileSafe(path);
                }
                else
                {
                    // This package installed a file that was modified so warn the user
                    fileSystem.Logger.Log(MessageLevel.Warning, NuGetResources.Warning_FileModified, path);
                }
            }
        }
Example #29
0
        public void ConstructorAndUpdate()
        {
            DateTime     modifTime = new DateTime(2000, 1, 1);
            long         size      = 100;
            MyFileStream stm       = Substitute.For <MyFileStream>(new object());

            stm.Length.Returns(size);
            stm.IsDeleted.Returns(false);
            stm.LastWriteTime.Returns(modifTime);

            IFileSystem fs = Substitute.For <IFileSystem>();

            fs.OpenFile("test").Returns(stm);

            using (SimpleFileMedia media = new SimpleFileMedia(fs, SimpleFileMedia.CreateConnectionParamsFromFileName("test")))
            {
                Assert.AreEqual(modifTime, media.LastModified);
                Assert.AreEqual(size, media.Size);
            }

            stm.Received(1).Dispose();
        }
Example #30
0
        private void PopulateMasterpageTemplate(ITemplate template, string fileName)
        {
            string content = string.Empty;
            string path    = string.Empty;

            using (var stream = _masterpagesFileSystem.OpenFile(fileName))
            {
                byte[] bytes = new byte[stream.Length];
                stream.Position = 0;
                stream.Read(bytes, 0, (int)stream.Length);
                content = Encoding.UTF8.GetString(bytes);
            }

            template.Path       = _masterpagesFileSystem.GetRelativePath(fileName);
            template.UpdateDate = _masterpagesFileSystem.GetLastModified(path).UtcDateTime;
            //Currently set with db values, but will eventually be changed
            //template.CreateDate = _masterpagesFileSystem.GetCreated(path).UtcDateTime;
            //template.Key = new FileInfo(path).Name.EncodeAsGuid();

            template.Path    = path;
            template.Content = content;
        }
Example #31
0
        private string GetFileContents(IFileSystem fileSystem, AssetFile asset)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();
            Stream    stream    = null;

            do
            {
                try
                {
                    stream = fileSystem.OpenFile(asset.AbsolutePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                    StreamReader reader = asset.Encoding != null ? new StreamReader(stream, asset.Encoding) : new StreamReader(stream);

                    return(reader.ReadToEnd());
                }
                catch (FileNotFoundException)
                {
                    Thread.Sleep(_fileReadRetryDelay);
                }
                catch (IOException)
                {
                    Thread.Sleep(_fileReadRetryDelay);
                }
                catch (UnauthorizedAccessException)
                {
                    Thread.Sleep(_fileReadRetryDelay);
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Close();
                        stream = null;
                    }
                }
            } while (stopwatch.Elapsed < _fileReadTimeout);

            throw new ApplicationException(String.Format("The timeout of {0} was reached trying to read bundle file '{1}'.", _fileReadTimeout, asset.AbsolutePath));
        }
Example #32
0
 private static IEnumerable <Tuple <string, Stream> > OpenFiles(IFileSystem system, IEnumerable <string> files)
 {
     foreach (var file in files)
     {
         if (system.FileExists(file))
         {
             Stream stream = null;
             try
             {
                 stream = system.OpenFile(file);
             }
             catch (Exception e)
             {
                 Console.WriteLine($"Error: {e.Message}");
             }
             if (stream != null)
             {
                 yield return(new Tuple <string, Stream>(file, stream));
             }
         }
     }
 }
Example #33
0
        /// <summary>
        /// Gets the package reference files.
        /// </summary>
        /// <param name="repositoryConfig">The repository config.</param>
        /// <returns></returns>
        public IEnumerable <PackageReferenceFile> GetPackageReferenceFiles(FileInfo repositoryConfig)
        {
            var packageConfigs = new List <PackageReferenceFile>();

            //TODO OpenFile probably needs to be a little simpler....
            XDocument doc          = XDocument.Load(fileSystem.OpenFile(repositoryConfig.FullName));
            XElement  repositories = doc.Element("repositories");

            if (repositories != null)
            {
                foreach (var repository in repositories.Descendants("repository"))
                {
                    var packageconfig = (string)repository.Attribute("path") ?? "";
                    if (!string.IsNullOrEmpty(packageconfig))
                    {
                        var fullpath = Path.GetFullPath(repositoryConfig.Directory + "..\\" + packageconfig);
                        packageConfigs.Add(new PackageReferenceFile(fileSystem, fullpath));
                    }
                }
            }
            return(packageConfigs);
        }
Example #34
0
 private static IEnumerable <ArchiveFile> OpenFiles(IFileSystem system, IEnumerable <string> files)
 {
     foreach (var file in files)
     {
         if (system.FileExists(file))
         {
             Stream stream = null;
             try
             {
                 stream = system.OpenFile(file);
             }
             catch (Exception e)
             {
                 Console.Error.WriteLine($"Error: {e.Message}");
             }
             if (stream != null)
             {
                 yield return(new ArchiveFile(file, stream, system.GetLastWriteTime(file)));
             }
         }
     }
 }
Example #35
0
        public void Boot(IKernel kernel, IProcessManager processManager)
        {
            _kernel         = kernel;
            _processManager = processManager;
            _currentDrive   = kernel.Drives.First().Value;
            _myLib          = new MyLib(_kernel, _currentDrive)
            {
                CurrentDirectory = "/"
            };

            Console.WriteLine("Checking for boot.ini");

            if (_currentDrive.Exists(FileSystemPath.Parse("/system/boot.ini")))
            {
                using (Stream bootStream = _currentDrive.OpenFile(FileSystemPath.Parse("/system/boot.ini"), FileAccess.Read))
                    using (StreamReader streamReader = new StreamReader(bootStream))
                    {
                        ExecuteCommand("cd system");

                        while (!streamReader.EndOfStream)
                        {
                            ExecuteCommand(streamReader.ReadLine());
                        }

                        ExecuteCommand("cd ..");
                    }
            }
            else
            {
                Console.WriteLine("No boot.ini file found, moving on");
            }

            while (true)
            {
                Console.Write("#" + _myLib.CurrentDirectory + "> ");
                ExecuteCommand(Console.ReadLine());
            }
        }
        public void Write(IFileSystem fs, ContentItem item, XmlTextWriter writer)
        {
            string url = item[Name] as string;

            if (!string.IsNullOrEmpty(url))
            {
                string path = url;
                if (fs.FileExists(path))
                {
                    using (ElementWriter ew = new ElementWriter("file", writer))
                    {
                        ew.WriteAttribute("url", url);

                        using (var s = fs.OpenFile(path))
                        {
                            byte[] fileContents         = ReadFully(s);
                            string base64representation = Convert.ToBase64String(fileContents);
                            ew.Write(base64representation);
                        }
                    }
                }
            }
        }
Example #37
0
        private static bool ShallowFileCompare(IFileSystem srcFileSystem, IFileSystem destFileSystem, string srcFilePath, string destFilePath)
        {
            var srcFileInfo  = srcFileSystem.GetFileInfo(srcFilePath);
            var destFileInfo = destFileSystem.GetFileInfo(destFilePath);

            var srcFileLength = srcFileInfo.Length;

            if (srcFileInfo.Attributes.HasFlag(FileAttributes.ReparsePoint))
            {
                using (var srcFileStream = srcFileSystem.OpenFile(srcFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    srcFileLength = srcFileStream.Length;
                }
            }

            // Different lengths -> not equal
            if (srcFileLength != destFileInfo.Length)
            {
                return(false);
            }

            return(true);
        }
Example #38
0
        public void IFileWrite_CanReadBackWrittenData()
        {
            var data = new byte[] { 7, 4, 1, 0, 8, 5, 2, 9, 6, 3 };

            IFileSystem fs = CreateFileSystem();

            fs.CreateFile("/file".ToU8Span(), data.Length, CreateFileOptions.None);

            fs.OpenFile(out IFile file, "/file".ToU8Span(), OpenMode.Write);
            file.Write(0, data, WriteOption.None);
            file.Dispose();

            var readData = new byte[data.Length];

            fs.OpenFile(out file, "/file".ToU8Span(), OpenMode.Read);
            using (file)
            {
                Assert.True(file.Read(out long bytesRead, 0, readData, ReadOption.None).IsSuccess());
                Assert.Equal(data.Length, bytesRead);
            }

            Assert.Equal(data, readData);
        }
Example #39
0
 public static void Encrypt(IFileSystem sourceFs, IFileSystem destFs, bool verifyEncrypted, Keyset keyset,
                            Output Out)
 {
     foreach (var decryptedNcaEntry in sourceFs.EnumerateEntries().Where(item => item.Name.EndsWith(".nca")))
     {
         Out.Log($"Input: {decryptedNcaEntry.Name}\r\n");
         using (var decryptedNca = sourceFs.OpenFile(decryptedNcaEntry.FullPath, OpenMode.Read))
         {
             if (destFs != null)
             {
                 Out.Log("Opened NCA for writing...\r\n");
                 using (IFile outputFile = FolderTools.CreateAndOpen(decryptedNcaEntry, destFs, decryptedNcaEntry.Name, decryptedNca.GetSize()))
                 {
                     EncryptFunct(decryptedNca.AsStream(), outputFile.AsStream(), decryptedNcaEntry.Name, verifyEncrypted, keyset, Out);
                 }
             }
             else
             {
                 EncryptFunct(decryptedNca.AsStream(), null, decryptedNcaEntry.Name, verifyEncrypted, keyset, Out);
             }
         }
     }
 }
Example #40
0
        public void ExceptionInConstructorMustNotLeakStreams()
        {
            MyFileStream stm = Substitute.For <MyFileStream>(new object());
            Exception    ex  = new TestException();

            stm.Length.Returns(callInfo => { throw ex; });
            stm.IsDeleted.Returns(callInfo => { throw ex; });
            stm.LastWriteTime.Returns(callInfo => { throw ex; });

            IFileSystem fs = Substitute.For <IFileSystem>();

            fs.OpenFile("test").Returns(stm);

            try
            {
                (new SimpleFileMedia(fs, SimpleFileMedia.CreateConnectionParamsFromFileName("test"))).Dispose();
            }
            catch (TestException)
            {
            }

            stm.Received(1).Dispose();
        }
Example #41
0
        public static SemanticVersion?GetVersionFromMsBuildFile(UPath versionFile, IFileSystem fileSystem, ILogger logger)
        {
            try
            {
                using var stream = fileSystem.OpenFile(versionFile, FileMode.Open, FileAccess.Read);

                var document = XDocument.Load(stream);

                var project = document.Element(XName.Get("Project"));

                if (project is null)
                {
                    throw new InvalidOperationException($"Could not find project in file {versionFile.FullName}");
                }

                var propertyGroups = project.Elements(XName.Get("PropertyGroup"));

                string?versionValue = propertyGroups.Elements(XName.Get("Version")).FirstOrDefault()?.Value;

                if (string.IsNullOrWhiteSpace(versionValue))
                {
                    throw new InvalidOperationException($"Could not find a version property in file {versionFile.FullName}");
                }

                if (!SemanticVersion.TryParse(versionValue, out var version))
                {
                    throw new InvalidOperationException($"Could not parse '{versionValue}' as a valid semantic version");
                }

                return(version);
            }
            catch (Exception ex) when(!ex.IsFatal())
            {
                logger.Error(ex, "Could not get version from file {VersionFile}", versionFile);
                return(null);
            }
        }
        private XDocument GetStoreDocument(bool createIfNotExists = false)
        {
            try
            {
                // If the file exists then open and return it
                if (_storeFileSystem.FileExists(StoreFilePath))
                {
                    using (Stream stream = _storeFileSystem.OpenFile(StoreFilePath))
                    {
                        try
                        {
                            return(XDocument.Load(stream));
                        }
                        catch (XmlException)
                        {
                            // There was an error reading the file, but don't throw as a result
                        }
                    }
                }

                // If it doesn't exist and we're creating a new file then return a
                // document with an empty packages node
                if (createIfNotExists)
                {
                    return(new XDocument(new XElement("repositories")));
                }

                return(null);
            }
            catch (Exception e)
            {
                throw new InvalidOperationException(
                          String.Format(CultureInfo.CurrentCulture,
                                        NuGetResources.ErrorReadingFile,
                                        _storeFileSystem.GetFullPath(StoreFilePath)), e);
            }
        }
Example #43
0
        // Upload partial file
        private IEnumerable <FilesStatus> UploadPartialFile(string fileName, HttpContext context, IFileSystem fs, SelectionUtility selection)
        {
            if (context.Request.Files.Count != 1)
            {
                throw new HttpRequestValidationException("Attempt to upload chunked file containing more than one fragment per request");
            }

            var fileUpload  = context.Request.Files[0];
            var inputStream = fileUpload.InputStream;
            var virtualPath = Url.Combine(selection.SelectedItem.Url, fileName);

            if (!IsFilenameTrusted(fileName))
            {
                yield return(new FilesStatus(virtualPath, 0)
                {
                    error = "Unsafe filename"
                });
            }
            else
            {
                using (var s = fs.OpenFile(virtualPath))
                {
                    var buffer = new byte[1024];

                    var l = inputStream.Read(buffer, 0, 1024);
                    while (l > 0)
                    {
                        s.Write(buffer, 0, l);
                        l = inputStream.Read(buffer, 0, 1024);
                    }
                    s.Flush();
                    s.Close();
                }
                yield return(new FilesStatus(virtualPath, fileUpload.ContentLength));
            }
        }
Example #44
0
        internal static void AddReferencesToConfig(IFileSystem fileSystem, string references)
        {
            var       webConfigPath = Path.Combine(fileSystem.Root, "web.config");
            XDocument document;

            // Read the web.config file from the AppRoot if it exists.
            if (fileSystem.FileExists(webConfigPath))
            {
                using (Stream stream = fileSystem.OpenFile(webConfigPath))
                {
                    document = XDocument.Load(stream, LoadOptions.PreserveWhitespace);
                }
            }
            else
            {
                document = new XDocument(new XElement("configuration"));
            }

            var assemblies = GetOrCreateChild(document.Root, "system.web/compilation/assemblies");

            // Get the name of the existing references
            // References are stored in the format <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            bool existingAssembly = (
                from item in assemblies.Elements()
                where !String.IsNullOrEmpty(item.GetOptionalAttributeValue("assembly"))
                let assemblyName = new AssemblyName(item.Attribute("assembly").Value).Name
                                   where String.Equals(assemblyName, references, StringComparison.OrdinalIgnoreCase)
                                   select item
                ).Any();

            if (!existingAssembly)
            {
                assemblies.Add(new XElement("add", new XAttribute("assembly", references)));
                SaveDocument(fileSystem, webConfigPath, document);
            }
        }
 private static FrameworkName LoadSupportedFramework(IFileSystem fileSystem, string frameworkFile)
 {
     using (Stream stream = fileSystem.OpenFile(frameworkFile))
     {
         return LoadSupportedFramework(stream);
     }
 }
Example #46
0
        internal static void AddReferencesToConfig(IFileSystem fileSystem, string references)
        {
            var webConfigPath = Path.Combine(fileSystem.Root, "web.config");
            XDocument document;

            // Read the web.config file from the AppRoot if it exists.
            if (fileSystem.FileExists(webConfigPath))
            {
                using (Stream stream = fileSystem.OpenFile(webConfigPath))
                {
                    document = XDocument.Load(stream, LoadOptions.PreserveWhitespace);
                }
            }
            else
            {
                document = new XDocument(new XElement("configuration"));
            }

            var assemblies = GetOrCreateChild(document.Root, "system.web/compilation/assemblies");

            // Get the name of the existing references 
            // References are stored in the format <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            bool existingAssembly = (from item in assemblies.Elements()
                                     where !String.IsNullOrEmpty(item.GetOptionalAttributeValue("assembly"))
                                     let assemblyName = new AssemblyName(item.Attribute("assembly").Value).Name
                                     where String.Equals(assemblyName, references, StringComparison.OrdinalIgnoreCase)
                                     select item).Any();

            if (!existingAssembly)
            {
                assemblies.Add(new XElement("add", new XAttribute("assembly", references)));
                SaveDocument(fileSystem, webConfigPath, document);
            }
        }
Example #47
0
 private static void EnsureFileExists(IFileSystem fileSystem, string configFilePath)
 {
     using (fileSystem.OpenFile(configFilePath))
     {
         // Do nothing
     }
 }
Example #48
0
 private static PackageReferenceFile GetPackageReferenceFile(IFileSystem fileSystem, string configFilePath)
 {
     // By default the PackageReferenceFile does not throw if the file does not exist at the specified path.
     // We'll try reading from the file so that the file system throws a file not found
     using (fileSystem.OpenFile(configFilePath))
     {
         // Do nothing
     }
     return new PackageReferenceFile(fileSystem, Path.GetFullPath(configFilePath));
 }
        internal static List<AssemblyName> GetFrameworkAssemblies(IFileSystem fileSystemFrameworkListFile)
        {
            List<AssemblyName> frameworkAssemblies = new List<AssemblyName>();
            try
            {
                if (fileSystemFrameworkListFile.FileExists(FrameworkListFileName))
                {
                    using (Stream stream = fileSystemFrameworkListFile.OpenFile(FrameworkListFileName))
                    {                        
                        var document = XmlUtility.LoadSafe(stream);
                        var root = document.Root;
                        if (root.Name.LocalName.Equals("FileList", StringComparison.OrdinalIgnoreCase))
                        {
                            foreach (var element in root.Elements("File"))
                            {
                                string simpleAssemblyName = element.GetOptionalAttributeValue("AssemblyName");
                                string version = element.GetOptionalAttributeValue("Version");
                                if (simpleAssemblyName == null || version == null)
                                {
                                    // Skip this file. Return an empty list
                                    // Clear frameworkAssemblies since we don't want partial results
                                    frameworkAssemblies.Clear();
                                    break;
                                }
                                else
                                {
                                    AssemblyName assemblyName = new AssemblyName();
                                    assemblyName.Name = simpleAssemblyName;
                                    assemblyName.Version = new Version(version);
                                    frameworkAssemblies.Add(assemblyName);
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
            }

            return frameworkAssemblies;
        }
Example #50
0
        private void CopyNativeBinaries(IProjectSystem projectSystem, IFileSystem packagesFileSystem, PackageName packageName)
        {
            const string nativeBinariesFolder = "NativeBinaries";

            string nativeBinariesPath = Path.Combine(packageName.Name, nativeBinariesFolder);
            if (packagesFileSystem.DirectoryExists(nativeBinariesPath))
            {
                IEnumerable<string> nativeFiles = packagesFileSystem.GetFiles(nativeBinariesPath, "*.*", recursive: true);
                foreach (string file in nativeFiles)
                {
                    string targetPath = Path.Combine(Constants.BinDirectory, file.Substring(nativeBinariesPath.Length + 1));  // skip over NativeBinaries/ word
                    using (Stream stream = packagesFileSystem.OpenFile(file)) 
                    {
                        projectSystem.AddFile(targetPath, stream);
                    }
                }
            }
        }
		private Tuple<int, int> GetDimensions(string path, IFileSystem fs)
		{

			int fileWidth;
			int fileHeight;
			using (var stream = fs.OpenFile(path))
			{
				using (var image = Image.FromStream(stream))
				{
					fileWidth = image.Width;
					fileHeight = image.Height;
				}
			}

			return new Tuple<int, int>(fileWidth, fileHeight);
		}
Example #52
0
        private string GetFileContents(IFileSystem fileSystem, AssetFile asset)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();
            Stream stream = null;

            do
            {
                try
                {
                    stream = fileSystem.OpenFile(asset.AbsolutePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                    StreamReader reader = asset.Encoding != null ? new StreamReader(stream, asset.Encoding) : new StreamReader(stream);

                    return reader.ReadToEnd();
                }
                catch (FileNotFoundException)
                {
                    Thread.Sleep(_fileReadRetryDelay);
                }
                catch (IOException)
                {
                    Thread.Sleep(_fileReadRetryDelay);
                }
                catch (UnauthorizedAccessException)
                {
                    Thread.Sleep(_fileReadRetryDelay);
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Close();
                        stream = null;
                    }
                }
            } while (stopwatch.Elapsed < _fileReadTimeout);

            throw new ApplicationException(String.Format("The timeout of {0} was reached trying to read bundle file '{1}'.", _fileReadTimeout, asset.AbsolutePath));
        }
Example #53
0
        public static PackageInfo LoadFromFileSystem( IFileSystem fileSystem )
        {
            if ( !fileSystem.IsFileExist ( "packageInfo.json" ) )
                throw new ArgumentException ();

            PackageInfo packageInfo = new PackageInfo ();
            JsonEntry entry = JsonParser.Parse ( fileSystem.OpenFile ( "packageInfo.json" ) );

            packageInfo.PackageName = entry [ "title" ] as string;

            packageInfo.Author = entry [ "author" ] as string;
            packageInfo.Copyright = entry [ "copyright" ] as string;
            packageInfo.Description = entry [ "description" ] as string;

            packageInfo.PackageID = new Guid ( entry [ "packId" ] as string );
            packageInfo.Version = new Version ( entry [ "version" ] as string );
            packageInfo.ReleaseDate = DateTime.Parse ( entry [ "release_date" ] as string );

            if ( packageInfo.IsSubPackage = ( bool ) entry [ "issubpack" ] )
            {
                List<Guid> mainGuid = new List<Guid> ();
                JsonArray mainPackIds = entry [ "mainpacks" ] as JsonArray;
                foreach ( object item in mainPackIds )
                    mainGuid.Add ( new Guid ( item as string ) );

                if ( !mainGuid.Contains ( PackageSystem.MainPackage.PackageID ) )
                {
                    bool isContains = false;
                    foreach ( PackageInfo subpack in PackageSystem.SubPackages )
                        if ( mainGuid.Contains ( subpack.PackageID ) )
                            isContains = true;
                    if ( !isContains )
                        throw new SubPackageNotAllowedThisPackageException ();
                }

                packageInfo.MainPackageIDs = mainGuid.ToArray ();
            }

            if ( fileSystem.IsFileExist ( "packageCover.png" ) )
                packageInfo.PackageCover = new PngDecoder ().Decode ( fileSystem.OpenFile ( "packageCover.png" ) );

            if ( fileSystem.IsFileExist ( "stringTable.stt" ) )
                packageInfo.StringTable = new StringTable ( fileSystem.OpenFile ( "stringTable.stt" ) );

            if ( fileSystem.IsFileExist ( "resourceTable.rst" ) )
                packageInfo.ResourceTable = new ContentManager ( new ZipFileSystem ( fileSystem.OpenFile ( "resourceTable.rst" ) ) );

            if ( fileSystem.IsFileExist ( "scriptTable.scr" ) )
                packageInfo.ScriptTable = new ScriptTable ( fileSystem.OpenFile ( "scriptTable.scr" ) );

            return packageInfo;
        }
Example #54
0
 public static bool ContentEqual(IFileSystem fileSystem, string path, Func<Stream> streamFactory)
 {
     using (Stream stream = streamFactory(),
         fileStream = fileSystem.OpenFile(path))
     {
         return stream.ContentEquals(fileStream);
     }
 }
 internal static void ForceRecompile(IFileSystem fileSystem, string binDirectory)
 {
     var fileToWrite = Path.Combine(binDirectory, ForceRecompilationFile);
     try
     {
         // Note: We should use BuildManager::ForceRecompile once that method makes it into System.Web.
         using (var writer = new StreamWriter(fileSystem.OpenFile(fileToWrite)))
         {
             writer.WriteLine();
         }
     }
     catch
     {
     }
 }
Example #56
0
        public static PackageInfo LoadFromFileSystem( IFileSystem fileSystem )
        {
            if ( !fileSystem.IsFileExist ( "packageInfo.json" ) )
                throw new ArgumentException ();

            PackageInfo packageInfo = new PackageInfo ();
            JsonContainer entry = new JsonContainer ( fileSystem.OpenFile ( "packageInfo.json" ) );

            packageInfo.PackageName = entry [ "title" ] as string;

            packageInfo.Author = entry [ "author" ] as string;
            packageInfo.Copyright = entry [ "copyright" ] as string;
            packageInfo.Description = entry [ "description" ] as string;

            packageInfo.PackageID = new Guid ( entry [ "packId" ] as string );
            packageInfo.Version = new Version ( entry [ "version" ] as string );
            packageInfo.ReleaseDate = DateTime.Parse ( entry [ "release_date" ] as string );

            if ( packageInfo.IsSubPackage = ( bool ) entry [ "issubpack" ] )
            {
                List<Guid> mainGuid = new List<Guid> ();
                JsonContainer mainPackIds = entry [ "mainpacks" ] as JsonContainer;
                foreach ( object item in mainPackIds.GetListEnumerable () )
                    mainGuid.Add ( new Guid ( item as string ) );

                if ( !mainGuid.Contains ( Core.MainPackage.PackageID ) )
                {
                    bool isContains = false;
                    foreach ( PackageInfo subpack in Core.SubPackages )
                        if ( mainGuid.Contains ( subpack.PackageID ) )
                            isContains = true;
                    if ( !isContains )
                        throw new ArgumentException ( "This package is not allowed to this package." );
                }

                packageInfo.MainPackageIDs = mainGuid.ToArray ();
            }

            if ( fileSystem.IsFileExist ( "packageCover.png" ) )
            {
                ImageInfo imageInfo;
                new PngDecoder ().Decode ( fileSystem.OpenFile ( "packageCover.png" ), out imageInfo );
                packageInfo.PackageCover = imageInfo;
            }

            if ( fileSystem.IsFileExist ( "stringTable.stt" ) )
                packageInfo.StringTable = new StringTable ( fileSystem.OpenFile ( "stringTable.stt" ) );

            if ( fileSystem.IsFileExist ( "resourceTable.rst" ) )
                packageInfo.ResourceTable = new ResourceTable ( new ZipFileSystem ( fileSystem.OpenFile ( "resourceTable.rst" ) ) );

            return packageInfo;
        }