Beispiel #1
1
// Return the hash for path stored in the xattrs. If the hash is out of date,
// the hash is computed anew, unless `compute` is false in which case nil is
// returned.
func GetHash(path string, info os.FileInfo, compute bool) (mh.Multihash, error) {
	if info.Mode()&os.ModeSymlink != 0 {
		return symlinkHash(path)
	}

	hashTimeStr, err := attrs.Get(path, XattrHashTime)
	if err != nil {
		if compute {
			return HashFile(path, info)
		} else if IsNoData(err) {
			// ignore error
			return nil, nil
		} else {
			return nil, err
		}
	}

	hashTime, err := time.Parse(time.RFC3339Nano, string(hashTimeStr))
	if err != nil {
		return nil, err
	}

	if hashTime != info.ModTime() {
		if compute {
			return HashFile(path, info)
		} else {
			return nil, nil
		}
	}

	return attrs.Get(path, XattrHash)
}
Beispiel #2
1
func (f *Filesystem) shouldRead(filePath string, fi os.FileInfo) (bool, error) {
	if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
		link, err := filepath.EvalSymlinks(filePath)
		if err != nil {
			jww.ERROR.Printf("Cannot read symbolic link '%s', error was: %s", filePath, err)
			return false, nil
		}
		linkfi, err := os.Stat(link)
		if err != nil {
			jww.ERROR.Printf("Cannot stat '%s', error was: %s", link, err)
			return false, nil
		}
		if !linkfi.Mode().IsRegular() {
			jww.ERROR.Printf("Symbolic links for directories not supported, skipping '%s'", filePath)
		}
		return false, nil
	}

	if fi.IsDir() {
		if f.avoid(filePath) || isNonProcessablePath(filePath) {
			return false, filepath.SkipDir
		}
		return false, nil
	}

	if isNonProcessablePath(filePath) {
		return false, nil
	}
	return true, nil
}
Beispiel #3
1
func (daemon *Daemon) mountVolumes(container *Container) error {
	mounts, err := daemon.setupMounts(container)
	if err != nil {
		return err
	}

	for _, m := range mounts {
		dest, err := container.GetResourcePath(m.Destination)
		if err != nil {
			return err
		}

		var stat os.FileInfo
		stat, err = os.Stat(m.Source)
		if err != nil {
			return err
		}
		if err = fileutils.CreateIfNotExists(dest, stat.IsDir()); err != nil {
			return err
		}

		opts := "rbind,ro"
		if m.Writable {
			opts = "rbind,rw"
		}

		if err := mount.Mount(m.Source, dest, "bind", opts); err != nil {
			return err
		}
	}

	return nil
}
Beispiel #4
1
func checkFile(f os.FileInfo) bool {
	if f.Mode().IsRegular() {
		return true
	}

	return false
}
Beispiel #5
1
// getReaderSize gets the size of the underlying reader, if possible.
func getReaderSize(reader io.Reader) (size int64, err error) {
	size = -1
	if reader != nil {
		switch v := reader.(type) {
		case *bytes.Buffer:
			size = int64(v.Len())
		case *bytes.Reader:
			size = int64(v.Len())
		case *strings.Reader:
			size = int64(v.Len())
		case *os.File:
			var st os.FileInfo
			st, err = v.Stat()
			if err != nil {
				return 0, err
			}
			size = st.Size()
		case *Object:
			var st ObjectInfo
			st, err = v.Stat()
			if err != nil {
				return 0, err
			}
			size = st.Size
		}
	}
	return size, nil
}
Beispiel #6
1
func NewCommonFileMap(fileName string, fi *os.FileInfo) map[string]interface{} {
	m := newCamliMap(1, "" /* no type yet */)

	lastSlash := strings.LastIndex(fileName, "/")
	baseName := fileName[lastSlash+1:]
	if isValidUtf8(baseName) {
		m["fileName"] = baseName
	} else {
		m["fileNameBytes"] = []uint8(baseName)
	}

	// Common elements (from file-common.txt)
	m["unixPermission"] = fmt.Sprintf("0%o", fi.Permission())
	if fi.Uid != -1 {
		m["unixOwnerId"] = fi.Uid
		if user := getUserFromUid(fi.Uid); user != "" {
			m["unixOwner"] = user
		}
	}
	if fi.Gid != -1 {
		m["unixGroupId"] = fi.Gid
		if group := getGroupFromGid(fi.Gid); group != "" {
			m["unixGroup"] = group
		}
	}
	if mtime := fi.Mtime_ns; mtime != 0 {
		m["unixMtime"] = rfc3339FromNanos(mtime)
	}
	// Include the ctime too, if it differs.
	if ctime := fi.Ctime_ns; ctime != 0 && fi.Mtime_ns != fi.Ctime_ns {
		m["unixCtime"] = rfc3339FromNanos(ctime)
	}

	return m
}
Beispiel #7
0
// writeTarHeader writes tar header for given file, returns error if operation fails
func (t *stiTar) writeTarHeader(tarWriter *tar.Writer, dir string, path string, info os.FileInfo, includeDirInPath bool) error {
	var (
		link string
		err  error
	)
	if info.Mode()&os.ModeSymlink != 0 {
		link, err = os.Readlink(path)
		if err != nil {
			return err
		}
	}
	header, err := tar.FileInfoHeader(info, link)
	if err != nil {
		return err
	}
	prefix := dir
	if includeDirInPath {
		prefix = filepath.Dir(prefix)
	}
	header.Name = filepath.ToSlash(path[1+len(prefix):])
	glog.V(5).Infof("Adding to tar: %s as %s", path, header.Name)
	if err = tarWriter.WriteHeader(header); err != nil {
		return err
	}

	return nil
}
Beispiel #8
0
func parseProvidesRequires(fi os.FileInfo, path string, f io.Reader) (provides, requires []string, err error) {
	mt := fi.ModTime()
	depCacheMu.Lock()
	defer depCacheMu.Unlock()
	if ci := depCache[path]; ci.modTime.Equal(mt) {
		return ci.provides, ci.requires, nil
	}

	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		l := scanner.Text()
		if !strings.HasPrefix(l, "goog.") {
			continue
		}
		m := provReqRx.FindStringSubmatch(l)
		if m != nil {
			if m[1] == "provide" {
				provides = append(provides, m[2])
			} else {
				requires = append(requires, m[2])
			}
		}
	}
	if err := scanner.Err(); err != nil {
		return nil, nil, err
	}
	depCache[path] = depCacheItem{provides: provides, requires: requires, modTime: mt}
	return provides, requires, nil
}
Beispiel #9
0
func (p *Page) checkHtmlDoWrite(tplFi, htmlFi os.FileInfo, htmlErr error) bool {
	var doWrite bool
	if p.Config.AutoGenerateHtmlCycleTime <= 0 {
		doWrite = true
	} else {
		if htmlErr != nil {
			doWrite = true
		} else {
			switch {
			case tplFi.ModTime().Unix() >= htmlFi.ModTime().Unix():
				doWrite = true
			case tplFi.ModTime().Unix() >= htmlFi.ModTime().Unix():
				doWrite = true
			case time.Now().Unix()-htmlFi.ModTime().Unix() >= p.Config.AutoGenerateHtmlCycleTime:
				doWrite = true
			default:
				if globalTplCache := p.site.GetTemplateCache("globalTpl"); globalTplCache.ModTime > 0 && globalTplCache.ModTime >= htmlFi.ModTime().Unix() {
					doWrite = true
				}
			}
		}
	}

	return doWrite
}
Beispiel #10
0
func handleFile(sourceDirectory string, targetDirectory string,
	file os.FileInfo) {
	fn := filepath.Join(sourceDirectory, file.Name())
	logging.Log.Debug("filename=%s", fn)
	photo := photograph.New()
	err := photo.Load(fn)
	if err != nil {
		logging.Log.Error(err.Error())
	}
	logging.Log.Debug("%d EXIF tags", len(photo.ExifMap))
	for tag, value := range photo.ExifMap {
		fmt.Printf("%s\t%s\n", tag, value)
	}
	logging.Log.Debug("photo=%v", photo.OriginalFileName)
	logging.Log.Debug("targetDir=%v", targetDirectory)
	targetDirectory = getTargetDirectoryNameWithDate(photo.Time,
		targetDirectory)
	err = os.MkdirAll(targetDirectory, 0700)
	if err != nil {
		logging.Log.Error(err.Error())
	}
	s := strings.Replace(file.Name(), "_DSC", "", 1)
	s = strings.Replace(s, "DSC_", "", 1)
	ext := filepath.Ext(s)
	s = strings.Replace(s, ext, "", 1)
	ext = strings.ToLower(ext)
	logging.Log.Debug("ext=%s", ext)
	targetFile := fmt.Sprintf("%s_%s_%s%s", photo.Iso8601(), s,
		"makela_ari", ext)
	targetFile = filepath.Join(targetDirectory, targetFile)
	logging.Log.Debug("targetFile=%s", targetFile)
	ioutil.WriteFile(targetFile, photo.Data, 0600)
}
Beispiel #11
0
// Load a single message file
func loadMessageFile(path string, info os.FileInfo, osError error) error {
	if osError != nil {
		return osError
	}
	if info.IsDir() {
		return nil
	}

	if matched, _ := regexp.MatchString(messageFilePattern, info.Name()); matched {
		if config, error := parseMessagesFile(path); error != nil {
			return error
		} else {
			locale := parseLocaleFromFileName(info.Name())

			// If we have already parsed a message file for this locale, merge both
			if _, exists := messages[locale]; exists {
				messages[locale].Merge(config)
				TRACE.Printf("Successfully merged messages for locale '%s'", locale)
			} else {
				messages[locale] = config
			}

			TRACE.Println("Successfully loaded messages from file", info.Name())
		}
	} else {
		TRACE.Printf("Ignoring file %s because it did not have a valid extension", info.Name())
	}

	return nil
}
Beispiel #12
0
func NewSerialFile(name, path string, hidden bool, stat os.FileInfo) (File, error) {
	switch mode := stat.Mode(); {
	case mode.IsRegular():
		file, err := os.Open(path)
		if err != nil {
			return nil, err
		}
		return NewReaderFile(name, path, file, stat), nil
	case mode.IsDir():
		// for directories, stat all of the contents first, so we know what files to
		// open when NextFile() is called
		contents, err := ioutil.ReadDir(path)
		if err != nil {
			return nil, err
		}
		return &serialFile{name, path, contents, stat, nil, hidden}, nil
	case mode&os.ModeSymlink != 0:
		target, err := os.Readlink(path)
		if err != nil {
			return nil, err
		}
		return NewLinkFile(name, path, target, stat), nil
	default:
		return nil, fmt.Errorf("Unrecognized file type for %s: %s", name, mode.String())
	}
}
Beispiel #13
0
func (self *Watcher) includeFolders(path string, info os.FileInfo, err error) error {
	if info.IsDir() {
		log.Println("Including:", path)
		self.watched[path] = contract.NewPackage(path)
	}
	return nil
}
Beispiel #14
0
func isRegularFile(fi os.FileInfo) bool {
	if fi == nil {
		return false
	}

	return fi.Mode()&(os.ModeType|os.ModeCharDevice) == 0
}
Beispiel #15
0
// Append the paths to an array in case that they are markdown files.
func (site *Site) parseAndStore(path string, fileInfo os.FileInfo, inputErr error) (err error) {
	if inputErr != nil {
		return inputErr
	}

	slugsPresence := make(map[string]bool)

	if !fileInfo.Mode().IsDir() && strings.HasSuffix(path, ".md") {
		file, err := file.New(path)
		if err != nil {
			return err
		}

		if _, present := slugsPresence[file.Slug]; present {
			return newUniqueSlugError(file.Slug)
		}
		slugsPresence[file.Slug] = true

		// Add the pages or articles to the proper array on the site
		if file.IsPage {
			site.Pages = append(site.Pages, file)
		} else {
			site.Articles = append(site.Articles, file)

			// Just supported on Articles
			if len(file.Tags) > 0 {
				site.AppendUniqueTags(file.Tags)
			}
			site.AppendUniqueCategory(file.Category)
		}
	}
	return
}
Beispiel #16
0
func findDisplayName(fs FileSystem, ls LockSystem, name string, fi os.FileInfo) (string, error) {
	if slashClean(name) == "/" {
		// Hide the real name of a possibly prefixed root directory.
		return "", nil
	}
	return fi.Name(), nil
}
Beispiel #17
0
func (ps *memPS) findDisplayName(name string, fi os.FileInfo) (string, error) {
	if slashClean(name) == "/" {
		// Hide the real name of a possibly prefixed root directory.
		return "", nil
	}
	return fi.Name(), nil
}
Beispiel #18
0
// walk recursively descends path, calling w.
func walk(path string, info os.FileInfo, walkFn WalkFunc) error {
	err := walkFn(path, info, nil)
	if err != nil {
		if info.IsDir() && err == SkipDir {
			return nil
		}
		return err
	}

	if !info.IsDir() {
		return nil
	}

	list, err := readDir(path)
	if err != nil {
		return walkFn(path, info, err)
	}

	for _, fileInfo := range list {
		err = walk(Join(path, fileInfo.Name()), fileInfo, walkFn)
		if err != nil {
			if !fileInfo.IsDir() || err != SkipDir {
				return err
			}
		}
	}
	return nil
}
Beispiel #19
0
func archiveFile(fname string, cfg *conf.Config, rootStat os.FileInfo, fs os.FileInfo) (newname string, err error) {
	year := strconv.Itoa(fs.ModTime().Year())
	month := fs.ModTime().Month().String()

	archivePath := filepath.Join(cfg.ArchivePath(rootStat.Name()), year, month)
	err = os.MkdirAll(archivePath, rootStat.Mode())
	if err != nil && !os.IsExist(err) {
		return
	}

	zipPath := archivePath + ".zip"
	if util.FileExists(zipPath) {
		//unzip so we can archive the new file ... it will be rezipped later
		if err = util.Unzip(zipPath, archivePath); err != nil {
			return
		}
	}

	newname = filepath.Join(archivePath, fs.Name())
	if _, err = os.Stat(newname); err == nil {
		err = fmt.Errorf("A file of the same name already exists in the archive")
		return
	}
	err = os.Rename(fname, newname)

	return
}
Beispiel #20
0
func (wft *walkFileTree) walkLeaf(fpath string, fi os.FileInfo, err error) error {
	if err != nil {
		return err
	}

	if fpath == outputP {
		return nil
	}

	if fi.IsDir() {
		return nil
	}

	if ssym && fi.Mode()&os.ModeSymlink > 0 {
		return nil
	}

	name := wft.virPath(fpath)

	if wft.allfiles[name] {
		return nil
	}

	if added, err := wft.wak.compress(name, fpath, fi); added {
		if verbose {
			fmt.Printf("Compressed: %s\n", name)
		}
		wft.allfiles[name] = true
		return err
	} else {
		return err
	}
}
Beispiel #21
0
func checkDir(f os.FileInfo) bool {
	if f.Mode().IsDir() {
		return true
	}

	return false
}
Beispiel #22
0
func (wft *tarWalk) compress(name, fpath string, fi os.FileInfo) (bool, error) {
	isSym := fi.Mode()&os.ModeSymlink > 0
	link := ""
	if isSym {
		link, _ = os.Readlink(fpath)
	}

	hdr, err := tar.FileInfoHeader(fi, link)
	if err != nil {
		return false, err
	}
	hdr.Name = name

	tw := wft.tw
	err = tw.WriteHeader(hdr)
	if err != nil {
		return false, err
	}

	if isSym == false {
		fr, err := os.Open(fpath)
		if err != nil {
			return false, err
		}
		defer fr.Close()
		_, err = io.Copy(tw, fr)
		if err != nil {
			return false, err
		}
		tw.Flush()
	}

	return true, nil
}
Beispiel #23
0
func fileInfoNameFunc(fi os.FileInfo) string {
	name := fi.Name()
	if fi.IsDir() {
		name += "/"
	}
	return name
}
Beispiel #24
0
func (dir *Dir) dotu(path string, d os.FileInfo, upool p.Users, sysMode *syscall.Stat_t) {
	u := upool.Uid2User(int(sysMode.Uid))
	g := upool.Gid2Group(int(sysMode.Gid))
	dir.Uid = u.Name()
	if dir.Uid == "" {
		dir.Uid = "none"
	}

	dir.Gid = g.Name()
	if dir.Gid == "" {
		dir.Gid = "none"
	}
	dir.Muid = "none"
	dir.Ext = ""
	dir.Uidnum = uint32(u.Id())
	dir.Gidnum = uint32(g.Id())
	dir.Muidnum = p.NOUID
	if d.Mode()&os.ModeSymlink != 0 {
		var err error
		dir.Ext, err = os.Readlink(path)
		if err != nil {
			dir.Ext = ""
		}
	} else if isBlock(d) {
		dir.Ext = fmt.Sprintf("b %d %d", sysMode.Rdev>>24, sysMode.Rdev&0xFFFFFF)
	} else if isChar(d) {
		dir.Ext = fmt.Sprintf("c %d %d", sysMode.Rdev>>24, sysMode.Rdev&0xFFFFFF)
	}
}
Beispiel #25
0
func checkCopyright(path string, info os.FileInfo, err error) error {
	if err != nil {
		return err
	}
	if !info.Mode().IsRegular() {
		return nil
	}
	if !checkExts[filepath.Ext(path)] {
		return nil
	}

	fd, err := os.Open(path)
	if err != nil {
		return err
	}
	defer fd.Close()

	scanner := bufio.NewScanner(fd)
	for i := 0; scanner.Scan() && i < 5; i++ {
		if copyrightRe.MatchString(scanner.Text()) {
			return nil
		}
	}

	return fmt.Errorf("Missing copyright in %s?", path)
}
Beispiel #26
0
func shouldUpdate(resp *http.Response, fi os.FileInfo, newer, newermetamtime bool) bool {
	var stale bool = false
	filemtime := fi.ModTime()
	if newermetamtime {
		parsed, err := time.Parse(time.RFC1123, resp.Header.Get("x-amz-meta-last-modified"))
		if err != nil {
			// can't see metamtime upload anyhow
			stale = true
			fmt.Fprint(os.Stderr, "Can't read metamtime, setting stale to upload again\n")
		}
		if parsed.Before(filemtime) {
			stale = true
		}
	}
	if newer {
		parsed, err := time.Parse(time.RFC1123, resp.Header.Get("Last-Modified"))
		if err != nil {
			// can't see metamtime upload anyhow
			stale = true
			fmt.Fprint(os.Stderr, "Can't read metamtime, setting stale to upload again\n")
		}
		if parsed.Before(filemtime) {
			stale = true
		}
	}
	return stale
}
Beispiel #27
0
func (t *tarRenderer) Walk(path string, finfo os.FileInfo, err error) error {
	if err != nil {
		return err
	}

	fh, err := tar.FileInfoHeader(finfo, "")
	if err != nil {
		return err
	}

	fh.Name = "download/" + path
	err = t.w.WriteHeader(fh)
	if err != nil {
		return err
	}

	if finfo.IsDir() {
		return nil
	}

	f, err := os.Open(filepath.Join(t.root, path))
	if err != nil {
		return err
	}
	defer f.Close()

	_, err = io.Copy(t.w, f)
	return err
}
Beispiel #28
0
// Walk recursively iterates over all files in a directory and processes any
// file that imports "github.com/boltdb/raw".
func walk(path string, info os.FileInfo, err error) error {
	traceln("walk:", path)

	if info == nil {
		return fmt.Errorf("file not found: %s", err)
	} else if info.IsDir() {
		traceln("skipping: is directory")
		return nil
	} else if filepath.Ext(path) != ".go" {
		traceln("skipping: is not a go file")
		return nil
	}

	// Check if file imports boltdb/raw.
	if v, err := importsRaw(path); err != nil {
		return err
	} else if !v {
		traceln("skipping: does not import raw")
		return nil
	}

	// Process each file.
	if err := process(path); err != nil {
		return err
	}

	return nil
}
Beispiel #29
0
func (t *tarmonster) walk(path string, info os.FileInfo, err error) error {
	if err != nil {
		return err
	}

	if !info.Mode().IsRegular() || info.Size() == 0 {
		return nil
	}

	file, err := os.Open(path)
	if err != nil {
		return err
	}
	defer file.Close()

	// Get tar.Header
	fih, err := tar.FileInfoHeader(info, "")
	if err != nil {
		return err
	}
	fih.Name = strings.TrimPrefix(path, t.src+string(filepath.Separator))

	// Begin a new file
	if err := t.writer.WriteHeader(fih); err != nil {
		return err
	}

	// Write the file
	if _, err := io.CopyBuffer(t.writer, file, t.buffer); err != nil {
		return err
	}

	return err
}
Beispiel #30
0
func (t *Templates) parseFile(path string, f os.FileInfo, err error) error {
	if err != nil {
		return err
	}

	ext := filepath.Ext(f.Name())
	if f.IsDir() || !t.check(ext) {
		return nil
	}

	file, err := os.Open(path)
	if err != nil {
		return err
	}
	defer file.Close()

	contents, err := ioutil.ReadAll(file)
	if err != nil {
		return err
	}

	subPath := strings.Replace(path, t.stripPrefix, "", 1)
	if strings.Contains(path, "/view/") || strings.Contains(path, "/views/") {
		t.views[subPath] = string(contents)
	} else {
		t.partials[subPath] = string(contents)
	}

	return nil
}