Esempio n. 1
0
func mkpath(dstres string) error {
	var nodes []*gomega.Node
	var node *gomega.Node

	root, pathsplit, err := getLookupParams(fmt.Sprintf("%s:/%s", MEGA_ROOT, dstres), mega.FS)
	if err != nil {
		return err
	}
	if len(*pathsplit) > 0 {
		nodes, err = mega.FS.PathLookup(root, *pathsplit)
	} else {
		return nil
	}

	lp := len(*pathsplit)
	ln := len(nodes)

	if len(nodes) > 0 {
		node = nodes[ln-1]
	} else {
		node = root
	}

	switch {
	case err == nil:
		if node.GetType() != gomega.FOLDER {
			return ENOT_DIRECTORY
		}
		return nil
	case err == gomega.ENOENT:
		remaining := lp - ln
		for i := 0; i < remaining; i++ {
			name := (*pathsplit)[ln]
			node, err = mega.CreateDir(name, node)
			if err != nil {
				return err
			}
			ln += 1
		}
		err = nil

	default:
		return err
	}

	return nil
}
Esempio n. 2
0
func transfer_retrieve(key string, file string) bool {
	var nodes []*gomega.Node
	var node *gomega.Node
	fullpath := getFullPath(key, getDirHash(key))

	root, pathsplit, err := getLookupParams(fullpath, mega.FS)
	if err != nil {
		fmt.Printf("TRANSFER-FAILURE RETRIEVE %s %s\n", key, err)
		return false
	}

	if len(*pathsplit) > 0 {
		nodes, err = mega.FS.PathLookup(root, *pathsplit)
	} else {
		err = EINVALID_PATH
	}

	if err != nil {
		fmt.Printf("TRANSFER-FAILURE RETRIEVE %s %s\n", key, err)
		return false
	} else {
		node = nodes[len(nodes)-1]
		if node.GetType() != gomega.FILE {
			fmt.Printf("TRANSFER-FAILURE RETRIEVE %s %s\n", key, ENOT_FILE)
			return false
		}
	}

	info, err := os.Stat(file)
	if os.IsNotExist(err) {
		d := path.Dir(file)
		_, err := os.Stat(d)
		if os.IsNotExist(err) {
			fmt.Printf("TRANSFER-FAILURE RETRIEVE %s %s\n", key, EINVALID_DEST)
			return false
		}
	} else {
		if info.Mode().IsDir() {
			if strings.HasSuffix(file, "/") {
				file = path.Join(file, (*pathsplit)[len(*pathsplit)-1])
			} else {
				fmt.Printf("TRANSFER-FAILURE RETRIEVE %s %s\n", key, EDIR_EXISTS)
				return false
			}
		}

		info, err = os.Stat(file)
		if os.IsNotExist(err) == false {
			if info.Size() == node.GetSize() {
				fmt.Printf("TRANSFER-SUCCESS RETRIEVE %s\n", key)
				return true
			}

			os.Remove(file)

			if err != nil {
				fmt.Printf("TRANSFER-FAILURE RETRIEVE %s %s\n", key, EFILE_EXISTS)
				return false
			}
		}
		err = nil
	}

	var ch *chan int
	var wg sync.WaitGroup
	ch = new(chan int)
	*ch = make(chan int)
	wg.Add(1)
	go progressBar(*ch, &wg, node.GetSize())

	err = mega.DownloadFile(node, file, ch)
	wg.Wait()
	if err != nil {
		fmt.Printf("TRANSFER-FAILURE RETRIEVE %s %s\n", key, err)
		return false
	}

	fmt.Printf("TRANSFER-SUCCESS RETRIEVE %s\n", key)
	return true
}