static void test_by_data (const char *dir, const char *filename, const char *mt_expected, int xfail) { FILE *file; const char *mt; int max_extent; char *data; int bytes_read; int result_prio; char path[1024]; snprintf (path, 1024, "%s/%s", dir, filename); file = fopen (path, "r"); if (file == NULL) { printf ("Could not open %s\n", path); error++; return; } max_extent = xdg_mime_get_max_buffer_extents (); data = malloc (max_extent); if (data == NULL) { printf ("Failed to allocate memory for file %s\n", filename); error++; return; } bytes_read = fread (data, 1, max_extent, file); if (ferror (file)) { printf ("Error reading file %s\n", path); error++; free (data); fclose (file); return; } mt = xdg_mime_get_mime_type_for_data (data, bytes_read, &result_prio); free (data); fclose (file); check_mime_type (mt, mt_expected, xfail, "data", filename); }
static void test_by_name (const char *filename, const char *mt_expected, int xfail) { const char *mt; mt = xdg_mime_get_mime_type_from_file_name (filename); check_mime_type (mt, mt_expected, xfail, "name", filename); }
static void test_by_file (const char *dir, const char *filename, const char *mt_expected, int xfail) { const char *mt; char path[1024]; snprintf (path, 1024, "%s/%s", dir, filename); mt = xdg_mime_get_mime_type_for_file (path, NULL); check_mime_type (mt, mt_expected, xfail, "file", filename); }
// main int main(int argc, const char *const *argv) { kArgc = argc; kArgv = argv; // parameters const char **files = new const char*[argc]; int fileCount = 0; const char *type = NULL; const char *signature = NULL; const char *preferredApp = NULL; // parse the arguments for (int argi = 1; argi < argc; ) { const char *arg = argv[argi++]; if (arg[0] == '-') { if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) { print_usage_and_exit(false); } else if (strcmp(arg, "-preferredAppSig") == 0) { preferredApp = next_arg(argi); } else if (strcmp(arg, "-s") == 0) { signature = next_arg(argi); } else if (strcmp(arg, "-t") == 0) { type = next_arg(argi); } else { fprintf(stderr, "Error: Invalid option: \"%s\"\n", arg); print_usage_and_exit(true); } } else { // file files[fileCount++] = arg; } } // check parameters if (!preferredApp && !signature && !type) { fprintf(stderr, "Error: At least one option of \"-preferredAppSig\", " "\"-s\", and \"-t\" must be given.\n"); print_usage_and_exit(true); } if (fileCount == 0) { fprintf(stderr, "Error: No file specified.\n"); print_usage_and_exit(true); } // check for valid MIME types check_mime_type(preferredApp); check_mime_type(type); check_mime_type(signature); // iterate through the files for (int i = 0; i < fileCount; i++) { const char *fileName = files[i]; // check, whether the file exists BEntry entry; status_t error = entry.SetTo(fileName, false); if (error != B_OK) { fprintf(stderr, "Error: Can't access file \"%s\": %s\n", fileName, strerror(error)); exit(1); } if (!entry.Exists()) { fprintf(stderr, "Error: \"%s\": No such file or directory.\n", fileName); exit(1); } // ... and has the right type if (signature && !entry.IsFile()) { fprintf(stderr, "Error: \"%s\" is not a file. Signatures can only " "be set for executable files.\n", fileName); exit(1); } // open the file BFile file; BNode _node; BNode &node = (signature ? file : _node); error = (signature ? file.SetTo(fileName, B_READ_WRITE) : node.SetTo(fileName)); if (error != B_OK) { fprintf(stderr, "Error: Failed to open file \"%s\": %s\n", fileName, strerror(error)); exit(1); } // prepare an node/app info object BAppFileInfo appInfo; BNodeInfo _nodeInfo; BNodeInfo &nodeInfo = (signature ? appInfo : _nodeInfo); error = (signature ? appInfo.SetTo(&file) : nodeInfo.SetTo(&node)); if (error != B_OK) { fprintf(stderr, "Error: Failed to open file \"%s\": %s\n", fileName, strerror(error)); exit(1); } // set preferred app if (preferredApp) { error = nodeInfo.SetPreferredApp(preferredApp); if (error != B_OK) { fprintf(stderr, "Error: Failed to set the preferred " "application of file \"%s\": %s\n", fileName, strerror(error)); exit(1); } } // set type if (type) { error = nodeInfo.SetType(type); if (error != B_OK) { fprintf(stderr, "Error: Failed to set the MIME type of file " "\"%s\": %s\n", fileName, strerror(error)); exit(1); } } // set signature if (signature) { error = appInfo.SetSignature(signature); if (error != B_OK) { fprintf(stderr, "Error: Failed to set the signature of file " "\"%s\": %s\n", fileName, strerror(error)); exit(1); } } } return 0; }