func dirList(w http.ResponseWriter, f *os.File) { w.Header().Set("Content-Type", "text/html; charset=utf-8") if dirs, err := f.Readdir(-1); err == nil { files := make([]map[string]string, len(dirs)+1) files[0] = map[string]string{ "name": "..", "href": "..", "size": "-", "mtime": "-", } for i, d := range dirs { href := d.Name() if d.IsDir() { href += "/" } files[i+1] = map[string]string{ "name": d.Name(), "href": href, "size": formatSize(d), "mtime": d.ModTime().Format("2006-01-02 15:04:05"), } } reloadCfg.dirListTmpl.Execute(w, map[string]interface{}{ "dir": f.Name(), "files": files, }) } }
// Directory sends a Gopher listing of the directory specified // If a gophermap file is present, it is used instead of listing the directory contents func (s *Server) Directory(ctx *Context, dir *os.File) (ok bool, err os.Error) { cwd := dir.Name()[len(s.Cwd):] if mapfile, maperr := os.Open(dir.Name()+"/gophermap", 0, 0); maperr == nil { defer mapfile.Close() s.Gophermap(ctx, mapfile, dir) ok = true } else { entries, err := dir.Readdir(-1) if err != nil { s.Logger.Printf("Could not show directory: `%s'\n", err) return } for _, entry := range entries { expandedName := strings.Trim(fmt.Sprintf("%s/%s", cwd, entry.Name), "/") switch true { case entry.IsRegular(): _, err = ctx.Write(s.TextfileLine(entry.Name, expandedName)) case entry.IsDirectory(): _, err = ctx.Write(s.DirectoryLine(entry.Name, expandedName)) default: _, err = ctx.Write(s.InfoLine(entry.Name)) } } s.Logger.Printf("Served directory `%s'\n", cwd) ctx.Write(".") ok = true } return }
func writeFilelist(w http.ResponseWriter, f *os.File, run bool) { files, err := f.Readdir(0) if err != nil { fmt.Fprintf(w, "404") return } fileInfos := []*FileInfo{} for _, file := range files { fileName := file.Name() fileSize := file.Size() if file.IsDir() { fileName += "/" } url := fileName if run { url += "?run=1" } fileInfos = append(fileInfos, &FileInfo{Name: fileName, Url: url, Size: fileSize}) } if run { StatGet(fileInfos) err = tmpl2.Execute(w, fileInfos) } else { err = tmpl.Execute(w, fileInfos) } checkErr(err) }
func (p *DmSrcProvider) getFileList(path string) ([]string, error) { var err error var f *os.File var fileNameArr []string if f, err = os.Open(path); err != nil { return fileNameArr, err } defer f.Close() fileInfoArr, err := f.Readdir(-1) if err != nil { return fileNameArr, err } for _, fileInfo := range fileInfoArr { if fileInfo.Name() == "." || fileInfo.Name() == ".." { continue } if fileInfo.IsDir() || strings.TrimSpace(fileInfo.Name()) == "" { continue } fileNameArr = append(fileNameArr, fileInfo.Name()) } sort.Strings(fileNameArr) return fileNameArr, nil }
// MapDirItems calls todo for every item in dir for which check returns true func MapDirItems(dir string, check func(os.FileInfo) bool, todo func(os.FileInfo) error) (err error) { var ( dh *os.File fi os.FileInfo fis []os.FileInfo ) if dh, err = os.Open(dir); err != nil { return } defer dh.Close() for { if fis, err = dh.Readdir(1000); err != nil { if err == io.EOF { break } return } for _, fi = range fis { if check(fi) { if err = todo(fi); err != nil { return } } } } return nil }
// GetDirectoryStructureWithDepth returns hierarchical representation of os's directories with given depth, // as well as their sizes. func GetDirectoryStructureWithDepth(fullpath string, depth int) (result Directory, err error) { var fileSize int64 if fileSize, err = GetFullDirSize(fullpath); err != nil { return } var currentDir *os.File defer currentDir.Close() if currentDir, err = os.Open(fullpath); err != nil { return } var files []os.FileInfo if files, err = currentDir.Readdir(0); err != nil { return } var directories []Directory for _, v := range files { if v.IsDir() && depth > 0 { var subDir Directory subDirPath := path.Join(fullpath, v.Name()) if subDir, err = GetDirectoryStructureWithDepth(subDirPath, depth-1); err == nil { directories = append(directories, subDir) } else { return } } } result = Directory{fullpath, fileSize, directories} return result, nil }
func init_tx(f *os.File) error { tx = newTxLog() infos, err := f.Readdir(1024) if err != nil { fmt.Println(err) panic("error initializing scas") } log.Println("checking for old tx logs to replay...") for _, info := range infos { name := info.Name() if strings.HasPrefix(name, LOGPREFIX) && name != tx.file.Name() { log.Println("replaying tx log: ", name) fullpath := path.Join(txdir, name) oldfile, err := os.Open(fullpath) err = tx.replay(oldfile, func(k *Key, v Value) error { log.Println("recovering key / value:", k, v) return Put(k, v) }) oldfile.Close() err = os.Remove(fullpath) if err != nil { fmt.Println("error removing old tx file!", err) // continue on for now... will keep getting replayed at startup. } log.Println("tx log replay complete.") } } return nil }
// find the directories and files which matches the regular expression func findRecursively(dir *os.File, reg *regexp.Regexp, currentwd string) (dirs, files []string) { dirs = make([]string, 0) files = make([]string, 0) fileInfos, err := dir.Readdir(0) if err != nil { return } for i := 0; i < len(fileInfos); i++ { name := fileInfos[i].Name() if reg.Match([]byte(name)) == true { if fileInfos[i].IsDir() == true { dirs = append(dirs, currentwd+"/"+name) } else { files = append(dirs, currentwd+"/"+name) } } if fileInfos[i].IsDir() == true { redir, err := os.Open(currentwd + "/" + name) if err != nil { continue } newDirs, newFiles := findRecursively(redir, reg, currentwd+"/"+name) dirs = append(dirs, newDirs...) files = append(files, newFiles...) redir.Close() } } return }
func ListMp3Files(f *os.File, sfl []*SongFile) []*SongFile { fi, err := f.Stat() if err != nil { log.Fatal(err) } if fi.Mode().IsDir() { fis, err := f.Readdir(0) if err != nil { log.Fatal(err) } for _, fi := range fis { name := filepath.Join(f.Name(), fi.Name()) if f, err := os.Open(name); err != nil { log.Printf("Failed to open '%s': %v", name, err) } else { sfl = ListMp3Files(f, sfl) } } } else if fi.Mode().IsRegular() { if strings.ToLower(filepath.Ext(f.Name())) == ".mp3" { sfl = append(sfl, NewSongFile(f.Name(), fi.Size())) } } return sfl }
func init_ss(f *os.File) error { infos, err := f.Readdir(1024) if err != nil { fmt.Println(err) panic("error initializing scas") } log.Println("loading sstables...") for _, info := range infos { if strings.HasPrefix(info.Name(), SSDATA_PREFIX) { log.Println("loading sstable: ", info.Name()) datafile, err := os.Open(path.Join(ssdir, info.Name())) if err != nil { return err } ss, err := newSSTable(datafile) if err != nil { return err } err = ss.readIndex() if err != nil { fmt.Println("error loading sstable!", err) // continue on for now... will keep getting replayed at startup. } sstables = append(sstables, *ss) } } log.Println("loading sstables complete.") return nil }
// MustReaddir returns a slice of os.FileInfo for every file in the given dir. This is equivilent to calling dir.Readdir(-1), except this call will stop the program on an error func MustReaddir(dir *os.File) []os.FileInfo { fi, err := dir.Readdir(-1) if err != nil { glog.Fatal(err) } return fi }
func ensureFolderHierarchy(srcDir *os.File, targetDirPath string) { var fileInfos, err = srcDir.Readdir(0) var srcSubDir, targetSubDir *os.File var srcDirPath, subDirName, subDirPath string if err != nil { panic(err) } for _, fileInfo := range fileInfos { if fileInfo.IsDir() { srcDirPath = srcDir.Name() subDirName = fileInfo.Name() if srcSubDir, err = os.Open(path.Join(srcDirPath, subDirName)); err != nil { panic(err) } subDirPath = path.Join(targetDirPath, subDirName) if targetSubDir, err = os.Open(subDirPath); err != nil { if err = os.Mkdir(subDirPath, os.ModeDir|os.ModePerm); err != nil { panic(err) } } else { targetSubDir.Close() } ensureFolderHierarchy(srcSubDir, subDirPath) srcSubDir.Close() } } }
func dirServ(w http.ResponseWriter, oFile *os.File, p string) { w.Header().Set("Content-Type", "text/html;charset=utf-8") data := make(map[string]string) data["title"] = "目录查看" data["head"] = "<strong>当前位置:</strong> " + getPathLink(p) fiList, _ := oFile.Readdir(-1) if len(fiList) == 0 { data["content"] = "<i><空目录></i>" } for _, fi := range fiList { linkStr := fi.Name() var sizeStr, target = "", "" if fi.IsDir() { linkStr += "/" } else { target = " target=\"_blank\"" sizeStr = "[" + getFileSize(fi.Size()) + "] " } linkStr = "<a href=\"" + linkStr + "\"" + target + ">" + linkStr + "</a>" data["content"] += "<div>" + linkStr + " <span class=\"comment\">" + sizeStr + "[" + fi.ModTime().Format("2006-01-02 03:04") + "]</span></div>" } render(w, data) }
func (_ *FileServer) processDir(w http.ResponseWriter, dir *os.File, fullpath string, relpath string) { w.Header().Set("Content-type", "text/html; charset=UTF-8") fis, err := dir.Readdir(-1) gotang.AssertNoError(err, "读取文件夹信息出错!") items := make([]Item, 0, len(fis)) for _, fi := range fis { var size int64 = 0 if !fi.IsDir() { size = fi.Size() } item := Item{ Name: fi.Name(), Title: fi.Name(), URI: path.Join(relpath, fi.Name()), Size: size, } items = append(items, item) } tmp.Execute(w, map[string]interface{}{ "ParentURI": path.Dir(relpath), "CurrentURI": relpath, "files": items, }) }
// adds a directory recursively func (z *zipFile) AddDirectory(f *os.File, infoCh chan<- string) error { debug := debug.Debug("zipFile.AddDirectory") dirName := f.Name() debug("adding directory %s", dirName) fi, err := f.Readdir(-1) if err != nil { return err } for _, info := range fi { debug("zname is...", z.f.Name()) fName := info.Name() fullName := dirName + "/" + fName if info.IsDir() { if strings.Index(fName, ".") != 0 { debug("found directory, adding file %s", dirName+"/"+fName) if infoCh != nil { infoCh <- fmt.Sprintf("adding %s", fullName) } f, err := os.Open(fullName) if err != nil { // TODO: prob don't want to do this ALWAYS return err } err = z.AddDirectory(f, infoCh) if err != nil { return err } err = f.Close() if err != nil { return err } } } else if info.Name() != z.fName { debug("have file %s in directory %s, adding now", info.Name(), dirName) if infoCh != nil { infoCh <- fmt.Sprintf("adding %s", fullName) } _, err := z.AddFile(dirName + "/" + info.Name()) if err != nil { // TODO: prob don't want to do this ALWAYS return err } } } return nil }
// GetDirectoriesBiggerThan returns a map of directories, which are bigger than size. func GetDirectoriesBiggerThan(size int64, root string) (result map[string]string, err error) { var dir *os.File defer dir.Close() if dir, err = os.Open(root); err != nil { return } result = make(map[string]string) var dirInfo os.FileInfo if dirInfo, err = dir.Stat(); err == nil && dirInfo.IsDir() { var dirSize int64 if dirSize, err = GetFullDirSize(root); err == nil && dirSize >= size*1000 { result[root] = fmt.Sprintf("%v", int64(math.Ceil(float64(dirSize)/1000))) var files []os.FileInfo if files, err = dir.Readdir(0); err == nil { for _, v := range files { var subMap map[string]string if subMap, err = GetDirectoriesBiggerThan(size, path.Join(root, v.Name())); err == nil { combineMaps(&subMap, &result) } } } } } return }
func gatherEntires(reqPath string, dir *os.File, showHidden bool) (entries entrySlice, err error) { // Attempt to read the directory's contents contents, err := dir.Readdir(-1) // Check for an error if err != nil { return nil, err } //if // Loop through the contents for _, f := range contents { // Check if we are excluding the hidden files if !showHidden { // Check if this file is hidden if []rune(f.Name())[0] == '.' { continue } //if } //if // Add the entry to the slice entries = append(entries, newEntry(reqPath, f)) } //for // Sort the entries sort.Sort(entries) return } //gatherEntries
func newSerialFile(name, path string, file *os.File, stat os.FileInfo) (File, error) { // for non-directories, return a ReaderFile if !stat.IsDir() { return &ReaderFile{name, path, file, stat}, nil } // for directories, stat all of the contents first, so we know what files to // open when NextFile() is called contents, err := file.Readdir(0) if err != nil { return nil, err } // we no longer need our root directory file (we already statted the contents), // so close it err = file.Close() if err != nil { return nil, err } // make sure contents are sorted so -- repeatably -- we get the same inputs. sort.Sort(sortFIByName(contents)) return &serialFile{name, path, contents, stat, nil}, nil }
// filterList returns files in a directory passed through a filter. func filterList(directory string, filter func(os.FileInfo) bool) fileList { var list fileList var err error // Attempt to open directory. var fp *os.File if fp, err = os.Open(directory); err != nil { panic(err) } defer fp.Close() // Listing directory contents. var dirContents []os.FileInfo if dirContents, err = fp.Readdir(-1); err != nil { panic(err) } // Looping over directory contents. for _, file := range dirContents { if filter(file) == true { list = append(list, file) } } // Sorting file list. sort.Sort(struct{ fileList }{list}) return list }
func uploadDirectory(d *gdrive.Drive, input *os.File, inputInfo os.FileInfo, title string, parentId string, share bool, mimeType string, convert bool, jobs chan<- JobInfo) (error, int) { // Create folder folder, err := makeFolder(d, filepath.Base(inputInfo.Name()), parentId, share) if err != nil { return err, 0 } // Read all files in directory files, err := input.Readdir(0) if err != nil { return err, 0 } // Get current dir currentDir, err := os.Getwd() if err != nil { return err, 0 } // Go into directory dstDir := filepath.Join(currentDir, inputInfo.Name()) err = os.Chdir(dstDir) if err != nil { return err, 0 } // Change back to original directory when done defer func() { os.Chdir(currentDir) }() // Keep track for number of jobs jobCount := 0 jc := 0 for _, fi := range files { f, err := os.Open(fi.Name()) if err != nil { return err, 0 } if fi.IsDir() { err, jc = uploadDirectory(d, f, fi, "", folder.Id, share, mimeType, convert, jobs) } else { fmt.Printf("Adding job\r\n") jobCount++ jobs <- JobInfo{d, f, fi, "", folder.Id, share, mimeType, convert} } jobCount += jc jc = 0 if err != nil { return err, jobCount } } return nil, jobCount }
func readtemplates(dir string, titles map[Language]string) (err error) { var templates *template.Template templates, err = template.New("base").Funcs(tmplFuncs).ParseGlob(dir + "/*.tmpl") if err != nil { return } var d *os.File d, err = os.Open(dir) if err != nil { return } var v []os.FileInfo v, err = d.Readdir(0) if err != nil { return } for _, fi := range v { if fi.IsDir() { var t *template.Template t, err = templates.Clone() if err != nil { return } subdir := dir + "/" + fi.Name() _, err = t.ParseGlob(subdir + "/*.tmpl") if err != nil { return } th := t.Lookup("home") ti := t.Lookup("info") if th == nil { return fmt.Errorf(`Template "home" is missing in %s`, subdir) } if ti == nil { return fmt.Errorf(`Template "info" is missing in %s`, subdir) } title, ok := "", false if title, ok = titles[Language(fi.Name())]; !ok { if title, ok = titles[defaultlang]; !ok { title = "Uploader" } } langtmpl[Language(fi.Name())] = &tmpl{title, th, ti} } } defaulttmpl = langtmpl[defaultlang] if defaulttmpl == nil { fmt.Errorf("missing " + string(defaultlang) + " template") } languages = make([]Language, 0, len(langtmpl)) languages = append(languages, defaultlang) for k := range langtmpl { if k != defaultlang { languages = append(languages, k) } } return }
func (this *TemplateCache) Load(root string) (err os.Error) { var fd *os.File if fd, err = os.Open(root, os.O_RDONLY, 0600); err != nil { return } var dirs []os.FileInfo if dirs, err = fd.Readdir(-1); err != nil { fd.Close() return } fd.Close() var p string var ok bool var tmpl *template.Template for _, sd := range dirs { p = path.Join(root, sd.Name) if sd.IsDirectory() { if err = this.Load(p); err != nil { return } } else { if tmplExt != path.Ext(p) { continue } fmt.Fprintf(os.Stdout, "[i] Parsing template: %s\n", p) if tmpl, err = template.ParseFile(p, nil); err != nil { return } // Strip webroot and extension p = p[len(context.Config().WebRoot):] p = p[0 : len(p)-len(tmplExt)] if len(p) == 0 { return os.NewError("Invalid template name") } this.lock.RLock() if _, ok = this.data[p]; ok { this.lock.RUnlock() return os.NewError("Duplicate template definition: " + p) } this.lock.RUnlock() this.lock.Lock() this.data[p] = tmpl this.lock.Unlock() } } return }
// Get the folder files func getFolderFiles(d *os.File) []os.FileInfo { files, err := d.Readdir(-1) if err != nil { fmt.Println(err) os.Exit(1) } return files }
/* Helper. Recursively zip the contents of a directory tree using the zip.Writer. Filters so it only includes .rel files Note: this will not work if there are symbolic links in the src directory tree. (because Readdir does not follow links.) */ func zipSrcDirTree2(w *zip.Writer, directoryPath string, relativeDirName string) (err error) { var dir *os.File var filesInDir []os.FileInfo dir, err = gos.Open(directoryPath) filesInDir, err = dir.Readdir(0) if err != nil { return } err = dir.Close() for _, fileInfo := range filesInDir { if fileInfo.IsDir() { subItemPath := directoryPath + "/" + fileInfo.Name() subItemRelativePath := relativeDirName + "/" + fileInfo.Name() err = zipSrcDirTree2(w, subItemPath, subItemRelativePath) if err != nil { return } } else { // plain old file to be added. if strings.HasSuffix(fileInfo.Name(), ".rel") || strings.HasSuffix(fileInfo.Name(), ".txt") || strings.HasSuffix(fileInfo.Name(), ".html") || strings.HasSuffix(fileInfo.Name(), ".htm") || strings.HasSuffix(fileInfo.Name(), ".css") { subItemPath := directoryPath + "/" + fileInfo.Name() subItemRelativePath := relativeDirName + "/" + fileInfo.Name() var fh *zip.FileHeader fh, err = zip.FileInfoHeader(fileInfo) if err != nil { return } fh.Name = subItemRelativePath var zw io.Writer zw, err = w.CreateHeader(fh) if err != nil { return } var f *os.File f, err = gos.Open(subItemPath) if err != nil { return } _, err = io.Copy(zw, f) err = f.Close() if err != nil { return } } } } return }
func serveDir(conn net.Conn, dir *os.File, path string) { files, _ := dir.Readdir(0) ctx := struct { Dir string Files []os.FileInfo }{ path, files, } fmt.Fprint(conn, "HTTP/1.1 200 ok\r\nServer: Webs\r\nContent-Type: text/html; charset=utf-8\r\n\r\n") tmpl.Execute(conn, ctx) }
func (c *directoryCrawler) parseDirectory(prefix string, lessDir, cssDir *os.File) { files, err := lessDir.Readdir(-1) if err != nil { fmt.Printf("Can't scan %s for files", lessDir.Name()) return } for _, v := range files { if v.IsDir() { if strings.HasPrefix(v.Name(), "_") { // We're dealing with an underscore-prefixed directory. if isVerbose { dir, _ := filepath.Rel(c.rootLESS.Name(), filepath.Join(lessDir.Name(), v.Name())) fmt.Printf("skip: %s\n", dir+"/*") } continue } lessDeeper, _ := os.Open(lessDir.Name() + string(os.PathSeparator) + v.Name()) cssDeeper, err := os.Open(cssDir.Name() + string(os.PathSeparator) + v.Name()) if err != nil { if os.IsNotExist(err) { err = os.Mkdir(cssDir.Name()+string(os.PathSeparator)+v.Name(), 0755) if err != nil { fmt.Println("Can't create css directory") return } cssDeeper, _ = os.Open(cssDir.Name() + string(os.PathSeparator) + v.Name()) } } c.parseDirectory(v.Name()+string(os.PathSeparator), lessDeeper, cssDeeper) } if !v.IsDir() && lessFilename.MatchString(v.Name()) { if strings.HasPrefix(v.Name(), "_") { // We're dealing with an underscore-prefixed file (an include). if isVerbose { filename, _ := filepath.Rel(c.rootLESS.Name(), filepath.Join(lessDir.Name(), v.Name())) fmt.Printf("skip: %s\n", filename) } continue } c.addFunc(c, lessDir, cssDir, v) } } }
func get_dir(dir string) *[]dir_t { var err error var fi_buf os.FileInfo var fi_tab []os.FileInfo var f *os.File var t []dir_t var u dir_t t = make([]dir_t, 0) f, err = os.Open(dir) if err != nil { fmt.Printf("failed to open %s (%v)\n", dir, err) return nil } fi_buf, err = f.Stat() if err != nil { fmt.Printf("failed to stat %s (%v)\n", dir, err) return nil } if fi_buf.IsDir() == false { fmt.Printf("%s is not a directory\n", dir) return nil } fi_tab, err = f.Readdir(0) if err != nil { fmt.Printf("failed to read %s (%v)\n", dir, err) return nil } // loop on the folders for _, g := range fi_tab { if strings.HasPrefix(g.Name(), ".") { continue } if g.IsDir() == false { continue } u.name = g.Name() u.user = g.Sys().(*syscall.Stat_t).Uid u.date = g.ModTime().Unix() t = append(t, u) //fmt.Printf("%s %d %d\n", u.name, u.user, u.date) } return &t }
func uploadDirectory(d *gdrive.Drive, input *os.File, inputInfo os.FileInfo, title string, parentId string, share bool, mimeType string, convert bool) error { // Create folder folder, err := makeFolder(d, filepath.Base(inputInfo.Name()), parentId, share, "") if err != nil { return err } // Read all files in directory files, err := input.Readdir(0) if err != nil { return err } // Get current dir currentDir, err := os.Getwd() if err != nil { return err } // Go into directory dstDir := filepath.Join(currentDir, inputInfo.Name()) err = os.Chdir(dstDir) if err != nil { return err } // Change back to original directory when done defer func() { os.Chdir(currentDir) }() for _, fi := range files { f, err := os.Open(fi.Name()) if err != nil { return err } if fi.IsDir() { err = uploadDirectory(d, f, fi, "", folder.Id, share, mimeType, convert) } else { err = uploadFile(d, f, fi, "", folder.Id, share, mimeType, convert, "") } if err != nil { return err } } return nil }
func cleanupFiles(files []ArchFileItem, tbz []ArchFileItem, ctx *Context) { dirs := make(map[string]bool, 16) for _, item := range files { dirs[filepath.Dir(item.Filename)] = true if item.File != nil { _ = item.File.Close() } if !LeaveTempFiles { _ = unlink(item.Filename, "after zipped") // ignore error } } for _, item := range tbz { dirs[filepath.Dir(item.Filename)] = true if item.File != nil { _ = item.File.Close() } if !LeaveTempFiles { _ = unlink(item.Filename, "after zipped2") // ignore error } } var ( dh *os.File fis []os.FileInfo n int e error ) for dn := range dirs { if dn == ctx.Dir { continue } n = 0 if dh, e = os.Open(dn); e != nil { continue } if fis, e = dh.Readdir(2); e == nil { for i := range fis { if fis[i].Name() == "doc_data.txt" { _ = os.Remove(filepath.Join(dn, fis[i].Name())) } else { n++ } } } _ = dh.Close() if n == 0 { logger.Info("msg", "removing empty directory", "path", dn) _ = os.Remove(dn) } } }
func retryListDir(path string) (listing []os.FileInfo) { for { var err error var f *os.File if f, err = os.Open(path); err == nil { listing, err = f.Readdir(0) f.Close() if err == nil { return } } fmt.Println("Readdir("+path+") failed:", err) time.Sleep(RetryTimeout) } }