Created
December 21, 2017 00:35
-
-
Save adriansr/47c30580914228ff432c58cb751eda68 to your computer and use it in GitHub Desktop.
Read kMDItemWhereFroms in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
#include <stdlib.h> | |
#include <sys/xattr.h> | |
*/ | |
import "C" | |
import ( | |
"fmt" | |
"os" | |
"unsafe" | |
"howett.net/plist" | |
) | |
var key = C.CString("com.apple.metadata:kMDItemWhereFroms") | |
func main() { | |
if len(os.Args) != 2 { | |
fmt.Printf("Usage: %s <path>\n", os.Args[0]) | |
return | |
} | |
path := os.Args[1] | |
cpath := C.CString(path) | |
defer C.free(unsafe.Pointer(cpath)) | |
nbytes, err := C.getxattr(cpath, key, nil, 0, 0, 0) | |
if nbytes == -1 { | |
fmt.Printf("getxattr failed: %s\n", err) | |
return | |
} | |
if nbytes == 0 { | |
fmt.Printf("No data\n") | |
return | |
} | |
fmt.Printf("Got %d bytes\n", nbytes) | |
mem := make([]byte, nbytes) | |
size2, err := C.getxattr(cpath, key, unsafe.Pointer(&mem[0]), C.size_t(nbytes), 0, 0) | |
if size2 != nbytes { | |
fmt.Printf("Size mismatch\n") | |
return | |
} | |
fmt.Printf("Got '%s'\n", mem) | |
var urls []string | |
format, err := plist.Unmarshal(mem, &urls) | |
fmt.Printf("Result format:%d err:%v urls:%+v\n", format, err, urls) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment