Example #1
0
func saveConfigLoop(cfgFile string) {
	for _ = range saveConfigCh {
		fd, err := os.Create(cfgFile + ".tmp")
		if err != nil {
			l.Warnln(err)
			continue
		}

		err = config.Save(fd, cfg)
		if err != nil {
			l.Warnln(err)
			fd.Close()
			continue
		}

		err = fd.Close()
		if err != nil {
			l.Warnln(err)
			continue
		}

		err = osutil.Rename(cfgFile+".tmp", cfgFile)
		if err != nil {
			l.Warnln(err)
		}
	}
}
Example #2
0
func (p *puller) handleEmptyBlock(b bqBlock) {
	f := b.file
	of := p.openFiles[f.Name]

	if b.last {
		if of.err == nil {
			of.file.Close()
		}
	}

	if protocol.IsDeleted(f.Flags) {
		if debug {
			l.Debugf("pull: delete %q", f.Name)
		}
		os.Remove(of.temp)
		os.Chmod(of.filepath, 0666)
		if p.versioner != nil {
			if debug {
				l.Debugln("pull: deleting with versioner")
			}
			if err := p.versioner.Archive(p.repoCfg.Directory, of.filepath); err == nil {
				p.model.updateLocal(p.repoCfg.ID, f)
			} else if debug {
				l.Debugln("pull: error:", err)
			}
		} else if err := os.Remove(of.filepath); err == nil || os.IsNotExist(err) {
			p.model.updateLocal(p.repoCfg.ID, f)
		}
	} else {
		if debug {
			l.Debugf("pull: no blocks to fetch and nothing to copy for %q / %q", p.repoCfg.ID, f.Name)
		}
		t := time.Unix(f.Modified, 0)
		if os.Chtimes(of.temp, t, t) != nil {
			delete(p.openFiles, f.Name)
			return
		}
		if !p.repoCfg.IgnorePerms && protocol.HasPermissionBits(f.Flags) && os.Chmod(of.temp, os.FileMode(f.Flags&0777)) != nil {
			delete(p.openFiles, f.Name)
			return
		}
		osutil.ShowFile(of.temp)
		if osutil.Rename(of.temp, of.filepath) == nil {
			p.model.updateLocal(p.repoCfg.ID, f)
		}
	}
	delete(p.openFiles, f.Name)
}
Example #3
0
// Move away the named file to a version archive. If this function returns
// nil, the named file does not exist any more (has been archived).
func (v Simple) Archive(path string) error {
	_, err := os.Stat(path)
	if err != nil && os.IsNotExist(err) {
		return nil
	}

	if debug {
		l.Debugln("archiving", path)
	}

	file := filepath.Base(path)
	dir := filepath.Join(filepath.Dir(path), ".stversions")
	err = os.MkdirAll(dir, 0755)
	if err != nil && !os.IsExist(err) {
		return err
	} else {
		osutil.HideFile(dir)
	}

	ver := file + "~" + time.Now().Format("20060102-150405")
	err = osutil.Rename(path, filepath.Join(dir, ver))
	if err != nil {
		return err
	}

	versions, err := filepath.Glob(filepath.Join(dir, file+"~*"))
	if err != nil {
		l.Warnln(err)
		return nil
	}

	if len(versions) > v.keep {
		sort.Strings(versions)
		for _, toRemove := range versions[:len(versions)-v.keep] {
			err = os.Remove(toRemove)
			if err != nil {
				l.Warnln(err)
			}
		}
	}

	return nil
}
Example #4
0
func (m *Model) saveIndex(repo string, dir string, fs []protocol.FileInfo) {
	id := fmt.Sprintf("%x", sha1.Sum([]byte(m.repoCfgs[repo].Directory)))
	name := id + ".idx.gz"
	name = filepath.Join(dir, name)

	idxf, err := os.Create(name + ".tmp")
	if err != nil {
		return
	}

	gzw := gzip.NewWriter(idxf)

	protocol.IndexMessage{
		Repository: repo,
		Files:      fs,
	}.EncodeXDR(gzw)
	gzw.Close()
	idxf.Close()

	osutil.Rename(name+".tmp", name)
}
Example #5
0
func (m *Model) saveIndex(repo string, dir string, fs []protocol.FileInfo) error {
	id := fmt.Sprintf("%x", sha1.Sum([]byte(m.repoCfgs[repo].Directory)))
	name := id + ".idx.gz"
	name = filepath.Join(dir, name)
	tmp := fmt.Sprintf("%s.tmp.%d", name, time.Now().UnixNano())
	idxf, err := os.OpenFile(tmp, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
	if err != nil {
		return err
	}
	defer os.Remove(tmp)

	gzw := gzip.NewWriter(idxf)

	n, err := protocol.IndexMessage{
		Repository: repo,
		Files:      fs,
	}.EncodeXDR(gzw)
	if err != nil {
		gzw.Close()
		idxf.Close()
		return err
	}

	err = gzw.Close()
	if err != nil {
		return err
	}

	err = idxf.Close()
	if err != nil {
		return err
	}

	if debug {
		l.Debugln("wrote index,", n, "bytes uncompressed")
	}

	return osutil.Rename(tmp, name)
}
Example #6
0
func (m *Model) saveIndex(repo string, dir string, fs []protocol.FileInfo) error {
	id := fmt.Sprintf("%x", sha1.Sum([]byte(m.repoCfgs[repo].Directory)))
	name := id + ".idx.gz"
	name = filepath.Join(dir, name)

	idxf, err := os.Create(name + ".tmp")
	if err != nil {
		return err
	}

	gzw := gzip.NewWriter(idxf)

	n, err := protocol.IndexMessage{
		Repository: repo,
		Files:      fs,
	}.EncodeXDR(gzw)
	if err != nil {
		gzw.Close()
		idxf.Close()
		return err
	}

	err = gzw.Close()
	if err != nil {
		return err
	}

	err = idxf.Close()
	if err != nil {
		return err
	}

	if debug {
		l.Debugln("wrote index,", n, "bytes uncompressed")
	}

	return osutil.Rename(name+".tmp", name)
}
Example #7
0
func saveCsrfTokens() {
	name := filepath.Join(confDir, "csrftokens.txt")
	tmp := fmt.Sprintf("%s.tmp.%d", name, time.Now().UnixNano())

	f, err := os.OpenFile(tmp, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
	if err != nil {
		return
	}
	defer os.Remove(tmp)

	for _, t := range csrfTokens {
		_, err := fmt.Fprintln(f, t)
		if err != nil {
			return
		}
	}

	err = f.Close()
	if err != nil {
		return
	}

	osutil.Rename(tmp, name)
}
Example #8
0
func (p *puller) closeFile(f scanner.File) {
	if debug {
		l.Debugf("pull: closing %q / %q", p.repoCfg.ID, f.Name)
	}

	of := p.openFiles[f.Name]
	of.file.Close()
	defer os.Remove(of.temp)

	delete(p.openFiles, f.Name)

	fd, err := os.Open(of.temp)
	if err != nil {
		if debug {
			l.Debugf("pull: error: %q / %q: %v", p.repoCfg.ID, f.Name, err)
		}
		return
	}
	hb, _ := scanner.Blocks(fd, scanner.StandardBlockSize)
	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 {
			l.Debugf("pull: %q / %q: block %d hash mismatch", p.repoCfg.ID, f.Name, i)
			return
		}
	}

	t := time.Unix(f.Modified, 0)
	err = os.Chtimes(of.temp, t, t)
	if debug && err != nil {
		l.Debugf("pull: 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 debug && err != nil {
			l.Debugf("pull: 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 {
		l.Debugf("pull: error: %q / %q: %v", p.repoCfg.ID, f.Name, err)
	}
}
Example #9
0
// Move away the named file to a version archive. If this function returns
// nil, the named file does not exist any more (has been archived).
func (v Simple) Archive(repoPath, filePath string) error {
	_, err := os.Stat(filePath)
	if err != nil && os.IsNotExist(err) {
		if debug {
			l.Debugln("not archiving nonexistent file", filePath)
		}
		return nil
	}

	versionsDir := filepath.Join(repoPath, ".stversions")
	_, err = os.Stat(versionsDir)
	if err != nil {
		if os.IsNotExist(err) {
			if debug {
				l.Debugln("creating versions dir", versionsDir)
			}
			os.MkdirAll(versionsDir, 0755)
			osutil.HideFile(versionsDir)
		} else {
			return err
		}
	}

	if debug {
		l.Debugln("archiving", filePath)
	}

	file := filepath.Base(filePath)
	inRepoPath, err := filepath.Rel(repoPath, filepath.Dir(filePath))
	if err != nil {
		return err
	}

	dir := filepath.Join(versionsDir, inRepoPath)
	err = os.MkdirAll(dir, 0755)
	if err != nil && !os.IsExist(err) {
		return err
	}

	ver := file + "~" + time.Now().Format("20060102-150405")
	dst := filepath.Join(dir, ver)
	if debug {
		l.Debugln("moving to", dst)
	}
	err = osutil.Rename(filePath, dst)
	if err != nil {
		return err
	}

	versions, err := filepath.Glob(filepath.Join(dir, file+"~*"))
	if err != nil {
		l.Warnln(err)
		return nil
	}

	if len(versions) > v.keep {
		sort.Strings(versions)
		for _, toRemove := range versions[:len(versions)-v.keep] {
			if debug {
				l.Debugln("cleaning out", toRemove)
			}
			err = os.Remove(toRemove)
			if err != nil {
				l.Warnln(err)
			}
		}
	}

	return nil
}