func procesImage(path string, f os.FileInfo, err error) error { if f.IsDir() { return nil } log.Debugf("Processing %s", path) extension := filepath.Ext(f.Name()) if !isSupportPhotoType(strings.ToLower(extension)) { log.Warnf("%s's file type %s is unsupported", path, extension) return nil } reader := exif.New() err = reader.Open(path) if err != nil { log.Fatal(err) } str := fmt.Sprintf("%s", reader.Tags["Date and Time"]) t := f.ModTime() if len(str) == 0 { log.Warnf("Date and Time EXIF tag missing for %s", path) } else { layout := "2006:01:02 15:04:05" t, err = time.Parse(layout, str) if err != nil { log.Fatal(err) } } newDir := fmt.Sprintf("%s/%4d/%02d/%02d", destPath, t.Year(), t.Month(), t.Day()) err = os.MkdirAll(newDir, 0777) if err != nil { log.Fatal(err) } newFile := fmt.Sprintf("%s/%s", newDir, f.Name()) if mode == "move" { log.Debugf("Moving %s %s", path, newFile) err = os.Rename(path, newFile) } else { if _, err := os.Stat(newFile); err == nil { log.Warnf("Photo %s already exists", newFile) } else { log.Debugf("Copying %s %s", path, newFile) err = copyFile(path, newFile) } } if err != nil { log.Fatal(err) } return nil }
// Walks the f_base directory and kills procs read from any qemu or // dnsmasq pid files func nukeWalker(path string, info os.FileInfo, err error) error { if err != nil { return nil } log.Debug("walking file: %v", path) switch info.Name() { case "qemu.pid", "dnsmasq.pid": d, err := ioutil.ReadFile(path) t := strings.TrimSpace(string(d)) log.Debug("found pid: %v", t) if err != nil { return err } args := []string{ "kill", t, } log.Infoln("killing process:", t) out, err := processWrapper(args...) if err != nil { log.Error("%v: %v", err, out) } } return nil }
func mkFile(path string, fi os.FileInfo) (err error) { fset := token.NewFileSet() f, err := parser.ParseFile(fset, path, nil, parser.ParseComments) if err != nil { log.Fatalf("Could not parse file: %s", err) } res := &ThriftCli{ Package: f.Name.Name, // Def: fmt.Sprintf("%s", suitecase.Camelize(f.Name.Name)), Fname: strings.TrimSuffix(fi.Name(), ".go") + "_gen_client.go", } for _, decl := range f.Decls { funcDecl, ok := decl.(*ast.FuncDecl) if !ok { continue } if strings.HasSuffix(funcDecl.Name.Name, "ClientFactory") { n := strings.TrimPrefix(strings.TrimSuffix( funcDecl.Name.Name, "ClientFactory"), "New") res.Svc = n break } } res.Def = res.Svc == suitecase.Camelize(res.Package) renderFile(res) return }
func (t *GcsfuseTest) OnlyDir_WithImplicitDir() { var err error var fi os.FileInfo // Mount only a single implicit directory from the bucket. args := []string{ "--only-dir", path.Dir(canned.ImplicitDirFile), canned.FakeBucketName, t.dir, } err = t.runGcsfuse(args) AssertEq(nil, err) defer unmount(t.dir) // It should be as if t.dir points into the implicit directory entries, err := fusetesting.ReadDirPicky(t.dir) AssertEq(nil, err) AssertEq(1, len(entries)) fi = entries[0] ExpectEq(path.Base(canned.ImplicitDirFile), fi.Name()) ExpectEq(len(canned.ImplicitDirFile_Contents), fi.Size()) }
// visit a file path // if the file is a .pb.go (protobuf file), call the parse function and add result to output func visit(currentPath string, f os.FileInfo, err error) error { fmt.Printf("Visited: %s\n", currentPath) if !f.IsDir() { var fileName = f.Name() var strLen = len(fileName) if strLen >= len(DEFAULT_EXTENSION) && fileName[strLen-len(DEFAULT_EXTENSION):strLen] == DEFAULT_EXTENSION { absPath, err1 := filepath.Abs(currentPath) if err1 == nil { importPath := filepath.Dir(absPath) if importPath[0:len(goPath)] == goPath { importPath = importPath[len(goPath):len(importPath)] } srcIndex := strings.Index(importPath, SRC_DIR) importPath = importPath[srcIndex+len(SRC_DIR) : len(importPath)] fmt.Printf("file path from go path: %d %s\n", srcIndex, importPath) result := parseProtobufFile(absPath, importPath) if !output.ImportPath[result.ImportPath] { // add import path to output output.ImportPath[result.ImportPath] = true } for key, dataType := range result.Types { if _, ok := output.Types[key]; !ok { output.Types[key] = dataType } } } } } return err }
// StreamFile streams a file or directory entry into StreamArchive. func (s *StreamArchive) StreamFile(relPath string, fi os.FileInfo, data []byte) error { if fi.IsDir() { fh, err := zip.FileInfoHeader(fi) if err != nil { return err } fh.Name = relPath + "/" if _, err = s.Writer.CreateHeader(fh); err != nil { return err } } else { fh, err := zip.FileInfoHeader(fi) if err != nil { return err } fh.Name = filepath.Join(relPath, fi.Name()) fh.Method = zip.Deflate fw, err := s.Writer.CreateHeader(fh) if err != nil { return err } else if _, err = fw.Write(data); err != nil { return err } } return nil }
func (t *GcsfuseTest) OnlyDir_TrailingSlash() { var err error var fi os.FileInfo // Mount only a single directory from the bucket, including a trailing slash. args := []string{ "--only-dir", path.Dir(canned.ExplicitDirFile) + "/", canned.FakeBucketName, t.dir, } err = t.runGcsfuse(args) AssertEq(nil, err) defer unmount(t.dir) // It should be as if t.dir points into the bucket's first-level directory. entries, err := fusetesting.ReadDirPicky(t.dir) AssertEq(nil, err) AssertEq(1, len(entries)) fi = entries[0] ExpectEq(path.Base(canned.ExplicitDirFile), fi.Name()) ExpectEq(len(canned.ExplicitDirFile_Contents), fi.Size()) }
// ToTempFile writes an action into a generated temporary file and returns its filename func (a Action) ToTempFile() (filename string, err error) { var ( data []byte fd *os.File fi os.FileInfo ) data, err = json.Marshal(a) if err != nil { return } fd, err = ioutil.TempFile("", "migaction_") defer fd.Close() if err != nil { return } _, err = fd.Write(data) if err != nil { return } fi, err = fd.Stat() if err != nil { return } filename = fmt.Sprintf("%s/%s", os.TempDir(), fi.Name()) return }
func list(path string, info os.FileInfo, node *fsNode, n *int) error { if (!info.IsDir() && !info.Mode().IsRegular()) || strings.HasPrefix(info.Name(), ".") { return errors.New("Non-regular file") } (*n)++ if (*n) > fileNumberLimit { return errors.New("Over file limit") //limit number of files walked } node.Name = info.Name() node.Size = info.Size() node.Modified = info.ModTime() if !info.IsDir() { return nil } children, err := ioutil.ReadDir(path) if err != nil { return fmt.Errorf("Failed to list files") } node.Size = 0 for _, i := range children { c := &fsNode{} p := filepath.Join(path, i.Name()) if err := list(p, i, c, n); err != nil { continue } node.Size += c.Size node.Children = append(node.Children, c) } return nil }
func downloadsWalkFunc(fullPath string, Version string, fi2 os.FileInfo, err error, tp TaskParams, report Report, reportFilename, format string) error { if fi2.IsDir() || fi2.Name() == reportFilename { return nil } versionDir := filepath.Join(tp.OutDestRoot, tp.Settings.GetFullVersionName()) relativePath := strings.Replace(fullPath, versionDir, "", -1) relativePath = strings.TrimPrefix(relativePath, "/") text := fi2.Name() if format == "markdown" { text = strings.Replace(text, "_", "\\_", -1) } category := GetCategory(relativePath) //log.Printf("Adding: %s", relativePath) download := Download{text, Version, relativePath} v, ok := report.Categories[category] var existing []Download if !ok { existing = []Download{} } else { existing = *v } existing = append(existing, download) report.Categories[category] = &existing //entryCount++ return nil }
func (fs *memFileSystem) refreshCache(path string, info os.FileInfo) (err error) { // Delete the file if fi is nil. if info == nil { fs.lock.Lock() delete(fs.cache, path) fs.lock.Unlock() return } // Create memory fileinfo and read contents. fi := &memFileInfo{ name: info.Name(), size: info.Size(), mode: info.Mode(), modTime: info.ModTime(), isDir: info.IsDir(), path: path, fs: fs, } // Fill content of the file from disk. if !fi.isDir { fi.content, err = ioutil.ReadFile(path) if err != nil { return } } // Update cache and return. fs.lock.Lock() fs.cache[path] = fi fs.lock.Unlock() return }
// shouldTraverse reports whether the symlink fi should, found in dir, // should be followed. It makes sure symlinks were never visited // before to avoid symlink loops. func shouldTraverse(dir string, fi os.FileInfo) bool { path := filepath.Join(dir, fi.Name()) target, err := filepath.EvalSymlinks(path) if err != nil { if !os.IsNotExist(err) { fmt.Fprintln(os.Stderr, err) } return false } ts, err := os.Stat(target) if err != nil { fmt.Fprintln(os.Stderr, err) return false } if !ts.IsDir() { return false } realParent, err := filepath.EvalSymlinks(dir) if err != nil { fmt.Fprint(os.Stderr, err) return false } realPath := filepath.Join(realParent, fi.Name()) visitedSymlinks.Lock() defer visitedSymlinks.Unlock() if visitedSymlinks.m == nil { visitedSymlinks.m = make(map[string]struct{}) } if _, ok := visitedSymlinks.m[realPath]; ok { return false } visitedSymlinks.m[realPath] = struct{}{} return true }
// fingerprint generates a hash of a file's Last Modified date and the size of // the file. This technique isn't as reliable as hashing the entire body of // the file, but it's good enough. func fingerprint(info os.FileInfo) string { hasher := md5.New() _, _ = io.WriteString(hasher, info.Name()) _ = binary.Write(hasher, binary.LittleEndian, info.ModTime().UnixNano()) h := hasher.Sum(nil) return hex.EncodeToString(h[:4]) }
func (x *Generator) addPath(parent string, prefix string, info os.FileInfo) error { p := path.Join(parent, info.Name()) f := file{ info: info, path: path.Join(prefix, p), } x.fsFilesMap[p] = f if info.IsDir() { f, err := os.Open(f.path) fi, err := f.Readdir(-1) f.Close() if err != nil { return err } x.fsDirsMap[p] = make([]string, 0, len(fi)) for _, f := range fi { if err := x.addPath(p, prefix, f); err != nil { return err } } } else { x.appendFileInDir(parent, info.Name()) } return nil }
// checkWalk is a helper function to sanity check for *.wsp files in a // file tree walk. If the file is valid, normal *.wsp nil is returned. // Otherwise a non-nil error value is returned. func checkWalk(path string, info os.FileInfo, err error) (bool, error) { // Did the Walk function hit an error on this file? if err != nil { log.Printf("%s\n", err) // File perm or exists error, log and skip return false, nil } // Sanity check our file if info.IsDir() { // Ignore dot-files and dot-directories if strings.HasPrefix(info.Name(), ".") { return false, filepath.SkipDir } return false, nil } if !info.Mode().IsRegular() { // Not a regular file return false, nil } if !strings.HasSuffix(path, ".wsp") { // Not a Whisper Database return false, nil } return true, nil }
func fileInfoNameFunc(fi os.FileInfo) string { name := fi.Name() if fi.IsDir() { name += "/" } return name }
func (t *ImplicitDirsTest) DirectoryObjectPresent() { var fi os.FileInfo var entries []os.FileInfo var err error // Set up contents. AssertEq( nil, t.createObjects( map[string]string{ // Directory "foo/": "", })) // Statting the name should return an entry for the directory. fi, err = os.Stat(path.Join(t.mfs.Dir(), "foo")) AssertEq(nil, err) ExpectEq("foo", fi.Name()) ExpectTrue(fi.IsDir()) // ReadDir should show the directory. entries, err = fusetesting.ReadDirPicky(t.mfs.Dir()) AssertEq(nil, err) AssertEq(1, len(entries)) fi = entries[0] ExpectEq("foo", fi.Name()) ExpectTrue(fi.IsDir()) }
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 }
func (h *fsHandler) newFSFile(f *os.File, fileInfo os.FileInfo, compressed bool) (*fsFile, error) { n := fileInfo.Size() contentLength := int(n) if n != int64(contentLength) { f.Close() return nil, fmt.Errorf("too big file: %d bytes", n) } // detect content-type ext := fileExtension(fileInfo.Name(), compressed) contentType := mime.TypeByExtension(ext) if len(contentType) == 0 { data, err := readFileHeader(f, compressed) if err != nil { return nil, fmt.Errorf("cannot read header of the file %q: %s", f.Name(), err) } contentType = http.DetectContentType(data) } lastModified := fileInfo.ModTime() ff := &fsFile{ h: h, f: f, contentType: contentType, contentLength: contentLength, compressed: compressed, lastModified: lastModified, lastModifiedStr: AppendHTTPDate(nil, lastModified), t: time.Now(), } return ff, nil }
func (show *Slideshow) walkSlideshow(localpath string, info os.FileInfo, err error) error { if err != nil { return err } if info.IsDir() { return nil } ext := filepath.Ext(localpath) ext = strings.ToLower(ext) if ext == ".jpg" { photo := new(SlideshowPhoto) photo.path = localpath photo.Photo = path.Join("/slideshow", info.Name()) photo.Timestamp = info.ModTime() //info.ModTime().Format(time.RFC3339) show.Photos = append(show.Photos, photo) } return nil }
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 }
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) }
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 }
func tarDir(srcBase, srcRelative string, tw *tar.Writer, fi os.FileInfo) (err error) { srcFull := srcBase + srcRelative fis, er := ioutil.ReadDir(srcFull) if er != nil { return er } for _, fi := range fis { if fi.IsDir() { tarDir(srcBase, srcRelative+"/"+fi.Name(), tw, fi) } else { tarFile(srcBase, srcRelative+"/"+fi.Name(), tw, fi) } } if len(srcRelative) > 0 { hdr, er := tar.FileInfoHeader(fi, "") if er != nil { return er } hdr.Name = srcRelative if er = tw.WriteHeader(hdr); er != nil { return er } } return nil }
func (self *F) visit(path string, f os.FileInfo, err error) error { if f == nil { return err } //如果是txt文本 if strings.HasSuffix(f.Name(), "txt") { var tp int if f.IsDir() { tp = IsDirectory } else if (f.Mode() & os.ModeSymlink) > 0 { tp = IsSymlink } else { tp = IsRegular } inoFile := &sysFile{ fName: path, fType: tp, fPerm: f.Mode(), fMtime: f.ModTime(), fSize: f.Size(), fShortName: f.Name(), } self.files = append(self.files, inoFile) } return nil }
func StitchWalk(path string, info os.FileInfo, err error) error { fmt.Println(path, info, err) if info.IsDir() { fmt.Println("Is a dir") } else { /* f0, err0 := os.Open(path) if err0 != nil { return err0 } */ fmt.Printf("writing %s. %v bytes", path, info.Size()) b0, err0 := ioutil.ReadFile(path) if err0 != nil { return err0 } s0 := string(b0) // s1 := fmt.Sprintf("%s,\n", s0) s1 := info.Name() s1 = s1[:len(s1)-5] s2 := fmt.Sprintf("%s:%s,\n", s1, s0) W.WriteString(s2) } return err }
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 }
// Prints the filesystem to stdout, useful for testing. // Not part of the filesystem interface. func (mf *MockFilesystem) Print() { var ( dirs []string lines [][]interface{} path string f File fi os.FileInfo files []os.FileInfo maxlen int ) dirs = append(dirs, "/") maxlen = 1 for len(dirs) > 0 { path = dirs[0] dirs = dirs[1:] f, _ = mf.Open(path) fi, _ = f.Stat() files, _ = f.Readdir(100) for _, fi = range files { name := filepath.Join(path, fi.Name()) line := []interface{}{name, fi.Mode(), fi.IsDir()} lines = append(lines, line) if len(name) > maxlen { maxlen = len(name) } if fi.IsDir() { dirs = append(dirs, name) } } } fmtstr := fmt.Sprintf("%%-%vv %%v %%v\n", maxlen) for _, line := range lines { fmt.Printf(fmtstr, line[0], line[1], line[2]) } }
// getRealName - gets the proper filename for sorting purposes // Readdir() filters out directory names without separators, add // them back for proper sorting results. func getRealName(info os.FileInfo) string { if info.IsDir() { // Make sure directory has its end separator. return info.Name() + string(os.PathSeparator) } return info.Name() }
func (p simplePattern) Matches(path string, fi os.FileInfo) bool { if p.matchDirOnly && !fi.IsDir() { return false } filename := fi.Name() return filename == p.content }