func handleFile(fname string) { mdr.Spinner() var l Language found := false for _, lang := range languages { if lang.Match(fname) { found = true l = lang break } } if !found { return // ignore this file } fileinfo, ok := info[l.Name()] if !ok { // language has no existing stat struct so make a new one fileinfo = &Stats{} info[l.Name()] = fileinfo } c, err := ioutil.ReadFile(fname) if err != nil { fmt.Fprintf(os.Stderr, " !Err---> file %s is not readable err:%v\n", fname, err) return } l.Update(c, fileinfo) }
// returns filepath.SkipDir on encountering .gvfs indicating a local mount of a remote file system // might want a flag option to switch this behavior // func CheckPath(pathname string, info os.FileInfo, err error) error { if info == nil { fmt.Printf("WARNING ---> no stat info available for %s\n", pathname) return nil } if info.IsDir() { Verbose.Printf("Checking path %s\n", pathname) Verbose.Printf("dir = %v\n", pathname) if strings.Contains(pathname, "/.gvfs/") { fmt.Printf("found a .gvfs dir (remote filesystem) - skipping it\n") return filepath.SkipDir } if g_LinksFlag { nlinks, err := mdr.FileLinkCt(pathname) if err != nil { fmt.Printf("# err %v getting link ct for %s\n", err, pathname) return nil } fmt.Printf("# dirLinks | %d | %s\n", nlinks, pathname) } mdr.Spinner() } else { // regular file fmode := info.Mode() if fmode.IsRegular() == false { Verbose.Printf("non-regular file skipped -> %s\n", pathname) // BUG(mdr): save skipped files in a list for appending? return nil } if doExtFilter { ext := strings.ToLower(path.Ext(pathname)) if ext != g_extFilter { return nil // not right extension } } Verbose.Printf("%10d %s\n", info.Size(), pathname) g_argList = append(g_argList, pathname) } return nil }
// add a path to the ones we want to inspect later // side effect is to append to files // note this is a recursive function // ? better served by path/filepath/walk? func add(pathName string) { mdr.Spinner() // Verbose.Printf("pathName %s tail %c\n", pathName, pathName[len(pathName)-1]) // short-circuit if it's a backup file if pathName[len(pathName)-1] == '~' { return } // short-circuit known tarpits like ".git" and probably ".gvfs" // need a []string to keep list of these like in mdserver fi, err := os.Stat(pathName) if err != nil { fmt.Fprintf(os.Stderr, " !Err---> cant get info for path %s : %v\n", pathName, err) return } if fi.IsDir() { fs, err := ioutil.ReadDir(pathName) if err != nil { fmt.Fprintf(os.Stderr, " !Err---> cant get read %s : %v\n", pathName, err) return } for _, f := range fs { if f.Name()[0] != '.' { // ignore self, parent and hidden files add(path.Join(pathName, f.Name())) // recursively do this } } return } // if its a regular file then add it to the files to process list if fi.Mode()&os.ModeType == 0 { files = append(files, pathName) return } // if it's not a regular file ignore it but print the mode? How does this help? println(fi.Mode()) fmt.Fprintf(os.Stderr, " !Err---> path %s is not a regular file\n", pathName) }