func ldbWithAllRepo(db *leveldb.DB, repo []byte, fn func(node []byte, f protocol.FileInfo) bool) { defer runtime.GC() start := nodeKey(repo, nil, nil) // before all repo/node files limit := nodeKey(repo, protocol.LocalNodeID[:], []byte{0xff, 0xff, 0xff, 0xff}) // after all repo/node files snap, err := db.GetSnapshot() if err != nil { panic(err) } defer snap.Release() dbi := snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil) defer dbi.Release() for dbi.Next() { node := nodeKeyNode(dbi.Key()) var f protocol.FileInfo err := f.UnmarshalXDR(dbi.Value()) if err != nil { panic(err) } if cont := fn(node, f); !cont { return } } }
func ldbReplaceWithDelete(db *leveldb.DB, repo, node []byte, fs []protocol.FileInfo) uint64 { return ldbGenericReplace(db, repo, node, fs, func(db dbReader, batch dbWriter, repo, node, name []byte, dbi iterator.Iterator) uint64 { var tf protocol.FileInfoTruncated err := tf.UnmarshalXDR(dbi.Value()) if err != nil { panic(err) } if !tf.IsDeleted() { if debug { l.Debugf("mark deleted; repo=%q node=%v name=%q", repo, protocol.NodeIDFromBytes(node), name) } ts := clock(tf.LocalVersion) f := protocol.FileInfo{ Name: tf.Name, Version: lamport.Default.Tick(tf.Version), LocalVersion: ts, Flags: tf.Flags | protocol.FlagDeleted, Modified: tf.Modified, } batch.Put(dbi.Key(), f.MarshalXDR()) ldbUpdateGlobal(db, batch, repo, node, nodeKeyName(dbi.Key()), f.Version) return ts } return 0 }) }
func unmarshalTrunc(bs []byte, truncate bool) (protocol.FileIntf, error) { if truncate { var tf protocol.FileInfoTruncated err := tf.UnmarshalXDR(bs) return tf, err } else { var tf protocol.FileInfo err := tf.UnmarshalXDR(bs) return tf, err } }
func (m *Model) updateLocal(repo string, f protocol.FileInfo) { f.LocalVersion = 0 m.rmut.RLock() m.repoFiles[repo].Update(protocol.LocalNodeID, []protocol.FileInfo{f}) m.rmut.RUnlock() events.Default.Log(events.LocalIndexUpdated, map[string]interface{}{ "repo": repo, "name": f.Name, "modified": time.Unix(f.Modified, 0), "flags": fmt.Sprintf("0%o", f.Flags), "size": f.Size(), }) }
func ldbInsert(batch dbWriter, repo, node, name []byte, file protocol.FileInfo) uint64 { if debug { l.Debugf("insert; repo=%q node=%v %v", repo, protocol.NodeIDFromBytes(node), file) } if file.LocalVersion == 0 { file.LocalVersion = clock(0) } nk := nodeKey(repo, node, name) batch.Put(nk, file.MarshalXDR()) return file.LocalVersion }
func sizeOfFile(f protocol.FileInfo) (files, deleted int, bytes int64) { if !protocol.IsDeleted(f.Flags) { files++ if !protocol.IsDirectory(f.Flags) { bytes += f.Size() } else { bytes += zeroEntrySize } } else { deleted++ bytes += zeroEntrySize } return }
func ldbGet(db *leveldb.DB, repo, node, file []byte) protocol.FileInfo { nk := nodeKey(repo, node, file) bs, err := db.Get(nk, nil) if err == leveldb.ErrNotFound { return protocol.FileInfo{} } if err != nil { panic(err) } var f protocol.FileInfo err = f.UnmarshalXDR(bs) if err != nil { panic(err) } return f }
func ldbUpdate(db *leveldb.DB, repo, node []byte, fs []protocol.FileInfo) uint64 { defer runtime.GC() batch := new(leveldb.Batch) snap, err := db.GetSnapshot() if err != nil { panic(err) } defer snap.Release() var maxLocalVer uint64 for _, f := range fs { name := []byte(f.Name) fk := nodeKey(repo, node, name) bs, err := snap.Get(fk, nil) if err == leveldb.ErrNotFound { if lv := ldbInsert(batch, repo, node, name, f); lv > maxLocalVer { maxLocalVer = lv } ldbUpdateGlobal(snap, batch, repo, node, name, f.Version) continue } var ef protocol.FileInfo err = ef.UnmarshalXDR(bs) if err != nil { panic(err) } if ef.Version != f.Version { if lv := ldbInsert(batch, repo, node, name, f); lv > maxLocalVer { maxLocalVer = lv } ldbUpdateGlobal(snap, batch, repo, node, name, f.Version) } } err = db.Write(batch, nil) if err != nil { panic(err) } return maxLocalVer }
func ldbWithGlobal(db *leveldb.DB, repo []byte, fn fileIterator) { defer runtime.GC() start := globalKey(repo, nil) limit := globalKey(repo, []byte{0xff, 0xff, 0xff, 0xff}) snap, err := db.GetSnapshot() if err != nil { panic(err) } defer snap.Release() dbi := snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil) defer dbi.Release() for dbi.Next() { var vl versionList err := vl.UnmarshalXDR(dbi.Value()) if err != nil { panic(err) } if len(vl.versions) == 0 { l.Debugln(dbi.Key()) panic("no versions?") } fk := nodeKey(repo, vl.versions[0].node, globalKeyName(dbi.Key())) bs, err := snap.Get(fk, nil) if err != nil { panic(err) } var f protocol.FileInfo err = f.UnmarshalXDR(bs) if err != nil { panic(err) } if cont := fn(f); !cont { return } } }
func ldbGetGlobal(db *leveldb.DB, repo, file []byte) protocol.FileInfo { k := globalKey(repo, file) snap, err := db.GetSnapshot() if err != nil { panic(err) } defer snap.Release() bs, err := snap.Get(k, nil) if err == leveldb.ErrNotFound { return protocol.FileInfo{} } if err != nil { panic(err) } var vl versionList err = vl.UnmarshalXDR(bs) if err != nil { panic(err) } if len(vl.versions) == 0 { l.Debugln(k) panic("no versions?") } k = nodeKey(repo, vl.versions[0].node, file) bs, err = snap.Get(k, nil) if err != nil { panic(err) } var f protocol.FileInfo err = f.UnmarshalXDR(bs) if err != nil { panic(err) } return f }
func ldbWithHave(db *leveldb.DB, repo, node []byte, fn fileIterator) { start := nodeKey(repo, node, nil) // before all repo/node files limit := nodeKey(repo, node, []byte{0xff, 0xff, 0xff, 0xff}) // after all repo/node files snap, err := db.GetSnapshot() if err != nil { panic(err) } defer snap.Release() dbi := snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil) defer dbi.Release() for dbi.Next() { var f protocol.FileInfo err := f.UnmarshalXDR(dbi.Value()) if err != nil { panic(err) } if cont := fn(f); !cont { return } } }
func ldbReplaceWithDelete(db *leveldb.DB, repo, node []byte, fs []protocol.FileInfo) uint64 { return ldbGenericReplace(db, repo, node, fs, func(db dbReader, batch dbWriter, repo, node, name []byte, dbi iterator.Iterator) uint64 { var f protocol.FileInfo err := f.UnmarshalXDR(dbi.Value()) if err != nil { panic(err) } if !protocol.IsDeleted(f.Flags) { if debug { l.Debugf("mark deleted; repo=%q node=%v name=%q", repo, protocol.NodeIDFromBytes(node), name) } ts := clock(f.LocalVersion) f.Blocks = nil f.Version = lamport.Default.Tick(f.Version) f.Flags |= protocol.FlagDeleted f.LocalVersion = ts batch.Put(dbi.Key(), f.MarshalXDR()) ldbUpdateGlobal(db, batch, repo, node, nodeKeyName(dbi.Key()), f.Version) return ts } return 0 }) }
func (p *puller) closeFile(f protocol.FileInfo) { if debug { l.Debugf("pull: closing %q / %q", p.repoCfg.ID, f.Name) } of := p.openFiles[f.Name] err := of.file.Close() if err != nil { p.errors++ l.Infof("close: error: %q / %q: %v", p.repoCfg.ID, f.Name, err) } defer os.Remove(of.temp) delete(p.openFiles, f.Name) fd, err := os.Open(of.temp) if err != nil { p.errors++ l.Infof("open: error: %q / %q: %v", p.repoCfg.ID, f.Name, err) return } hb, _ := scanner.Blocks(fd, scanner.StandardBlockSize, f.Size()) fd.Close() if l0, l1 := len(hb), len(f.Blocks); l0 != l1 { if debug { l.Debugf("pull: %q / %q: nblocks %d != %d", p.repoCfg.ID, f.Name, l0, l1) } return } for i := range hb { if bytes.Compare(hb[i].Hash, f.Blocks[i].Hash) != 0 { if debug { l.Debugf("pull: %q / %q: block %d hash mismatch\n have: %x\n want: %x", p.repoCfg.ID, f.Name, i, hb[i].Hash, f.Blocks[i].Hash) } return } } t := time.Unix(f.Modified, 0) err = os.Chtimes(of.temp, t, t) if err != nil { l.Infof("chtimes: error: %q / %q: %v", p.repoCfg.ID, f.Name, err) } if !p.repoCfg.IgnorePerms && protocol.HasPermissionBits(f.Flags) { err = os.Chmod(of.temp, os.FileMode(f.Flags&0777)) if err != nil { l.Infof("chmod: error: %q / %q: %v", p.repoCfg.ID, f.Name, err) } } osutil.ShowFile(of.temp) if p.versioner != nil { err := p.versioner.Archive(of.filepath) if err != nil { if debug { l.Debugf("pull: error: %q / %q: %v", p.repoCfg.ID, f.Name, err) } return } } if debug { l.Debugf("pull: rename %q / %q: %q", p.repoCfg.ID, f.Name, of.filepath) } if err := osutil.Rename(of.temp, of.filepath); err == nil { p.model.updateLocal(p.repoCfg.ID, f) } else { p.errors++ l.Infof("rename: error: %q / %q: %v", p.repoCfg.ID, f.Name, err) } }
func ldbWithNeed(db *leveldb.DB, repo, node []byte, fn fileIterator) { start := globalKey(repo, nil) limit := globalKey(repo, []byte{0xff, 0xff, 0xff, 0xff}) snap, err := db.GetSnapshot() if err != nil { panic(err) } defer snap.Release() dbi := snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil) defer dbi.Release() for dbi.Next() { var vl versionList err := vl.UnmarshalXDR(dbi.Value()) if err != nil { panic(err) } if len(vl.versions) == 0 { l.Debugln(dbi.Key()) panic("no versions?") } have := false // If we have the file, any version need := false // If we have a lower version of the file var haveVersion uint64 for _, v := range vl.versions { if bytes.Compare(v.node, node) == 0 { have = true haveVersion = v.version need = v.version < vl.versions[0].version break } } if need || !have { name := globalKeyName(dbi.Key()) fk := nodeKey(repo, vl.versions[0].node, name) bs, err := snap.Get(fk, nil) if err != nil { panic(err) } var gf protocol.FileInfo err = gf.UnmarshalXDR(bs) if err != nil { panic(err) } if protocol.IsDeleted(gf.Flags) && !have { // We don't need deleted files that we don't have continue } if debug { l.Debugf("need repo=%q node=%v name=%q need=%v have=%v haveV=%d globalV=%d", repo, protocol.NodeIDFromBytes(node), name, need, have, haveVersion, vl.versions[0].version) } if cont := fn(gf); !cont { return } } } }
func ldbGenericReplace(db *leveldb.DB, repo, node []byte, fs []protocol.FileInfo, deleteFn deletionHandler) uint64 { sort.Sort(fileList(fs)) // sort list on name, same as on disk start := nodeKey(repo, node, nil) // before all repo/node files limit := nodeKey(repo, node, []byte{0xff, 0xff, 0xff, 0xff}) // after all repo/node files batch := new(leveldb.Batch) snap, err := db.GetSnapshot() if err != nil { panic(err) } defer snap.Release() dbi := snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil) defer dbi.Release() moreDb := dbi.Next() fsi := 0 var maxLocalVer uint64 for { var newName, oldName []byte moreFs := fsi < len(fs) if !moreDb && !moreFs { break } if !moreFs && deleteFn == nil { // We don't have any more updated files to process and deletion // has not been requested, so we can exit early break } if moreFs { newName = []byte(fs[fsi].Name) } if moreDb { oldName = nodeKeyName(dbi.Key()) } cmp := bytes.Compare(newName, oldName) if debug { l.Debugf("generic replace; repo=%q node=%v moreFs=%v moreDb=%v cmp=%d newName=%q oldName=%q", repo, protocol.NodeIDFromBytes(node), moreFs, moreDb, cmp, newName, oldName) } switch { case moreFs && (!moreDb || cmp == -1): // Disk is missing this file. Insert it. if lv := ldbInsert(batch, repo, node, newName, fs[fsi]); lv > maxLocalVer { maxLocalVer = lv } ldbUpdateGlobal(snap, batch, repo, node, newName, fs[fsi].Version) fsi++ case moreFs && moreDb && cmp == 0: // File exists on both sides - compare versions. var ef protocol.FileInfo ef.UnmarshalXDR(dbi.Value()) if fs[fsi].Version > ef.Version { if lv := ldbInsert(batch, repo, node, newName, fs[fsi]); lv > maxLocalVer { maxLocalVer = lv } ldbUpdateGlobal(snap, batch, repo, node, newName, fs[fsi].Version) } // Iterate both sides. fsi++ moreDb = dbi.Next() case moreDb && (!moreFs || cmp == 1): if deleteFn != nil { if lv := deleteFn(snap, batch, repo, node, oldName, dbi); lv > maxLocalVer { maxLocalVer = lv } } moreDb = dbi.Next() } } err = db.Write(batch, nil) if err != nil { panic(err) } return maxLocalVer }