Last active
December 27, 2022 20:28
-
-
Save hotpxl/bdb881a520bf66b6da2058ffb13d02da to your computer and use it in GitHub Desktop.
Remove extra extended attributes
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/xattr.h> | |
char const* kPatterns[] = { | |
"com.apple.quarantine", "com.apple.lastuseddate#PS", | |
"com.apple.metadata:kMDItemWhereFroms", | |
"com.apple.metadata:_kMDItemUserTags", | |
"com.apple.metadata:kMDLabel_nhfihrek4vfesitn3irk5qye5q"}; | |
int main(int args, char** argv) { | |
if (args != 2) { | |
fprintf(stderr, "Usage: %s <file>\n", argv[0]); | |
exit(1); | |
} | |
int const kOptions = XATTR_NOFOLLOW; | |
char* filename = argv[1]; | |
int size = listxattr(filename, NULL, 0, kOptions); | |
if (size == -1) { | |
perror("Getting size of attributes"); | |
exit(1); | |
} | |
char* name_buffer = (char*)calloc(size, 1); | |
listxattr(filename, name_buffer, size, kOptions); | |
if (size == -1) { | |
perror("Listing attributes"); | |
free(name_buffer); | |
exit(1); | |
} | |
int pos = 0; | |
while (pos < size) { | |
char* attribute = name_buffer + pos; | |
int match = 0; | |
for (int i = 0; i < sizeof(kPatterns) / sizeof(char const*); ++i) { | |
if (strcmp(attribute, kPatterns[i]) == 0) { | |
match = 1; | |
break; | |
} | |
} | |
if (match) { | |
int ret = removexattr(filename, attribute, kOptions); | |
if (ret == -1) { | |
perror("Removing attributes"); | |
free(name_buffer); | |
exit(1); | |
} | |
printf("Removed attribute \"%s\" from \"%s\".\n", attribute, filename); | |
} else { | |
printf("Not removing attribute \"%s\" from \"%s\".\n", attribute, | |
filename); | |
} | |
pos += strlen(attribute) + 1; | |
} | |
free(name_buffer); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment