func fetch(srpcClient *srpc.Client, hashesFilename string) error { hashesFile, err := os.Open(hashesFilename) if err != nil { return err } defer hashesFile.Close() scanner := bufio.NewScanner(hashesFile) serverAddress := fmt.Sprintf("%s:%d", *objectServerHostname, *objectServerPortNum) hashes := make([]hash.Hash, 0) for scanner.Scan() { hashval, err := objectcache.FilenameToHash(scanner.Text()) if err != nil { return err } hashes = append(hashes, hashval) } if err := scanner.Err(); err != nil { return err } return srpcClient.RequestReply("Subd.Fetch", sub.FetchRequest{ ServerAddress: serverAddress, Wait: true, Hashes: hashes}, &sub.FetchResponse{}) }
func getObjectSubcommand(objSrv objectserver.ObjectServer, args []string) { hash, err := objectcache.FilenameToHash(args[0]) if err != nil { fmt.Fprintf(os.Stderr, "Error parsing hash\t%s\n", err) os.Exit(2) } if err = getObject(objSrv, hash, args[1]); err != nil { fmt.Fprintf(os.Stderr, "Error getting object\t%s\n", err) os.Exit(2) } os.Exit(0) }
func checkObjectSubcommand(objSrv objectserver.ObjectServer, args []string) { hashes := make([]hash.Hash, 1) var err error hashes[0], err = objectcache.FilenameToHash(args[0]) if err != nil { fmt.Fprintf(os.Stderr, "Error parsing hash\t%s\n", err) os.Exit(2) } objectSizes, err := objSrv.CheckObjects(hashes) if err != nil { fmt.Fprintf(os.Stderr, "Error checking object\t%s\n", err) os.Exit(2) } if objectSizes[0] > 0 { os.Exit(0) } else { os.Exit(1) } }
func getObjects(objSrv objectserver.ObjectServer, hashesFilename, outputDirectory string) error { hashesFile, err := os.Open(hashesFilename) if err != nil { return err } defer hashesFile.Close() scanner := bufio.NewScanner(hashesFile) var hashes []hash.Hash for scanner.Scan() { hashval, err := objectcache.FilenameToHash(scanner.Text()) if err != nil { return err } hashes = append(hashes, hashval) } if err := scanner.Err(); err != nil { return err } objectsReader, err := objSrv.GetObjects(hashes) if err != nil { return err } defer objectsReader.Close() tmpDirname := outputDirectory + "~" if err := os.Mkdir(tmpDirname, syscall.S_IRWXU); err != nil { return err } defer os.RemoveAll(tmpDirname) for _, hash := range hashes { length, reader, err := objectsReader.NextObject() if err != nil { return err } err = readOne(tmpDirname, hash, length, reader) reader.Close() if err != nil { return err } } return os.Rename(tmpDirname, outputDirectory) }
func fetch(srpcClient *srpc.Client, hashesFilename string) error { hashesFile, err := os.Open(hashesFilename) if err != nil { return err } defer hashesFile.Close() scanner := bufio.NewScanner(hashesFile) var request sub.FetchRequest var reply sub.FetchResponse request.ServerAddress = fmt.Sprintf("%s:%d", *objectServerHostname, *objectServerPortNum) for scanner.Scan() { hashval, err := objectcache.FilenameToHash(scanner.Text()) if err != nil { return err } request.Hashes = append(request.Hashes, hashval) } if err := scanner.Err(); err != nil { return err } return client.CallFetch(srpcClient, request, &reply) }
func scanDirectory(objSrv *ObjectServer, baseDir string, subpath string) error { myPathName := path.Join(baseDir, subpath) file, err := os.Open(myPathName) if err != nil { return err } names, err := file.Readdirnames(-1) file.Close() if err != nil { return err } for _, name := range names { fullPathName := path.Join(myPathName, name) fi, err := os.Lstat(fullPathName) if err != nil { continue } filename := path.Join(subpath, name) if fi.IsDir() { err = scanDirectory(objSrv, baseDir, filename) if err != nil { return err } } else { if fi.Size() < 1 { return errors.New( fmt.Sprintf("zero-length file: %s", fullPathName)) } hash, err := objectcache.FilenameToHash(filename) if err != nil { return err } objSrv.sizesMap[hash] = uint64(fi.Size()) } } return nil }