Esempio n. 1
0
func TestGetFirstLevelPath(t *testing.T) {
	type GFLPTest struct {
		basePath string
		path     string
		result   string
	}
	var tests []GFLPTest

	if filepath.Separator == '/' {
		tests = []GFLPTest{
			{"", "/usr/local/bin", "/usr"},
			{"", "/usr/local/file.txt", "/usr"},
			{"", "/usr", "/usr"},
			{"/", "/usr/local/bin", "/usr"},
			{"/usr", "/usr/local/bin", "/usr/local"},
			{"/usr", "/usr", ""},
			{"/usr", "/etc", ""},
		}
	} else {
		tests = []GFLPTest{
			{"", "D:\\usr\\local\\bin", "D:\\"},
			{"", "D:\\usr\\local\\file.txt", "D:\\"},
			{"", "D:\\", "D:\\"},
			{"D:\\", "D:\\usr\\local\\bin", "D:\\usr"},
			{"D:\\usr", "D:\\usr\\local\\bin", "D:\\usr\\local"},
			{"D:\\usr", "D:\\usr", ""},
			{"D:\\usr", "D:\\etc", ""},
		}
	}

	for _, test := range tests {
		flp := base.GetFirstLevelPath(test.basePath, test.path)
		if flp == test.result {
			t.Logf("Test passed. basePath: %v, path: %v\n", test.basePath, test.path)
		} else {
			t.Errorf("Test failed. basePath: %v, path: %v, expected: %v, got: %v\n",
				test.basePath, test.path, test.result, flp)
		}
	}
}
Esempio n. 2
0
func cmd_ArchivedList(w http.ResponseWriter, r *http.Request, plan core.BackupPlan) {
	err := indexLocalFiles(plan.Name)
	if err != nil {
		fmt.Fprintf(w, "Error occured while indexing local files of plan \"%s\": %v", plan.Name, err)
		return
	}
	localFiles := localFilesIndexByPlan[plan.Name]

	basePath := r.FormValue("basePath")
	archivedNodesMap := plan.GetArchivedNodesAllRevMap()
	workPathArchivedNodesMap := make(map[string]*NodeMetaInfoUI)
	for p, nodes := range archivedNodesMap {
		if basePath != "" && !base.IsPathInBasePath(basePath, p) {
			continue
		}

		flPath := base.GetFirstLevelPath(basePath, p)
		if flPath != "" {
			nodeUI, exists := workPathArchivedNodesMap[flPath]
			if !exists {
				nodeUI = &NodeMetaInfoUI{
					Path:  flPath,
					IsDir: nodes[0].IsDir() || flPath != p,
				}
				workPathArchivedNodesMap[flPath] = nodeUI
			}
			d, f := filepath.Split(nodeUI.Path)
			if f == "" {
				nodeUI.ShortPath = d
			} else {
				nodeUI.ShortPath = f
			}

			for i, node := range nodes {
				nodeUI.AllRevSize += node.Size()
				if i == len(nodes)-1 {
					nodeUI.LastRevSize += node.Size()
					if _, exists := localFiles[node.GetNodePath()]; !exists {
						nodeUI.LocalDeletedSize += node.Size()
					}
				}
			}
		}
	}
	workPathArchivedNodes := NodeMetaInfoUIList{}
	for _, nodeUI := range workPathArchivedNodesMap {
		workPathArchivedNodes = append(workPathArchivedNodes, nodeUI)
	}
	sort.Sort(workPathArchivedNodes)
	basePathList := []map[string]string{}

	basePathList = append(basePathList, map[string]string{
		"Path":      "",
		"ShortPath": "root",
	})
	if basePath != "" {
		parts := strings.Split(basePath, string(filepath.Separator))
		for i, part := range parts {
			if part == "" {
				continue
			}

			p := make(map[string]string)
			p["Path"] = filepath.Clean(strings.Join(parts[0:i+1], string(filepath.Separator)) + string(filepath.Separator))
			p["ShortPath"] = part
			basePathList = append(basePathList, p)
		}
	}

	tplData := struct {
		PlanName      string
		PathSeparator string
		NodesList     []*NodeMetaInfoUI
		BasePathList  []map[string]string
	}{}
	tplData.PlanName = plan.Name
	tplData.PathSeparator = string(filepath.Separator)
	tplData.NodesList = workPathArchivedNodes
	tplData.BasePathList = basePathList

	tplFuncMap := template.FuncMap{
		"filesizeHumanView": filesizeHumanView,
	}

	t, err := template.New("archived_list").Funcs(tplFuncMap).Parse(getTemplateSrc(templatesPath + "/archived_list.html"))
	if err != nil {
		fmt.Fprintf(w, "Error occured while parsing template: %v", err)
		return
	}

	err = t.Execute(w, tplData)
	if err != nil {
		fmt.Fprintf(w, "Error occured while parsing template: %v", err)
		return
	}
}