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); } } } }
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; }
public static bool IsInternal(this IElement element, VPath effectiveVPath) { using (element.Vault.ExposeReadOnly()) { return(element is IValue && (effectiveVPath == VaultBase.IdVPath || effectiveVPath == VaultBase.RevisionVPath)); } }
public IBranch GetBranch(VPath vpath) { using (Vault.ExposeReadOnly()) { return(vpath.Steps.Aggregate(this, (curr, step) => curr == null ? null : curr.Children.Branches[step])); } }
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); }
public IBranch CreateBranch(VPath vpath) { using (Vault.ExposeReadWrite()) { GetBranch(vpath).AssertNull(); return(GetOrCreateBranch(vpath)); } }
public IValue CreateValue(VPath vpath, Func <Stream> content, ValueKind valueKind) { using (Vault.ExposeReadWrite()) { GetValue(vpath).AssertNull(); return(GetOrCreateValue(vpath, content, valueKind)); } }
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()]); } }
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)); }
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())); }
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)); } } } }
/// <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))); }
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); }
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 { } }
/// <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)); }
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 }; } } })); } }
public static Value GetOrCreateValue(this Branch branch, VPath vpath, Stream content, ValueKind valueKind) { return((Value)branch.GetOrCreateValue(vpath, () => content, valueKind)); }
public static String ToFsPathFile(this VPath vpath) { return((String)vpath); }
public static String ToFsPathDir(this VPath vpath) { return((String)vpath + @"\"); }
public static TreeNode SelectNode(this TreeNode ctx, VPath vpath) { return(vpath == VPath.Empty ? ctx : SelectNode(ctx.Nodes, vpath)); }
public static String ToZipPathDir(this VPath vpath) { var s = (String)vpath; return(s.Substring(1).Replace(@"\", "/") + @"\"); }
private static TreeNode SelectNode(this TreeNodeCollection nodes, VPath vpath) { return(nodes.Cast <TreeNode>().Select(n => MatchNodeAndPath(n, vpath)).SingleOrDefault(n => n != null)); }
public static TreeNode SelectNode(this TreeView tree, VPath vpath) { return(SelectNode(tree.Nodes, vpath)); }
public IValue GetOrCreateValue(VPath vpath, Func <Stream> content) { return(((IBranch)Root).GetOrCreateValue(vpath, content)); }
public IBranch GetOrCreateBranch(VPath vpath) { return(((IBranch)Root).GetOrCreateBranch(vpath)); }
public IValue GetValue(VPath vpath) { return(((IBranch)Root).GetValue(vpath)); }
public static Value GetOrCreateValue(this Branch branch, VPath vpath, String content, ValueKind valueKind) { return((Value)branch.GetOrCreateValue(vpath, content.AsLazyStream(), valueKind)); }
public static Value GetOrCreateValue(this Branch branch, VPath vpath, ValueKind valueKind) { return((Value)branch.GetOrCreateValue(vpath, ((String)null).AsLazyStream(), valueKind)); }
IValue IBranch.CreateValue(VPath vpath, Func <Stream> content) { return(CreateValue(vpath, content, ValueKind.Regular)); }