mirror of
https://github.com/therootcompany/golib.git
synced 2026-04-24 20:58:00 +00:00
feat: gitshallow.File for per-file path/open/sync; use in check-ip git case
This commit is contained in:
parent
6b420badbc
commit
7b71dec445
@ -45,43 +45,43 @@ func main() {
|
|||||||
|
|
||||||
// -- Blacklist ----------------------------------------------------------
|
// -- Blacklist ----------------------------------------------------------
|
||||||
|
|
||||||
var syncs dataset.MultiSyncer
|
var (
|
||||||
var inboundPaths, outboundPaths []string
|
syncer dataset.Syncer
|
||||||
|
inboundPaths []string
|
||||||
|
outboundPaths []string
|
||||||
|
)
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case *inbound != "" || *outbound != "":
|
case *inbound != "" || *outbound != "":
|
||||||
inboundPaths = splitPaths(*inbound)
|
syncer = dataset.NopSyncer{}
|
||||||
|
inboundPaths = splitPaths(*inbound)
|
||||||
outboundPaths = splitPaths(*outbound)
|
outboundPaths = splitPaths(*outbound)
|
||||||
|
|
||||||
case *gitURL != "":
|
case *gitURL != "":
|
||||||
cacheDir := cacheDir(*dataDir, "bitwire-it")
|
dir := cacheDir(*dataDir, "bitwire-it")
|
||||||
syncs = dataset.MultiSyncer{gitshallow.New(*gitURL, cacheDir, 1, "")}
|
gr := gitshallow.New(*gitURL, dir, 1, "")
|
||||||
inboundPaths = []string{
|
syncer = gr
|
||||||
filepath.Join(cacheDir, "tables/inbound/single_ips.txt"),
|
inboundPaths = []string{
|
||||||
filepath.Join(cacheDir, "tables/inbound/networks.txt"),
|
gr.File("tables/inbound/single_ips.txt").Path(),
|
||||||
|
gr.File("tables/inbound/networks.txt").Path(),
|
||||||
}
|
}
|
||||||
outboundPaths = []string{
|
outboundPaths = []string{
|
||||||
filepath.Join(cacheDir, "tables/outbound/single_ips.txt"),
|
gr.File("tables/outbound/single_ips.txt").Path(),
|
||||||
filepath.Join(cacheDir, "tables/outbound/networks.txt"),
|
gr.File("tables/outbound/networks.txt").Path(),
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
dir := cacheDir(*dataDir, "bitwire-it")
|
dir := cacheDir(*dataDir, "bitwire-it")
|
||||||
inboundSingle := filepath.Join(dir, "inbound_single_ips.txt")
|
inSingle := httpcache.New(inboundSingleURL, filepath.Join(dir, "inbound_single_ips.txt"))
|
||||||
inboundNetwork := filepath.Join(dir, "inbound_networks.txt")
|
inNetwork := httpcache.New(inboundNetworkURL, filepath.Join(dir, "inbound_networks.txt"))
|
||||||
outboundSingle := filepath.Join(dir, "outbound_single_ips.txt")
|
outSingle := httpcache.New(outboundSingleURL, filepath.Join(dir, "outbound_single_ips.txt"))
|
||||||
outboundNetwork := filepath.Join(dir, "outbound_networks.txt")
|
outNetwork:= httpcache.New(outboundNetworkURL, filepath.Join(dir, "outbound_networks.txt"))
|
||||||
syncs = dataset.MultiSyncer{
|
syncer = dataset.MultiSyncer{inSingle, inNetwork, outSingle, outNetwork}
|
||||||
httpcache.New(inboundSingleURL, inboundSingle),
|
inboundPaths = []string{inSingle.Path, inNetwork.Path}
|
||||||
httpcache.New(inboundNetworkURL, inboundNetwork),
|
outboundPaths = []string{outSingle.Path, outNetwork.Path}
|
||||||
httpcache.New(outboundSingleURL, outboundSingle),
|
|
||||||
httpcache.New(outboundNetworkURL, outboundNetwork),
|
|
||||||
}
|
|
||||||
inboundPaths = []string{inboundSingle, inboundNetwork}
|
|
||||||
outboundPaths = []string{outboundSingle, outboundNetwork}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
g := dataset.NewGroup(syncs)
|
g := dataset.NewGroup(syncer)
|
||||||
var whitelistDS *dataset.View[ipcohort.Cohort]
|
var whitelistDS *dataset.View[ipcohort.Cohort]
|
||||||
if *whitelist != "" {
|
if *whitelist != "" {
|
||||||
paths := splitPaths(*whitelist)
|
paths := splitPaths(*whitelist)
|
||||||
|
|||||||
@ -168,11 +168,57 @@ func (r *Repo) Sync() (bool, error) {
|
|||||||
return r.syncGit()
|
return r.syncGit()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch satisfies httpcache.Syncer.
|
// Fetch satisfies dataset.Syncer.
|
||||||
func (r *Repo) Fetch() (bool, error) {
|
func (r *Repo) Fetch() (bool, error) {
|
||||||
return r.syncGit()
|
return r.syncGit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// File returns a handle to relPath within this repo.
|
||||||
|
// The handle's Path and Open methods give access to the file; its Fetch method
|
||||||
|
// syncs the repo and reports whether this specific file changed (by mtime).
|
||||||
|
func (r *Repo) File(relPath string) *File {
|
||||||
|
return &File{repo: r, rel: relPath}
|
||||||
|
}
|
||||||
|
|
||||||
|
// File is a handle to a single file inside a Repo.
|
||||||
|
// It implements dataset.Syncer: Fetch syncs the repo (deduped across all File
|
||||||
|
// handles sharing the same Repo) then reports whether this file changed.
|
||||||
|
type File struct {
|
||||||
|
repo *Repo
|
||||||
|
rel string
|
||||||
|
mu sync.Mutex
|
||||||
|
lastMod time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// Path returns the absolute path to the file.
|
||||||
|
func (f *File) Path() string {
|
||||||
|
return filepath.Join(f.repo.Path, f.rel)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open returns an open *os.File for reading. The caller must Close it.
|
||||||
|
func (f *File) Open() (*os.File, error) {
|
||||||
|
return os.Open(f.Path())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch syncs the repo and reports whether this file changed since last call.
|
||||||
|
// Implements dataset.Syncer; safe to call concurrently.
|
||||||
|
func (f *File) Fetch() (bool, error) {
|
||||||
|
if _, err := f.repo.syncGit(); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
info, err := os.Stat(f.Path())
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
if info.ModTime().Equal(f.lastMod) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
f.lastMod = info.ModTime()
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Repo) syncGit() (updated bool, err error) {
|
func (r *Repo) syncGit() (updated bool, err error) {
|
||||||
r.mu.Lock()
|
r.mu.Lock()
|
||||||
defer r.mu.Unlock()
|
defer r.mu.Unlock()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user