void parseWhereFrom(QueryData &results, const std::string &path) { CFStringRef CFPath = CFStringCreateWithCString( kCFAllocatorDefault, path.c_str(), kCFStringEncodingUTF8); MDItemRef metadata = MDItemCreate(kCFAllocatorDefault, CFPath); CFRelease(CFPath); if (metadata == nullptr) { VLOG(1) << "Metadata for " << path << " is null"; return; } CFTypeRef attributes; attributes = MDItemCopyAttribute(metadata, kMDItemWhereFroms); CFRelease(metadata); if (attributes == nullptr) { VLOG(1) << "No attributes found for " << path; return; } CFArrayRef attribs = (CFArrayRef)attributes; CFIndex count = CFArrayGetCount(attribs); for (CFIndex i = 0; i < count; i++) { CFStringRef attribute = (CFStringRef)CFArrayGetValueAtIndex(attribs, i); auto where_from_attribute = stringFromCFString(attribute); if (!where_from_attribute.empty()) { setRow(results, path, "where_from", where_from_attribute); } } CFRelease(attributes); }
VALUE method_search(VALUE self, VALUE queryString, VALUE scopeDirectories) { VALUE result = Qnil; int i; CFStringRef path; MDItemRef item; CFStringRef qs = rbstr2cfstring(queryString); MDQueryRef query = MDQueryCreate(kCFAllocatorDefault, qs, NULL, NULL); RELEASE_IF_NOT_NULL(qs); if (query) { set_search_scope(query, scopeDirectories); if (MDQueryExecute(query, kMDQuerySynchronous)) { result = rb_ary_new(); for(i = 0; i < MDQueryGetResultCount(query); ++i) { item = (MDItemRef) MDQueryGetResultAtIndex(query, i); if (item) { path = MDItemCopyAttribute(item, kMDItemPath); rb_ary_push(result, cfstring2rbstr(path)); RELEASE_IF_NOT_NULL(path); } } } RELEASE_IF_NOT_NULL(query); } return result; }
VALUE method_get_attribute(VALUE self, VALUE path, VALUE name) { MDItemRef mdi = createMDItemByPath(path); CFStringRef nameRef = rbstr2cfstring(name); CFTypeRef valueRef = MDItemCopyAttribute(mdi, nameRef); VALUE result = convert2rb_type(valueRef); RELEASE_IF_NOT_NULL(valueRef); RELEASE_IF_NOT_NULL(nameRef); RELEASE_IF_NOT_NULL(mdi); return result; }
VALUE method_attributes(VALUE self, VALUE path) { int i; CFStringRef attrNameRef; CFTypeRef attrValueRef; MDItemRef mdi = createMDItemByPath(path); CFArrayRef attrNamesRef = MDItemCopyAttributeNames(mdi); VALUE result = rb_hash_new(); for (i = 0; i < CFArrayGetCount(attrNamesRef); i++) { attrNameRef = CFArrayGetValueAtIndex(attrNamesRef, i); attrValueRef = MDItemCopyAttribute(mdi, attrNameRef); rb_hash_aset(result, cfstring2rbstr(attrNameRef), convert2rb_type(attrValueRef)); RELEASE_IF_NOT_NULL(attrValueRef); } RELEASE_IF_NOT_NULL(mdi); RELEASE_IF_NOT_NULL(attrNamesRef); return result; }