Example #1
0
func testCreateDir(fs storage.Directory, path, referencePath string) error {
	dir, err := fs.Mkdir(path)

	if err != nil {
		return errors.New(fmt.Sprintf("error creating directory: %v", err))
	}

	if actualPath := dir.Entry().Path(); actualPath != referencePath {
		return errors.New(fmt.Sprintf("new directory has wrong path; expected %s, got %s", referencePath, actualPath))
	}

	err = dir.Close()

	if err != nil {
		return errors.New(fmt.Sprintf("failed to close newly created directory: %v", err))
	}

	entry, err := fs.Stat(path)

	if err != nil {
		return errors.New(fmt.Sprintf("failed to stat new directory: %v", err))
	}

	if _, ok := entry.(storage.DirectoryEntry); !ok {
		return errors.New("new directory failed to stat as a directory")
	}

	if actualPath := entry.Path(); actualPath != referencePath {
		return errors.New(fmt.Sprintf("new directory has wrong path after statting; expected %s, got %s", referencePath, actualPath))
	}

	return nil
}
Example #2
0
func checkDirectoryContents(directory storage.Directory, expectedContents []string) (entries map[string]storage.Entry, e error) {
	entries = make(map[string]storage.Entry)
	e = nil

	contents := make([]string, 0, 10)

	for entry, err := directory.NextEntry(); entry != nil; entry, err = directory.NextEntry() {
		if err != nil {
			e = errors.New(fmt.Sprintf("error while iterating over directory entries: %v", err))
			return
		}

		contents = append(contents, entry.Name())
		entries[entry.Name()] = entry
	}

	if len(contents) != len(expectedContents) {
		e = errors.New(fmt.Sprintf("expected %d dir entries, got %d instead", len(expectedContents), len(contents)))
		return
	}

	sort.Sort(sort.StringSlice(contents))
	sort.Sort(sort.StringSlice(expectedContents))

	for idx, filename := range contents {
		if filename != expectedContents[idx] {
			e = errors.New(fmt.Sprintf("directory listing differs at %d: expected %s, got %s", idx, expectedContents[idx], filename))
			return
		}
	}

	return
}
Example #3
0
func (sync *Unidirectional) syncDir(entry storage.DirectoryEntry) error {
	path := entry.Path()

	var (
		targetEntry    storage.Entry
		targetDirEntry storage.DirectoryEntry
		targetDir      storage.Directory
		err            error
		ok             bool
	)

	targetEntry, err = sync.to.Stat(path)

	if err != nil {
		return err
	}

	if targetDirEntry, ok = targetEntry.(storage.DirectoryEntry); !ok && targetEntry != nil {
		if err := targetEntry.Remove(); err != nil {
			return err
		}

		targetDirEntry = nil
	}

	if targetDirEntry != nil {
		return nil
	}

	sync.events <- &EventStartSyncing{entry: entry}

	targetDir, err = sync.to.Mkdir(path)

	if err != nil {
		return err
	}

	defer func() {
		sync.setLastErrorIfApplicable(targetDir.Close())
	}()

	return nil
}
Example #4
0
func testCreateFile(target storage.Directory, path, referencePath string) error {
	file, err := target.CreateFile(path)

	if err != nil {
		return errors.New(fmt.Sprintf("creating file failed: %v", err))
	}

	fileClosed := false
	defer func() {
		if !fileClosed {
			if err := file.Close(); err != nil {
				panic(err)
			}
		}
	}()

	bytesWritten, err := file.Write([]byte("Hello world"))

	if err != nil {
		return errors.New(fmt.Sprintf("failed to write to file: %v", err))
	}

	if bytesWritten != 11 {
		return errors.New(fmt.Sprintf("failed to write the full buffer, wrote %d bytes", bytesWritten))
	}

	if actualPath := file.Entry().Path(); actualPath != referencePath {
		return errors.New(fmt.Sprintf("new file has wrong path: expected %s, got %s", referencePath, actualPath))
	}

	err = file.Close()
	if err != nil {
		return errors.New(fmt.Sprintf("error closing newly created file: %v", err))
	}
	fileClosed = true

	fileEntry, err := target.Stat(path)

	if err != nil {
		return errors.New(fmt.Sprintf("failed to stat newly created file: %v", err))
	}

	if actualPath := fileEntry.Path(); actualPath != referencePath {
		return errors.New(fmt.Sprintf("new file has wrong path after statting: expected %s, got %s", referencePath, actualPath))
	}

	fileRO, err := fileEntry.(storage.FileEntry).Open()

	if err != nil {
		return errors.New(fmt.Sprintf("failed to  opeen new file: %v", err))
	}

	defer func() {
		if err := fileRO.Close(); err != nil {
			panic(err)
		}
	}()

	buffer := make([]byte, 20)

	bytesRead, err := fileRO.Read(buffer)

	if err != nil && err != io.EOF {
		return errors.New(fmt.Sprintf("failed to read from newly created file: %v", err))
	}

	if bytesRead != 11 || string(buffer[:11]) != "Hello world" {
		return errors.New(fmt.Sprintf("invalid contents in written file: %v", string(buffer[:bytesRead+2])))
	}

	return nil
}