Exemplo n.º 1
0
        void injectFile(FileInfo file, PboFsFolder rootDirectory, string fileFullRealPath)
        {
            if (fileTreeLookup.Contains(new PboFsLookupDummy(fileFullRealPath)))
            {
                //file from writeable directory overrides pbo file.
                Console.WriteLine("DokanPbo::LinkRealDirectory overwriting file from PBO with real file: " + fileFullRealPath);
                fileTreeLookup.Remove(new PboFsLookupDummy(fileFullRealPath));
            }

            var newFile = new PboFsRealFile(file, rootDirectory);

            rootDirectory.Children[file.Name.ToLower()] = newFile;
            this.fileTreeLookup.Add(newFile);
        }
Exemplo n.º 2
0
        public NtStatus CreateFile(string filename, FileAccess access, System.IO.FileShare share, System.IO.FileMode mode, System.IO.FileOptions options, System.IO.FileAttributes attributes, DokanFileInfo info)
        {
            var node = GetNodeFast(filename, info);

            if (node == null)
            {
                if (access == FileAccess.Delete)
                {
                    return(DokanResult.Success);                            //already gone
                }
                switch (mode)
                {
                case FileMode.CreateNew:
                case FileMode.Create:
                case FileMode.OpenOrCreate:
                    if (filename.Length == 0)
                    {
                        return(NtStatus.Success);
                    }
                    string Directory = filename;
                    if (!info.IsDirectory)     //Directory doesn't have a filename that we want to cut off
                    {
                        Directory = filename.Substring(0, filename.LastIndexOf('\\'));
                        if (Directory.Length == 0)
                        {
                            Directory = "\\";
                        }
                    }


                    var nodeDirectory = CreateOrFindDirectoryRecursive(Directory);

                    if (!(nodeDirectory is PboFsRealFolder) && nodeDirectory is PboFsFolder virtualFolder)
                    {
                        nodeDirectory = fileTree.MakeDirectoryWriteable(virtualFolder);
                    }

                    if (nodeDirectory is PboFsRealFolder folder)
                    {
                        if (info.IsDirectory)
                        {
                            info.Context = nodeDirectory;
                            //Nothing else to do as full path is already included in DirectoryPath
                        }
                        else
                        {
                            //Filename without folder path
                            var FileNameDirect = filename.Substring(filename.LastIndexOf('\\'));
                            var FileNameDirectNoLeadingSlash = filename.Substring(filename.LastIndexOf('\\') + 1);

                            FileStream newStream = null;
                            try
                            {
                                newStream = System.IO.File.Create(folder.path + FileNameDirect);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e);
                                return(DokanResult.AccessDenied);   //#TODO correct result for exception type
                            }

                            var rlFile = new PboFsRealFile(new System.IO.FileInfo(folder.path + FileNameDirect), folder, newStream);

                            folder.Children[FileNameDirectNoLeadingSlash.ToLower()] = rlFile;
                            fileTree.AddNode(rlFile);
                            info.Context = rlFile;
                        }

                        return(DokanResult.Success);
                    }

                    return(DokanResult.DiskFull);

                case FileMode.Open:
                case FileMode.Truncate:
                case FileMode.Append:
                    return(DokanResult.FileNotFound);
                }
            }

            info.Context = node;
            if (node is PboFsFolder && !info.IsDirectory)
            {
                info.IsDirectory = true;                                           //Dokan documentation says we need to do that.
            }
            if (mode == FileMode.CreateNew)
            {
                return(DokanResult.FileExists);
            }

            if (access == FileAccess.Delete)
            {
                NtStatus deleteResult = DokanResult.NotImplemented;
                if (node is PboFsRealFile)
                {
                    deleteResult = DeleteFile(filename, info);
                }
                else if (node is PboFsRealFolder)
                {
                    deleteResult = DeleteDirectory(filename, info);
                }

                return(deleteResult);
            }

            bool wantsWrite = (access &
                               (FileAccess.WriteData | FileAccess.AppendData | FileAccess.Delete | FileAccess.GenericWrite)
                               ) != 0;

            bool wantsRead = (access &
                              (FileAccess.ReadData | FileAccess.GenericRead | FileAccess.Execute | FileAccess.GenericExecute)
                              ) != 0;

            if (wantsWrite && !(node is IPboFsRealObject))
            {
                return(DokanResult.AccessDenied);
            }

            if (node is IPboFsFile file && (wantsRead || wantsWrite))
            {
                return(file.Open(wantsWrite, mode));
            }

            return(DokanResult.Success);
        }