Esempio n. 1
0
func processFile(dbh *leveldb.DB, processingCh <-chan *fileEntry, move bool) {
	var fent *fileEntry
	more := true

	for more {
		select {
		case fent, more = <-processingCh:
			if more {

				if _, err := dbh.Get(fent.csum, nil); err == db.ErrNotFound {
					// file not processed yet
					if move {
						DEBUG("moving file %s to %s\n", fent.srcPath, fent.dstPath)

						if err = os.MkdirAll(filepath.Dir(fent.dstPath), (os.ModeDir | 0750)); err != nil {
							fmt.Printf("%s\n", err)
							continue
						}

						if err = os.Rename(fent.srcPath, fent.dstPath); err != nil {
							fmt.Printf("error: renaming file %s to %s failed. trying to copy instead.\n", fent.srcPath, fent.dstPath)
							if err = Copy(fent.srcPath, fent.dstPath); err != nil {
								fmt.Printf("error: Couldn't copy file %s to %s: %s\n", fent.srcPath, fent.dstPath, err)
								continue
							}
						}
					} else {
						DEBUG("copying file %s to %s\n", fent.srcPath, fent.dstPath)
						if err = Copy(fent.srcPath, fent.dstPath); err != nil {
							fmt.Printf("error: Couldn't copy file %s to %s: %s\n", fent.srcPath, fent.dstPath, err)
							continue
						}
					}

					dbh.Set(fent.csum, []byte(fent.srcPath), nil)
				} else if err == nil {
					DEBUG("skipping file %s since it already exists.\n", fent.srcPath)
				} else {
					fmt.Printf("error: Unable to check whether %s exists: %s. Skipping.\n", fent.srcPath, err)
					continue
				}
			}
		}
	}
}