Beispiel #1
0
func init() {
	var err error
	mm, err = magicmime.New(magicmime.MAGIC_MIME_TYPE | magicmime.MAGIC_ERROR | magicmime.MAGIC_SYMLINK)
	if err != nil {
		panic(err)
	}

	RegisterExtractor("", &defaultExtractor{})
}
Beispiel #2
0
func processPath(path string, info os.FileInfo, err error) error {
	//buffer to build string
	var buffer bytes.Buffer
	//file properties
	stat, err := os.Stat(path)
	if err != nil {
		log.Printf("Unable to stat %s: %s", path, err)
	}
	//permissions
	perms := stat.Mode()
	buffer.WriteString(fmt.Sprintf("%s ", perms))
	//size
	size := stat.Size()
	buffer.WriteString(fmt.Sprintf("%d ", size))
	//uid/gid
	uid := info.Sys().(*syscall.Stat_t).Uid
	gid := info.Sys().(*syscall.Stat_t).Gid
	user := getUserFromUid(int(uid))
	group := getGroupFromGid(int(gid))
	buffer.WriteString(fmt.Sprintf("%s ", user))
	buffer.WriteString(fmt.Sprintf("%s ", group))
	if stat.IsDir() {
		//directory
		buffer.WriteString(fmt.Sprintf("%s", path))
	} else {
		//mimetype detection
		mm, err := magicmime.New(magicmime.MAGIC_MIME_TYPE | magicmime.MAGIC_SYMLINK | magicmime.MAGIC_ERROR)
		if err != nil {
			log.Panicf("Problem evaluating libmagic detection: %s", err)
		}
		mimetype, err := mm.TypeByFile(path)
		if err != nil {
			log.Printf("Unable to determine file mimetype: %s", err)
			return nil
		}
		//read file content
		contents, err := ioutil.ReadFile(path)
		if err != nil {
			log.Printf("Unable to read %s: %s", path, err)
		}
		//process
		data := []byte(contents)
		if mimetype != "application/octet-stream" {
			//standard file
			buffer.WriteString(fmt.Sprintf("%s md5=%x", path, md5.Sum(data)))
		} else {
			//binary file
			//computing the md5sum for a binary file is irrelevant, as there might
			//be different pre-linking between machines, hence different sum for
			//the same binary
			buffer.WriteString(fmt.Sprintf("%s", path))
		}
	}
	fmt.Println(buffer.String())
	return nil
}
func RefreshBuilds() {
	log.Println("Processing files...")

	// Remove any missing files from the DB
	PruneMissingFiles()

	// Check for files in build directory that match zip MIME
	mm, _ := magicmime.New(magicmime.MAGIC_MIME_TYPE | magicmime.MAGIC_SYMLINK | magicmime.MAGIC_ERROR)
	files, _ := ioutil.ReadDir(BuildsPath)
	for _, f := range files {
		filepath := strings.Join([]string{BuildsPath, f.Name()}, "/")
		mimetype, _ := mm.TypeByFile(filepath)
		if mimetype == "application/java-archive" {
			existingFile, err := FindFileByName(f.Name())
			if err != nil {
				// Extract build props from file in zip
				props := BuildPropsFromZip(filepath)

				// Generate file struct using properties
				file := File{
					Name:        f.Name(),
					Size:        f.Size(),
					Md5:         Md5File(filepath),
					BuildDate:   props["ro.build.date.utc"],
					ApiLevel:    props["ro.build.version.sdk"],
					Incremental: props["ro.build.version.incremental"],
					Device:      props["ro.product.name"],
					User:        props["ro.build.user"],
					Published:   false,
				}
				// Insert file in database
				CreateFile(file)
			} else {
				// Insert file in database
				existingFile.Published = false
				UpdateFile(existingFile)
				log.Println("Refresh Builds: File exists, skipping")
			}
		} else {
			log.Println("Refresh Builds: File skipped invalid MIME", mimetype)
		}
	}

	// Update db published flag for files in published folder
	publishedPath := strings.Join([]string{BuildsPath, "published"}, "/")
	publishedFiles, _ := ioutil.ReadDir(publishedPath)
	for _, f := range publishedFiles {
		file, _ := FindFileByName(f.Name())
		file.Published = true
		UpdateFile(file)
	}
}
Beispiel #4
0
// getMimeType returns the file mime type.
func getMimeType(file string) string {
	mm, err := magicmime.New(magicmime.MAGIC_MIME_TYPE | magicmime.MAGIC_SYMLINK | magicmime.MAGIC_ERROR)
	if err != nil {
		numErrors++
		return DefaultMimeType
	}

	mimetype, err := mm.TypeByFile(file)
	if err != nil {
		numErrors++
		return DefaultMimeType
	}

	return mimetype
}
Beispiel #5
0
func Example_1() {
	mm, err := magicmime.New(magicmime.MAGIC_MIME_TYPE | magicmime.MAGIC_SYMLINK | magicmime.MAGIC_ERROR)
	if err != nil {
		panic(err)
	}

	filepath := "/bin/ls"

	mimetype, err := mm.TypeByFile(filepath)
	if err != nil {
		fmt.Printf("Something went wrong: %s", err)
		return
	}

	fmt.Printf("%s -> %s\n", filepath, mimetype)
}
Beispiel #6
0
func InitMagic() error {
	var err error
	Magic, err = magicmime.New(magicmime.MAGIC_MIME_TYPE | magicmime.MAGIC_SYMLINK | magicmime.MAGIC_ERROR)
	return err
}