Ejemplo n.º 1
0
func deleteForClientHandler(w http.ResponseWriter, r *http.Request, masterUrl string) {
	r.ParseForm()
	fids := r.Form["fid"]
	ret, err := operation.DeleteFiles(masterUrl, fids)
	if err != nil {
		writeJsonError(w, r, err)
		return
	}
	writeJsonQuiet(w, r, ret)
}
Ejemplo n.º 2
0
/*
目录元信息存储在内存中而文件的元信息则以 dirId+fileName的方式存储在levelDB中
在删除某个目录时需要每次最多100个的方式读取它的所有的子文件的fid并发送到master上
以从集群中删除
*/
func (filer *FilerEmbedded) DeleteDirectory(dirPath string, recursive bool) (err error) {
	dirId, e := filer.directories.FindDirectory(dirPath)
	if e != nil {
		return e
	}

	/*
		如果有子目录且不是递归删除则出错
	*/
	if sub_dirs, sub_err := filer.directories.ListDirectories(dirPath); sub_err == nil {
		if len(sub_dirs) > 0 && !recursive {
			return fmt.Errorf("Fail to delete directory %s: %d sub directories found!", dirPath, len(sub_dirs))
		}

		//递归删除子目录
		for _, sub := range sub_dirs {
			if delete_sub_err := filer.DeleteDirectory(filepath.Join(dirPath, sub.Name), recursive); delete_sub_err != nil {
				return delete_sub_err
			}
		}
	}

	//列出某个目录下的前100个文件
	list := filer.files.ListFiles(dirId, "", 100)
	if len(list) != 0 && !recursive {
		if !recursive {
			return fmt.Errorf("Fail to delete non-empty directory %s!", dirPath)
		}
	}

	for {
		//文件已经删空
		if len(list) == 0 {
			return filer.directories.DeleteDirectory(dirPath)
		}

		//归并文件id
		var fids []string
		for _, fileEntry := range list {
			fids = append(fids, string(fileEntry.Id))
		}

		/*
			从集群中删除文件
		*/
		if result_list, delete_file_err := operation.DeleteFiles(filer.master, fids); delete_file_err != nil {
			return delete_file_err
		} else {
			if len(result_list.Errors) > 0 {
				return errors.New(strings.Join(result_list.Errors, "\n"))
			}
		}

		//下一次读取的头
		lastFile := list[len(list)-1]

		//再读取100个 直到读完
		list = filer.files.ListFiles(dirId, lastFile.Name, 100)
	}

}