Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FileSystemHelper.cs « FtpServer - github.com/ClusterM/hakchi2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 36cc4979447f40390a1e8c685c29f8e0a04811fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;

namespace mooftpserv
{
    public class FileSystemHelper
    {
        /// Handles TVFS path resolution, similar to Path.GetFullPath(Path.Combine(basePath, path))
        public static string ResolvePath(string basePath, string path)
        {
            // CF is missing String.IsNullOrWhiteSpace
            if (path == null || path.Trim() == "")
                return null;

            // first, make a complete unix path
            string fullPath;
            if (path[0] == '/') {
                fullPath = path;
            } else {
                fullPath = basePath;
                if (!fullPath.EndsWith("/"))
                    fullPath += "/";
                fullPath += path;
            }

            // then, remove ".." and "."
            List<string> tokens = new List<string>(fullPath.Split('/'));
            for (int i = 0; i < tokens.Count; ++i) {
                if (tokens[i] == "") {
                    if (i == 0 || i == tokens.Count - 1) {
                        continue; // ignore, start and end should be empty tokens
                    } else {
                        tokens.RemoveAt(i);
                        --i;
                    }
                } else if (tokens[i] == "..") {
                    if (i < 2) {
                        // cannot go higher than root, just remove the token
                        tokens.RemoveAt(i);
                        --i;
                    } else {
                        tokens.RemoveRange(i - 1, 2);
                        i -= 2;
                    }
                } else if (i < tokens.Count - 1 && tokens[i].EndsWith(@"\")) {
                    int slashes = 0;
                    for (int c = tokens[i].Length - 1; c >= 0 && tokens[i][c] == '\\'; --c)
                        ++slashes;

                    if (slashes % 2 != 0) {
                        // the slash was actually escaped, merge tokens
                        tokens[i] += ("/" + tokens[i + 1]);
                        ++i;
                    }
                }
            }

            if (tokens.Count > 1)
                return String.Join("/", tokens.ToArray());
            else
                return "/";
        }

    }
}