// Open returns an in-memory VFS initialized with the contents // of the given filename, which must have one of the following // extensions: // // - .zip // - .tar // - .tar.gz // - .tar.bz2 func Open(filename string) (VFS, error) { f, err := os.Open(filename) if err != nil { return nil, err } defer f.Close() base := filepath.Base(filename) ext := strings.ToLower(filepath.Ext(base)) nonExt := filename[:len(filename)-len(ext)] if strings.ToLower(filepath.Ext(nonExt)) == ".tar" { ext = ".tar" + ext } switch ext { case ".zip": st, err := f.Stat() if err != nil { return nil, err } return Zip(f, st.Size()) case ".tar": return Tar(f) case ".tar.gz": return TarGzip(f) case ".tar.bz2": return TarBzip2(f) } return nil, fmt.Errorf("can't open a VFS from a %s file", ext) }
func suffixOf(filename string) string { suffix := filepath.Ext(filename) if suffix == ".gz" { suffix = filepath.Ext(filename[:len(filename)-3]) } return suffix }
// Run waits for watcher events sent by fsnotify // and triggers the restart process if required func (w *watch) Run() { t := time.Now() for { select { case event := <-w.Events: if time.Since(t) < threshold || event.Name == "" { break } t = time.Now() f, err := os.Stat(event.Name) if err != nil { log.Println("error watching ", err) break } if filepath.Ext(event.Name) == ".go" || filepath.Ext(event.Name) == ".tmpl" || f.IsDir() { Describe(event) w.HandleEvent(event) log.Println("restarting...") // Lock w to ensure that restart is an atomic operation w.mu.Lock() w.KillProcess() go w.StartNewProcess() } case err := <-w.Errors: if err != nil { log.Println("error:", err) } } } }
func (w *Watcher) readEvent() { var ( buf [syscall.SizeofInotifyEvent * 4096]byte // Buffer for a maximum of 4096 raw events n int // Number of bytes read with read() errno error // Syscall errno ) for { select { case <-w.done: syscall.Close(w.fd) close(w.acceptEvent) close(w.Error) return default: } n, errno = syscall.Read(w.fd, buf[0:]) if n == 0 { syscall.Close(w.fd) close(w.acceptEvent) close(w.Error) return } if n < syscall.SizeofInotifyEvent { log.Fatal("size of InotifyEvent error", errno) } var offset uint32 = 0 for offset <= uint32(n-syscall.SizeofInotifyEvent) { raw := (*syscall.InotifyEvent)(unsafe.Pointer(&buf[offset])) event := new(FileEvent) event.wd = raw.Wd event.nameLen = raw.Len event.mask = raw.Mask event.cookie = raw.Cookie path := w.wm.paths[int(raw.Wd)] if raw.Len > 0 { // Point "bytes" at the first byte of the filename bytes := (*[syscall.PathMax]byte)(unsafe.Pointer(&buf[offset+syscall.SizeofInotifyEvent])) // The filename is padded with NUL bytes. TrimRight() gets rid of those. event.fileName = path + "/" + strings.TrimRight(string(bytes[0:raw.Len]), "\000") } if _, found := w.skipExt[filepath.Ext(event.fileName)]; !found { fmt.Println("--->", w.skipExt, "--->", filepath.Ext(event.fileName), "--->", found) //发送事件acceptEvent通道 w.acceptEvent <- event } else { fmt.Println("过滤文件:", event.fileName) } offset += syscall.SizeofInotifyEvent + raw.Len } } }
// Generates a dependency Makefile (.d) for the specified source C file. // // @param file The name of the source file. func (c *Compiler) GenDepsForFile(file string) error { if util.NodeNotExist(c.dstDir) { os.MkdirAll(c.dstDir, 0755) } depFile := c.dstDir + "/" + strings.TrimSuffix(file, filepath.Ext(file)) + ".d" depFile = filepath.ToSlash(depFile) var cmd string var err error cmd = c.ccPath + " " + c.cflagsString() + " " + c.includesString() + " -MM -MG " + file + " > " + depFile o, err := util.ShellCommand(cmd) if err != nil { return util.NewNewtError(string(o)) } // Append the extra dependencies (.yml files) to the .d file. f, err := os.OpenFile(depFile, os.O_APPEND|os.O_WRONLY, 0666) if err != nil { return util.NewNewtError(err.Error()) } defer f.Close() objFile := strings.TrimSuffix(file, filepath.Ext(file)) + ".o" if _, err := f.WriteString(objFile + ": " + c.depsString()); err != nil { return util.NewNewtError(err.Error()) } return nil }
// Creates a list of all ASL files to compile. func readAslFiles(path string) { dir, err := ioutil.ReadDir(path) if err != nil { fmt.Println("Error reading in directory!") return } for i := 0; i < len(dir); i++ { name := dir[i].Name() if dir[i].IsDir() && recursive { readAslFiles(filepath.FromSlash(path + PathSeparator + name)) continue } if !dir[i].IsDir() && strings.ToLower(filepath.Ext(name)) == extension { in := filepath.FromSlash(path + PathSeparator + dir[i].Name()) out := filepath.FromSlash("./" + path[len(inDir):len(path)]) newname := name[:len(name)-len(filepath.Ext(name))] file := ASLFile{in, out, newname} aslFiles = append(aslFiles, file) } } }
// %f - whole filepath // %d - dirname of path // %b - basename of file // %B - basename without extension // %e - extension of file func replaceForEach(name, arg string) (string, bool) { li := -1 found := false for i := 0; i > li; { li = i if i = strings.Index(arg, "%"); i > -1 { ext := filepath.Ext(name) if len(ext) > 1 { ext = ext[1:] // strip } base := filepath.Base(name) m := map[rune]string{ 'f': name, 'd': filepath.Dir(name), 'b': base, 'B': strings.TrimSuffix(base, filepath.Ext(name)), 'e': ext, } i++ r := rune(arg[i]) insert, ok := m[r] if ok { found = true arg = strings.Replace(arg, "%"+string(r), insert, -1) // println(arg, insert, strings.TrimSuffix("dir/a.a", filepath.Ext("dir/a.a"))) } } else { break } } return arg, found }
func NewPlistHandler(rootDir string) macaron.Handler { return func(r *http.Request, w http.ResponseWriter, ctx *macaron.Context) { relpath := ctx.Params("*") if filepath.Ext(relpath) == ".plist" { relpath = relpath[0:len(relpath)-6] + ".ipa" } abspath := filepath.Join(rootDir, relpath) plinfo, err := parseIPA(abspath) if err != nil { log.Println(err) ctx.Error(500, err.Error()) return } filepath.Ext(relpath) ipaURL := url.URL{ Scheme: "https", Host: r.Host, Path: relpath, } imgURL := url.URL{ Scheme: "https", Host: r.Host, Path: filepath.Join("/$ipaicon", relpath), } data, err := generateDownloadPlist(ipaURL.String(), imgURL.String(), plinfo) if err != nil { ctx.Error(500, err.Error()) return } w.Header().Set("Content-Type", "text/xml") w.Write(data) } }
func runBenchmark(fileInfoName, pathToPagesets, pathToPyFiles, localOutputDir, chromiumBuildName, chromiumBinary, runID, browserExtraArgs string) error { pagesetBaseName := filepath.Base(fileInfoName) if pagesetBaseName == util.TIMESTAMP_FILE_NAME || filepath.Ext(pagesetBaseName) == ".pyc" { // Ignore timestamp files and .pyc files. return nil } // Convert the filename into a format consumable by the run_benchmarks // binary. pagesetName := strings.TrimSuffix(pagesetBaseName, filepath.Ext(pagesetBaseName)) pagesetPath := filepath.Join(pathToPagesets, fileInfoName) glog.Infof("===== Processing %s for %s =====", pagesetPath, runID) skutil.LogErr(os.Chdir(pathToPyFiles)) args := []string{ util.BINARY_RUN_BENCHMARK, fmt.Sprintf("%s.%s", *benchmarkName, util.BenchmarksToPagesetName[*benchmarkName]), "--page-set-name=" + pagesetName, "--page-set-base-dir=" + pathToPagesets, "--also-run-disabled-tests", } // Need to capture output for all benchmarks. outputDirArgValue := filepath.Join(localOutputDir, pagesetName) args = append(args, "--output-dir="+outputDirArgValue) // Figure out which browser should be used. if *targetPlatform == util.PLATFORM_ANDROID { if err := installChromeAPK(chromiumBuildName); err != nil { return fmt.Errorf("Error while installing APK: %s", err) } args = append(args, "--browser=android-chrome-shell") } else { args = append(args, "--browser=exact", "--browser-executable="+chromiumBinary) } // Split benchmark args if not empty and append to args. if *benchmarkExtraArgs != "" { for _, benchmarkArg := range strings.Split(*benchmarkExtraArgs, " ") { args = append(args, benchmarkArg) } } // Add the number of times to repeat. args = append(args, fmt.Sprintf("--page-repeat=%d", *repeatBenchmark)) // Add browserArgs if not empty to args. if browserExtraArgs != "" { args = append(args, "--extra-browser-args="+browserExtraArgs) } // Set the PYTHONPATH to the pagesets and the telemetry dirs. env := []string{ fmt.Sprintf("PYTHONPATH=%s:%s:%s:$PYTHONPATH", pathToPagesets, util.TelemetryBinariesDir, util.TelemetrySrcDir), "DISPLAY=:0", } timeoutSecs := util.PagesetTypeToInfo[*pagesetType].RunChromiumPerfTimeoutSecs if err := util.ExecuteCmd("python", args, env, time.Duration(timeoutSecs)*time.Second, nil, nil); err != nil { glog.Errorf("Run benchmark command failed with: %s", err) glog.Errorf("Killing all running chrome processes in case there is a non-recoverable error.") skutil.LogErr(util.ExecuteCmd("pkill", []string{"-9", "chrome"}, []string{}, 5*time.Minute, nil, nil)) } return nil }
// this converts pngs and jpgs to smaller jpgs, and writes them out to the http connection, this thing EATS ALL THE MEMORY func generateThumb(w http.ResponseWriter, path string) { file, err := os.Open(path) if err != nil { fmt.Print("Error opening image:", err) } var img image.Image = nil if filepath.Ext(path) == ".png" { img, err = png.Decode(file) if err != nil { fmt.Print("Error decoding image:", err) return } file.Close() } else if filepath.Ext(path) == ".jpg" { img, err = jpeg.Decode(file) if err != nil { fmt.Print("Error decoding image:", err) return } file.Close() } else { return } // resize to height 200 err = jpeg.Encode(w, resize.Resize(0, 200, img, resize.NearestNeighbor), nil) if err != nil { fmt.Print("Error encoding thumb:", err) } }
func (dc *DocController) Prepare() { dc.BaseController.Prepare() // get correct file in doc directory p := dc.Ctx.Request.URL.Path p = strings.Replace(p, "/docs", "beedoc/"+dc.Lang, -1) p = strings.TrimRight(p, "/") if filepath.Ext(p) == "" { p += ".md" } dc.docFile = p // serve static file if filepath.Ext(p) != ".md" { http.ServeFile(dc.Ctx.ResponseWriter, dc.Ctx.Request, p) dc.StopRun() } // render md doc file if err := dc.renderDoc(); err != nil { dc.CustomAbort(503, err.Error()) return } }
func walkpath(path string, f os.FileInfo, err error, outfile *os.File, fchan chan FileDesc) error { filetype := "FILE" if f.IsDir() { filetype = "DIR" } outfile.WriteString(filetype) outfile.WriteString(",\t\"") outfile.WriteString(f.Name()) outfile.WriteString("\",\t") outfile.WriteString(filepath.Ext(path)) outfile.WriteString(",\t\"") outfile.WriteString(filepath.Dir(path)) outfile.WriteString("\",\t\"") outfile.WriteString(f.ModTime().String()) outfile.WriteString("\",\t") outfile.WriteString(strconv.Itoa(int(f.Size()))) outfile.WriteString(",\t\"") outfile.WriteString(path) outfile.WriteString("\"\r\n") fdesc := FileDesc{"0000", filetype, f.Name(), filepath.Ext(path), filepath.Dir(path), path, strconv.Itoa(int(f.Size())), f.ModTime().String()} fchan <- fdesc return nil }
// 트윗에 붙은 이미지가 여러개인 경우와 한개인 경우를 구분 func MakeMediaFileName(tweet *anaconda.Tweet, media anaconda.EntityMedia) string { mediaCount := len(tweet.ExtendedEntities.Media) if mediaCount <= 1 { url := FindMediaURL(media) ext := filepath.Ext(url) return fmt.Sprintf("%s%s", tweet.IdStr, ext) } found := -1 for i := 0; i < mediaCount; i++ { m := tweet.ExtendedEntities.Media[i] if m.Media_url == media.Media_url { found = i break } } if found < 0 { // not found return "" } num := found + 1 url := FindMediaURL(media) ext := filepath.Ext(url) return fmt.Sprintf("%s_%d%s", tweet.IdStr, num, ext) }
// loadTemplates walks sourceDir and loads all base templates into t. These // are files that have a '.tmpl' extensions and a leading underscore. The // template name is the filename with those stripped. func loadBaseTemplates(t *template.Template) error { return filepath.Walk(*sourceDir, func(path string, info os.FileInfo, err error) error { if err != nil { return err } if info.IsDir() { return nil } basename := filepath.Base(path) // Only handle files that start with _ if !strings.HasPrefix(basename, "_") { return nil } // Only handle files with ".tmpl" extension ext := filepath.Ext(basename) if ext != ".tmpl" { return nil } fmt.Printf("Loading template file: %v\n", path) // Strip off "_" and ".tmpl" name := strings.TrimPrefix(strings.TrimSuffix(basename, filepath.Ext(basename)), "_") data, err := ioutil.ReadFile(path) _, err = t.New(name).Parse(string(data)) if err != nil { return err } return nil }) }
func (g *goemon) externalCommand(command, file string) bool { var cmd *exec.Cmd command = os.Expand(command, func(s string) string { switch s { case "GOEMON_TARGET_FILE": return file case "GOEMON_TARGET_BASE": return filepath.Base(file) case "GOEMON_TARGET_DIR": return filepath.ToSlash(filepath.Dir(file)) case "GOEMON_TARGET_EXT": return filepath.Ext(file) case "GOEMON_TARGET_NAME": fn := filepath.Base(file) ext := filepath.Ext(file) return fn[:len(fn)-len(ext)] } return os.Getenv(s) }) if runtime.GOOS == "windows" { cmd = exec.Command("cmd", "/c", command) } else { cmd = exec.Command("sh", "-c", command) } g.Logger.Println("executing", command) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err := cmd.Run() if err != nil { g.Logger.Println(err) return false } return true }
func (p *FileResource) ContentTypesProvided(req Request, cxt Context) ([]MediaTypeHandler, Request, Context, int, os.Error) { frc := cxt.(FileResourceContext) var arr []MediaTypeHandler if frc.IsDir() { arr = []MediaTypeHandler{NewJsonDirectoryListing(frc.FullPath(), req.URL().Path), NewHtmlDirectoryListing(frc.FullPath(), req.URL().Path)} } else if frc.HasMultipleResources() { dir, _ := path.Split(frc.FullPath()) filenames := frc.MultipleResourceNames() arr = make([]MediaTypeHandler, len(filenames)) for i, filename := range filenames { extension := filepath.Ext(filename) mediaType := mime.TypeByExtension(extension) if len(mediaType) == 0 { // default to text/plain mediaType = MIME_TYPE_TEXT_PLAIN } fullFilename := path.Join(dir, filename) tempFrc := NewFileResourceContextWithPath(fullFilename) arr[i] = NewPassThroughMediaTypeHandler(mediaType, tempFrc, tempFrc.Len(), tempFrc.LastModified()) } } else { extension := filepath.Ext(frc.FullPath()) mediaType := mime.TypeByExtension(extension) if len(mediaType) == 0 { // default to text/plain mediaType = MIME_TYPE_TEXT_PLAIN } arr = []MediaTypeHandler{NewPassThroughMediaTypeHandler(mediaType, frc, frc.Len(), frc.LastModified())} } return arr, req, cxt, 0, nil }
func discoverGtfsPaths(path string) (results []string) { // log.Println("discoverGtfsPaths") path = filepath.Clean(path) fileInfo, err := os.Lstat(path) if err != nil { return } if fileInfo.IsDir() { file, err := os.Open(path) if err != nil { return } defer file.Close() fileInfos, err := file.Readdir(-1) if err != nil { return } requiredFiles := gtfs.RequiredFiles requiredCalendarFiles := gtfs.RequiredEitherCalendarFiles foundCalendar := false foundFiles := make([]string, 0, len(requiredFiles)) for _, fi := range fileInfos { name := fi.Name() if fi.IsDir() { subdirectoryResults := discoverGtfsPaths(path + "/" + name) for _, newpath := range subdirectoryResults { results = append(results, newpath) } } else if filepath.Ext(name) == ".zip" { results = append(results, path+"/"+name) } else { for _, f := range requiredFiles { if name == f { // This loops a little too much but hey... foundFiles = append(foundFiles, f) } } if !foundCalendar { for _, f := range requiredCalendarFiles { if name == f { foundCalendar = true } } } } } if len(foundFiles) == len(requiredFiles) && foundCalendar { results = append(results, path) } } else { if filepath.Ext(path) == ".zip" { results = append(results, path) } } return }
// GetMp3Tags returns a FileTags struct with // all the information obtained from the tags in the // MP3 file. // Includes the Artist, Album and Song and defines // default values if the values are missing. // If the tags are missing, the default values will // be stored on the file. // If the tags are obtained correctly the first // return value will be nil. func GetMp3Tags(path string) (error, FileTags) { mp3File, err := id3.Open(path) if err != nil { _, file := filepath.Split(path) extension := filepath.Ext(file) songTitle := file[0 : len(file)-len(extension)] return err, FileTags{songTitle, "unknown", "unknown"} } defer mp3File.Close() title := mp3File.Title() if title == "" || title == "unknown" { _, file := filepath.Split(path) extension := filepath.Ext(file) title = file[0 : len(file)-len(extension)] mp3File.SetTitle(title) } artist := mp3File.Artist() if artist == "" { artist = "unknown" mp3File.SetArtist(artist) } album := mp3File.Album() if album == "" { album = "unknown" mp3File.SetAlbum(album) } ft := FileTags{title, artist, album} return nil, ft }
// Match a torrent against the patterns of m. If all non-nil patterns match the // corresponding fields in torrent, then the method returns true. func (m *Matcher) Match(torrent *metadata.Metadata) bool { if m.Tracker != nil { if !m.Tracker.MatchString(torrent.Announce) { return false } } if m.Ext != nil { var exts []string if torrent.Info.SingleFileMode() { exts = append(exts, filepath.Ext(torrent.Info.Name)) } else { for _, file := range torrent.Info.Files { path := file.Path exts = append(exts, filepath.Ext(path[len(path)-1])) } } matches := false for _, ext := range exts { if m.Ext.MatchString(ext) { matches = true } } if !matches { return false } } if m.Basename != nil { basename := filepath.Base(torrent.Info.Name) if !m.Basename.MatchString(basename) { return false } } return true }
func (s *Scanner) findSQLFiles(pathToFile string, f os.FileInfo, err error) error { if filepath.Ext(f.Name()) == SQL_EXTENSTION { namespace := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name())) file, err := os.Open(pathToFile) defer file.Close() buffer := bufio.NewScanner(file) if err != nil { log.Fatal(err) } line := 1 for buffer.Scan() { fileInfo := &FileInfo{namespace, f.Name(), pathToFile} node := &Ast{fileInfo, line, buffer.Text(), buffer.Bytes(), ""} s.Buffer = append(s.Buffer, node) line = line + 1 } } return nil }
func PrecompiledTestSuite(path string) (TestSuite, error) { info, err := os.Stat(path) if err != nil { return TestSuite{}, err } if info.IsDir() { return TestSuite{}, errors.New("this is a directory, not a file") } if filepath.Ext(path) != ".test" { return TestSuite{}, errors.New("this is not a .test binary") } if info.Mode()&0111 == 0 { return TestSuite{}, errors.New("this is not executable") } dir := relPath(filepath.Dir(path)) packageName := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)) return TestSuite{ Path: dir, PackageName: packageName, IsGinkgo: true, Precompiled: true, }, nil }
func (s *Server) LoadStaticFiles() { log.Print("Rebuild from: ", Cfg.TemplatePath) Link(filepath.Join(Cfg.TemplatePath, "asset"), filepath.Join(Cfg.PublicPath, Cfg.BasePath, "asset")) log.Print("Loading Asset files") orig, err := filepath.Abs(filepath.Join(Cfg.TemplatePath, "asset")) if err != nil { log.Fatal(err) } filepath.Walk(orig, func(path string, info os.FileInfo, e error) (err error) { if info == nil || !info.Mode().IsRegular() { return } if !(filepath.Ext(path) == ".css" || filepath.Ext(path) == ".js") { return } log.Print("|- ", path) s.StaticFiles = append(s.StaticFiles, path) return }) }
// Get a list of the files in the AWS test suite. // func getAWSSuiteFiles(dir string) (files []string, err error) { d, err := os.Open(dir) if err != nil { return } f, err := d.Readdirnames(0) if err != nil { return } sort.Strings(f) files = make([]string, 0) for i := 0; i < len(f)-1; { if filepath.Ext(f[i]) == ".req" && filepath.Ext(f[i+1]) == ".sreq" { files = append(files, f[i][:len(f[i])-4]) i += 2 } else { i++ } } return }
func parseConfig() *viper.Viper { if verbose { logrus.SetLevel(logrus.DebugLevel) logrus.SetOutput(os.Stderr) } // Get home directory for current user homeDir, err := homedir.Dir() if err != nil { fatalf("Cannot get current user home directory: %v", err) } if homeDir == "" { fatalf("Cannot get current user home directory") } // By default our trust directory (where keys are stored) is in ~/.notary/ mainViper.SetDefault("trust_dir", filepath.Join(homeDir, filepath.Dir(configDir))) // If there was a commandline configFile set, we parse that. // If there wasn't we attempt to find it on the default location ~/.notary/config if configFile != "" { configFileExt = strings.TrimPrefix(filepath.Ext(configFile), ".") configFileName = strings.TrimSuffix(filepath.Base(configFile), filepath.Ext(configFile)) configPath = filepath.Dir(configFile) } else { configPath = filepath.Join(homeDir, filepath.Dir(configDir)) } // Setup the configuration details into viper mainViper.SetConfigName(configFileName) mainViper.SetConfigType(configFileExt) mainViper.AddConfigPath(configPath) // Find and read the config file err = mainViper.ReadInConfig() if err != nil { logrus.Debugf("Configuration file not found, using defaults") // If we were passed in a configFile via -c, bail if it doesn't exist, // otherwise ignore it: we can use the defaults if configFile != "" || !os.IsNotExist(err) { fatalf("error opening config file %v", err) } } // At this point we either have the default value or the one set by the config. // Either way, the command-line flag has precedence and overwrites the value if trustDir != "" { mainViper.Set("trust_dir", trustDir) } // Expands all the possible ~/ that have been given, either through -d or config // If there is no error, use it, if not, attempt to use whatever the user gave us expandedTrustDir, err := homedir.Expand(mainViper.GetString("trust_dir")) if err == nil { mainViper.Set("trust_dir", expandedTrustDir) } logrus.Debugf("Using the following trust directory: %s", mainViper.GetString("trust_dir")) return mainViper }
// Processing a book func (book *Book) process() { mimetype() fmt.Printf("%#v\n", book) toInclude := []string{"mimetype"} // mimetype MUST be the first file in the archive filepath.Walk(config.workDir, func(path string, info os.FileInfo, err error) error { if err == nil && ismember(filepath.Ext(path), &fileExtToInclude) { toInclude = append(toInclude, path) } return err }) fmt.Printf("Files to include: %s\n", toInclude) // Process is as follows // 1. get book details: author, title + list of files to process // 2. Process files individually // 3. Generate OPF from spine and metadata // 4. zip generated files and auxiliary files in .epub (this should be // done from the original "source" directory. // Step 4: if config.epubFile == "" { config.epubFile = strings.Replace(strings.Replace(book.title, " ", "-", -1), ",", "", -1) } if filepath.Ext(config.epubFile) != ".epub" { config.epubFile += ".epub" } fmt.Printf("Outputting to file: %s\n", config.epubFile) }
// Files gets a list of all manifest files inside of a chart. // // chartDir should contain the path to a chart (the directory which // holds a Chart.yaml file). // // This returns an error if it can't access the directory. func Files(chartDir string) ([]string, error) { dir := filepath.Join(chartDir, "manifests") files := []string{} if _, err := os.Stat(dir); err != nil { return files, err } // add manifest files walker := func(fname string, fi os.FileInfo, e error) error { if e != nil { log.Warn("Encountered error walking %q: %s", fname, e) return nil } if fi.IsDir() { return nil } if filepath.Ext(fname) == ".yaml" || filepath.Ext(fname) == ".yml" { files = append(files, fname) } return nil } filepath.Walk(dir, walker) return files, nil }
// Scan scans all the plugin paths and returns all the names it found func Scan() ([]string, error) { var names []string if err := filepath.Walk(socketsPath, func(path string, fi os.FileInfo, err error) error { if err != nil { return nil } if fi.Mode()&os.ModeSocket != 0 { name := strings.TrimSuffix(fi.Name(), filepath.Ext(fi.Name())) names = append(names, name) } return nil }); err != nil { return nil, err } for _, path := range specsPaths { if err := filepath.Walk(path, func(p string, fi os.FileInfo, err error) error { if err != nil || fi.IsDir() { return nil } name := strings.TrimSuffix(fi.Name(), filepath.Ext(fi.Name())) names = append(names, name) return nil }); err != nil { return nil, err } } return names, nil }
func (ths *ImageTagServer) go_list_dir(w http.ResponseWriter, r *http.Request) { var exts map[string]bool = map[string]bool{".jpg": true, ".png": true, ".JPG": true} path := r.URL.Query().Get("path") path = ths.imgRoot + path path = strings.Replace(path, "..", "", -1) println("Path: " + path) info, infoErr := os.Stat(path) if infoErr != nil { ths.GetEvents() <- "Path err: " + infoErr.Error() http.Error(w, infoErr.Error(), http.StatusNotFound) return } if info.IsDir() == false { ths.GetEvents() <- path + " - no such directory" http.Error(w, "No such directory", http.StatusNotFound) return } result := new(Directory) result.Name = info.Name() children := bytes.NewBuffer(nil) files, _ := ioutil.ReadDir(path) for file := range files { finfo, finfoErr := os.Stat(path + "/" + files[file].Name()) if finfoErr != nil { ths.GetEvents() <- finfoErr.Error() continue } if finfo.IsDir() == true { fname := filepath.Base(files[file].Name()) children.WriteString(fname + "|" + ITEM_TYPE_DIR + ";") } else { ths.events <- ("Extension is: " + filepath.Ext(path+finfo.Name())) if _, ok := exts[filepath.Ext(path+finfo.Name())]; ok { fname := filepath.Base(files[file].Name()) children.WriteString(fname + "|" + ITEM_TYPE_IMG + ";") } } } result.Children = children.String() println(result.Children) jsonStr, jsonStrErr := json.Marshal(result) if jsonStrErr != nil { ths.GetEvents() <- jsonStrErr.Error() } println(string(jsonStr)) w.Write(jsonStr) return }
// AssetTree provides a map tree of files across the given directory that match the filenames being used func AssetTree(dir string, ext string) (AssetMap, error) { var stat os.FileInfo var err error //do the path exists if stat, err = os.Stat(dir); err != nil { return nil, err } var tree = make(AssetMap) //do we have a directory? if !stat.IsDir() { var fext string var rel = filepath.Base(dir) if strings.Index(rel, ".") != -1 { fext = filepath.Ext(rel) } if fext == ext { tree[rel] = filepath.ToSlash(dir) } } else { filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { //if info is nil or is a directory when we skip if info == nil || info.IsDir() { return nil } var rel string var rerr error //is this path relative to the current one, if not,return err if rel, rerr = filepath.Rel(dir, path); rerr != nil { return rerr } var fext string if strings.Index(rel, ".") != -1 { fext = filepath.Ext(rel) } if fext == ext { tree[rel] = filepath.ToSlash(path) // tree[strings.TrimSuffix(rel, ext)] = filepath.ToSlash(path) } return nil }) } return tree, nil }
func List(dir string) []models.FileInfo { list := []models.FileInfo{} fi := models.FileInfo{} if info, err := os.Stat(dir); err == nil && info.IsDir() { f, _ := ioutil.ReadDir(dir) for _, entry := range f { namePresent := "" fileNotExt := strings.TrimSuffix(entry.Name(), filepath.Ext(entry.Name())) taille, nomTaille := Taille(float64(entry.Size())) if a, _ := SrtOrNot(entry.Name()); a != true { for _, entry2 := range f { tailleSrt, SrtNametaillle := Taille(float64(entry2.Size())) if a, b := SrtOrNot(entry2.Name()); a == true { if fileNotExt == b { namePresent = entry.Name() fi = models.FileInfo{ Name: entry.Name(), NameExt: filepath.Ext(entry.Name()), Size: float64(int(taille*100)) / 100, Mode: entry.Mode(), ModTime: entry.ModTime(), IsDir: entry.IsDir(), NameSize: nomTaille, Srt: 1, SizeSrt: float64(int(tailleSrt*100)) / 100, NameTailleSrt: SrtNametaillle, GetUid: entry.Sys().(*syscall.Stat_t).Uid, GetGid: entry.Sys().(*syscall.Stat_t).Gid, } fi.Lock() list = append(list, fi) fi.Unlock() } } } if namePresent != entry.Name() { fi = models.FileInfo{ Name: entry.Name(), NameExt: filepath.Ext(entry.Name()), Size: float64(int(taille*100)) / 100, Mode: entry.Mode(), ModTime: entry.ModTime(), IsDir: entry.IsDir(), NameSize: nomTaille, Srt: 0, GetUid: entry.Sys().(*syscall.Stat_t).Uid, GetGid: entry.Sys().(*syscall.Stat_t).Gid, } fi.Lock() list = append(list, fi) fi.Unlock() } } } } return list }