Example #1
0
        public IValue GetOrCreateValue(VPath vpath, Func <Stream> content, ValueKind valueKind)
        {
            using (Vault.ExposeReadOnly())
            {
                var steps  = vpath.Steps.ToArray();
                var parent = (Branch)GetOrCreateBranch(steps.Take(steps.Length - 1).ToArray());

                var regularValue = (valueKind & ValueKind.Regular) != 0 ?
                                   parent.Children.Values[steps.Last()] : null;
                var internalValue = (valueKind & ValueKind.Internal) != 0 ?
                                    parent.Children.InternalValues[steps.Last()] : null;
                var value = regularValue ?? internalValue;

                if (value != null)
                {
                    return(value);
                }
                else
                {
                    using (Vault.ExposeReadWrite())
                    {
                        var newValue = new Value(Vault, steps.Last(), null);
                        newValue.Parent = parent;
                        newValue.SetContent(content);
                        return(newValue);
                    }
                }
            }
        }
Example #2
0
        public static void Delete(FtpServer server, string file = null, string directory = null)
        {
            string sname = VPath.RemoveInvalidPathChars(server.Config.ServerName);
            //string dir = Path.Combine(sname, "Config");
            string dir = directory == null?Path.Combine(sname, "Config") : Path.Combine(VPath.RemoveInvalidPathChars(directory), sname, "Config");

            if (!Directory.Exists(dir))
            {
                return;
            }
            else
            {
                if (file != null)
                {
                    File.Delete(Path.Combine(dir, file));
                }
                else
                {
                    File.Delete(Path.Combine(dir, "users.cfg"));
                    File.Delete(Path.Combine(dir, "usergroups.cfg"));
                    File.Delete(Path.Combine(dir, "config.cfg"));
                }
            }
            return;
        }
Example #3
0
 public static bool IsInternal(this IElement element, VPath effectiveVPath)
 {
     using (element.Vault.ExposeReadOnly())
     {
         return(element is IValue && (effectiveVPath == VaultBase.IdVPath || effectiveVPath == VaultBase.RevisionVPath));
     }
 }
Example #4
0
 public IBranch GetBranch(VPath vpath)
 {
     using (Vault.ExposeReadOnly())
     {
         return(vpath.Steps.Aggregate(this, (curr, step) => curr == null ? null : curr.Children.Branches[step]));
     }
 }
Example #5
0
        private static bool Save(FtpServer server, string file, string directory = null)
        {
            string sname = VPath.RemoveInvalidPathChars(server.Config.ServerName);
            string dir   = directory == null?Path.Combine(sname, "Config") : Path.Combine(VPath.RemoveInvalidPathChars(directory), sname, "Config");

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            using (FileStream fs = new FileStream(Path.Combine(dir, file), FileMode.Create))
            {
                switch (file)
                {
                case "users.cfg":
                    serializer.Serialize(fs, server.Users);
                    break;

                case "usergroups.cfg":
                    serializer.Serialize(fs, server.UserGroups);
                    break;

                case "config.cfg":
                    serializer.Serialize(fs, server.Config);
                    break;

                default:
                    fs.Close();
                    return(false);
                }
                //serializer.Serialize(fs, server.Users);
                fs.Close();
            }
            return(true);
        }
Example #6
0
 public IBranch CreateBranch(VPath vpath)
 {
     using (Vault.ExposeReadWrite())
     {
         GetBranch(vpath).AssertNull();
         return(GetOrCreateBranch(vpath));
     }
 }
Example #7
0
 public IValue CreateValue(VPath vpath, Func <Stream> content, ValueKind valueKind)
 {
     using (Vault.ExposeReadWrite())
     {
         GetValue(vpath).AssertNull();
         return(GetOrCreateValue(vpath, content, valueKind));
     }
 }
Example #8
0
 public IValue GetValue(VPath vpath)
 {
     using (Vault.ExposeReadOnly())
     {
         var steps  = vpath.Steps.ToArray();
         var parent = (Branch)GetBranch(steps.Take(steps.Length - 1).ToArray());
         return(parent == null ? null : parent.Children.Values[steps.Last()]);
     }
 }
Example #9
0
        public static String ToZipPathFile(this VPath vpath)
        {
            var s = (String)vpath;

            s = s.Substring(1).Replace(@"\", "/");
            if (!s.Contains("/"))
            {
                s = "/" + s;
            }
            return(s);
        }
        public byte[] ReadAllBytes(string filepath)
        {
            if (Settings.DownstreamProvider != null)
            {
                return(Settings.DownstreamProvider.ReadAllBytes(filepath));
            }

            var pathPart = VPath.GetPathPart(filepath);
            var fullPath = Path.Combine(Settings.BasePath, pathPart);

            return(File.ReadAllBytes(fullPath));
        }
Example #11
0
 private static TreeNode MatchNodeAndPath(this TreeNode ctx, VPath vpath)
 {
     if (ctx == null || vpath == VPath.Empty)
     {
         return(null);
     }
     if (ctx.Text != vpath.Steps.First())
     {
         return(null);
     }
     if (vpath.Steps.Count() == 1)
     {
         return(ctx);
     }
     return(SelectNode(ctx.Nodes, vpath.Steps.Skip(1).ToArray()));
 }
Example #12
0
        internal void VerifyMutation(VPath effectiveVPath)
        {
            int exposedRO, exposedRW;

            using (Vault.ExposeReadOnly(out exposedRO, out exposedRW))
            {
                int exposedInternal;
                using (Vault.InternalExpose(out exposedInternal))
                {
                    // mutation is never allowed in unexposed state
                    // exposition might be either regular (most public ops) or internal (ctor/save)
                    if (exposedRW != 0 || exposedInternal != 0)
                    {
                        // mutation is then allowed for non-internal elements
                        var mutationAprioriAllowed = !this.IsInternal(effectiveVPath);
                        if (mutationAprioriAllowed)
                        {
                            return;
                        }

                        // for internal values mutation is only allowed when it
                        // comes during construction and/or saving of different kinds
                        if (exposedInternal != 0)
                        {
                            return;
                        }

                        // otherwise the mutation is illegal
                        throw new NotSupportedException(String.Format(
                                                            "Completing this operation would mutate a value at effective vpath '{0}'. " +
                                                            "This is prohibited because this vpath is reserved by the vault for internal purposes.",
                                                            effectiveVPath));
                    }
                    else
                    {
                        // mutation is never allowed in unexposed state
                        throw new NotSupportedException(String.Format(
                                                            "Completing this operation would mutate the node '{0}'. " +
                                                            "This is prohibited because mutations are never allowed in unexposed state.",
                                                            effectiveVPath));
                    }
                }
            }
        }
Example #13
0
        /// <summary>
        /// MKD Command - RFC 959 - Section 4.1.3
        /// <para>创建文件夹</para>
        /// </summary>
        /// <param name="pathname"></param>
        /// <returns></returns>
        private Response CreateDir(string pathname)
        {
            DirectoryInfo di;
            FileError     result = _virtualFileSystem.CreateDirectory(pathname, out di);

            if (result == FileError.AlreadyExist)
            {
                return(GetResponse(FtpResponses.DIRECTORY_EXISTS));
            }
            if (result == FileError.NotFound)
            {
                return(GetResponse(FtpResponses.DIRECTORY_NOT_FOUND));
            }

            var name = di != null ? di.Name : VPath.GetFileName(pathname);

            _virtualFileSystem.RefreshCurrentDirectory();
            return(GetResponse(FtpResponses.MAKE_DIRECTORY_SUCCESS.SetData(name)));
        }
Example #14
0
        private static bool Load(FtpServer server, string file, string directory = null)
        {
            string sname = VPath.RemoveInvalidPathChars(server.Config.ServerName);
            string p     = directory == null?Path.Combine(sname, "Config", file) : Path.Combine(VPath.RemoveInvalidPathChars(directory), sname, "Config", file);

            if (File.Exists(p))
            {
                using (FileStream fs = new FileStream(p, FileMode.Open))
                {
                    try
                    {
                        switch (file)
                        {
                        case "users.cfg":
                            server.Users = serializer.Deserialize(fs) as Dictionary <string, FtpUser> ?? server.Users;
                            break;

                        case "usergroups.cfg":
                            server.UserGroups = serializer.Deserialize(fs) as Dictionary <string, FtpUserGroup> ??
                                                server.UserGroups;
                            break;

                        case "config.cfg":
                            server.Config = serializer.Deserialize(fs) as FtpConfig ?? server.Config;
                            break;

                        default:
                            fs.Close();
                            return(false);
                        }
                    }
                    catch (SerializationException)
                    {
                    }
                    //server.Users = serializer.Deserialize(fs) as Dictionary<string, FtpUser> ?? server.Users;
                    fs.Close();
                    return(true);
                }
            }
            return(false);
        }
Example #15
0
        public FsVaultEntry(DirectoryInfo root, FileSystemInfo fsItem)
        {
            Root = root;

            FsItem = fsItem;
            FsItem.Exists.AssertTrue();
            FsItem.FullName.StartsWith(Root.FullName).AssertTrue();
            Streams = new List <FileStream>();

            _absPathCached = FsItem.FullName;
            _relPathCached = _absPathCached.Substring(Root.FullName.Length);
            if (FsItem is DirectoryInfo && !_relPathCached.EndsWith(@"\"))
            {
                _relPathCached += @"\";
            }
            if (!_relPathCached.StartsWith(@"\"))
            {
                _relPathCached = @"\" + _relPathCached;
            }
            try { _vpathCached = new VPath(_relPathCached.Unbux()); } catch { }
        }
Example #16
0
        /// <summary>
        /// 上传操作
        /// </summary>
        /// <param name="dataStream"></param>
        /// <param name="pathname"></param>
        /// <returns></returns>
        private Response StoreOperation(Stream dataStream, string pathname)
        {
            long bytes = 0;

            FtpLogEntry logEntry = new FtpLogEntry
            {
                Date       = DateTime.Now,
                CIP        = ClientIP,
                CSMethod   = "STOR",
                CSUsername = _username,
                SCStatus   = "226",
            };

            if (VPath.ContainsInvalidPathChars(pathname))
            {
                pathname = VPath.RemoveInvalidPathChars(pathname);
            }
            using (FileStream fs = new FileStream(pathname, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read, BUFFER_SIZE, FileOptions.SequentialScan))
            {
                //if (_lastCommand.Code == "REST")
                //{
                //    fs.Seek(_transPosition, SeekOrigin.Begin);
                //}
                fs.Seek(_transPosition, SeekOrigin.Begin);
                bytes = CopyStream(dataStream, fs, _performanceCounter.IncrementBytesReceived);
            }

            logEntry.CSBytes = bytes.ToString(CultureInfo.InvariantCulture);

            _log.Info(logEntry);
            OnLog(logEntry);

            _performanceCounter.IncrementFilesReceived();

            _virtualFileSystem.RefreshCurrentDirectory();

            return(GetResponse(FtpResponses.TRANSFER_SUCCESSFUL));
        }
Example #17
0
 public IBranch GetOrCreateBranch(VPath vpath)
 {
     using (Vault.ExposeReadOnly())
     {
         return(vpath.Steps.Aggregate(this, (curr, step) => {
             var next = curr.Children.Branches[step];
             if (next != null)
             {
                 return next;
             }
             else
             {
                 using (Vault.ExposeReadWrite())
                 {
                     return new Branch(Vault, step, null)
                     {
                         Parent = curr
                     };
                 }
             }
         }));
     }
 }
Example #18
0
 public static Value GetOrCreateValue(this Branch branch, VPath vpath, Stream content, ValueKind valueKind)
 {
     return((Value)branch.GetOrCreateValue(vpath, () => content, valueKind));
 }
Example #19
0
 public static String ToFsPathFile(this VPath vpath)
 {
     return((String)vpath);
 }
Example #20
0
 public static String ToFsPathDir(this VPath vpath)
 {
     return((String)vpath + @"\");
 }
Example #21
0
 public static TreeNode SelectNode(this TreeNode ctx, VPath vpath)
 {
     return(vpath == VPath.Empty ? ctx : SelectNode(ctx.Nodes, vpath));
 }
Example #22
0
        public static String ToZipPathDir(this VPath vpath)
        {
            var s = (String)vpath;

            return(s.Substring(1).Replace(@"\", "/") + @"\");
        }
Example #23
0
 private static TreeNode SelectNode(this TreeNodeCollection nodes, VPath vpath)
 {
     return(nodes.Cast <TreeNode>().Select(n => MatchNodeAndPath(n, vpath)).SingleOrDefault(n => n != null));
 }
Example #24
0
 public static TreeNode SelectNode(this TreeView tree, VPath vpath)
 {
     return(SelectNode(tree.Nodes, vpath));
 }
Example #25
0
 public IValue GetOrCreateValue(VPath vpath, Func <Stream> content)
 {
     return(((IBranch)Root).GetOrCreateValue(vpath, content));
 }
Example #26
0
 public IBranch GetOrCreateBranch(VPath vpath)
 {
     return(((IBranch)Root).GetOrCreateBranch(vpath));
 }
Example #27
0
 public IValue GetValue(VPath vpath)
 {
     return(((IBranch)Root).GetValue(vpath));
 }
Example #28
0
 public static Value GetOrCreateValue(this Branch branch, VPath vpath, String content, ValueKind valueKind)
 {
     return((Value)branch.GetOrCreateValue(vpath, content.AsLazyStream(), valueKind));
 }
Example #29
0
 public static Value GetOrCreateValue(this Branch branch, VPath vpath, ValueKind valueKind)
 {
     return((Value)branch.GetOrCreateValue(vpath, ((String)null).AsLazyStream(), valueKind));
 }
Example #30
0
 IValue IBranch.CreateValue(VPath vpath, Func <Stream> content)
 {
     return(CreateValue(vpath, content, ValueKind.Regular));
 }