int tool_main(int argc, char** argv) { SkCommandLineFlags::SetUsage("Decode files, and optionally write the results to files."); SkCommandLineFlags::Parse(argc, argv); if (FLAGS_readPath.count() < 1) { SkDebugf("Folder(s) or image(s) to decode are required.\n"); return -1; } SkAutoGraphics ag; for (int i = 0; i < FLAGS_readPath.count(); i++) { const char* readPath = FLAGS_readPath[i]; if (strlen(readPath) < 1) { break; } if (sk_isdir(readPath)) { const char* dir = readPath; SkOSFile::Iter iter(dir); SkString filename; while (iter.next(&filename)) { if (!is_image_file(filename.c_str())) { continue; } SkString fullname = SkOSPath::SkPathJoin(dir, filename.c_str()); decodeFileAndWrite(fullname.c_str()); } } else if (sk_exists(readPath) && is_image_file(readPath)) { decodeFileAndWrite(readPath); } } return 0; }
gchar* get_image_path_from_dir (const gchar *path) { GError *error = NULL; GDir *dir = NULL; const gchar *next_file = NULL; gchar *ab_file = NULL; gchar *result = NULL; dir = g_dir_open(path, 0, &error); if (!dir) { g_critical("Unable to open dir: %s", path); g_error_free(error); return NULL; } next_file = g_dir_read_name(dir); while (next_file) { ab_file = g_strconcat(path, G_DIR_SEPARATOR_S, next_file, NULL); if (g_file_test(ab_file, G_FILE_TEST_IS_REGULAR) && is_image_file(ab_file)) { result = ab_file; goto exit; } g_free(ab_file); next_file = g_dir_read_name(dir); } exit: g_dir_close(dir); return result; }
gchar* get_pref_image_path_dir (PraghaPreferences *preferences, const gchar *path) { GError *error = NULL; GDir *dir = NULL; const gchar *next_file = NULL; gchar *ab_file = NULL, **pattern; const gchar *patterns = NULL; GSList *file_list = NULL; gint i = 0; patterns = pragha_preferences_get_album_art_pattern(preferences); if (string_is_empty(patterns)) return NULL; /* Form a list of all files in the given dir */ dir = g_dir_open(path, 0, &error); if (!dir) { g_critical("Unable to open dir: %s", path); g_error_free(error); return NULL; } next_file = g_dir_read_name(dir); while (next_file) { ab_file = g_strconcat(path, G_DIR_SEPARATOR_S, next_file, NULL); if (g_file_test(ab_file, G_FILE_TEST_IS_REGULAR)) file_list = g_slist_append(file_list, g_strdup(next_file)); g_free(ab_file); next_file = g_dir_read_name(dir); } g_dir_close(dir); /* Now, run the preferred patterns through them */ pattern = g_strsplit(patterns, ";", ALBUM_ART_NO_PATTERNS); while (pattern[i]) { if (is_present_str_list(pattern[i], file_list)) { ab_file = g_strconcat(path, G_DIR_SEPARATOR_S, pattern[i], NULL); if (is_image_file(ab_file)) return ab_file; g_free(ab_file); } i++; } /* Cleanup */ g_slist_free_full(file_list, g_free); g_strfreev(pattern); return NULL; }
int get_directory_images(const char* Folder, std::vector<string> &fileList) { int i=0; DIR* dir; struct dirent *ent; int numFiles=0; dir = opendir(Folder); fileList.clear(); if (dir) { /* print all the files and directories within directory */ while ((ent = readdir (dir))) { struct dirent *temp=(struct dirent*)malloc(sizeof(struct dirent)); //memcpy(temp->d_name,ent->d_name,sizeof(TChar)*ent->d_namlen); strcpy(temp->d_name,ent->d_name); temp->d_namlen=ent->d_namlen; temp->d_type=ent->d_type; switch (ent->d_type) { case DT_REG: if (is_image_file(temp->d_name)) { fileList.push_back(string( temp->d_name )); //t_list_push_list(&fileList,temp); numFiles++; break; } default: break; } } closedir (dir); } else { printf("Couldn't open folder!\n"); return 0; } return i; }
// Relative path static int process_file(char *filename, struct s_category *category) { //printf("Processing filename : %s\n", filename); if (is_markdown_file(filename)) return process_mkd(filename, category); if (is_image_file(filename)) return process_image(filename); // if (is_document_file(filename)) { // return process_document(filename); // } // is (is_music_file(filename)) { // return process_music(filename); // } // TODO : add_header_footer // return -1; }
/// Start a task in a slot directory. /// This includes setting up soft links, /// passing preferences, and starting the process. /// /// Current dir is top-level Synecdoche dir. /// /// \post /// - If any error occurs /// - #task_state is #PROCESS_COULDNT_START /// - CLIENT_STATE::report_result_error() is called /// - else /// - #task_state is #PROCESS_EXECUTING /// /// \return 0 on success, nonzero otherwise. int ACTIVE_TASK::start() { char exec_name[256], exec_path[256]; unsigned int i; FILE_REF fref; int retval; // F*** goto, need to define some variables here instead of where they are used! std::ostringstream err_stream; #ifdef _WIN32 std::string cmd_line; std::string slotdirpath; #else // Needs to be defined here because those gotos would skip the // initialization of 'cmdline' and 'argv' if it would be defined later. std::ostringstream cmdline; std::list<std::string> argv; #endif if ((!full_init_done) && (log_flags.task)) { msg_printf(wup->project, MSG_INFO, "Starting %s", result->name ); } if (log_flags.cpu_sched) { msg_printf(wup->project, MSG_INFO, "[cpu_sched] Starting %s%s", result->name, (full_init_done) ? " (resume)" : " (initial)" ); } // Always check if all required files are present. If not, trigger // re-downloads and don't start the science application. FILE_INFO_PSET missing_file_infos; retval = gstate.input_files_available(result, true, &missing_file_infos); if (retval) { for (FILE_INFO_PSET::iterator it = missing_file_infos.begin(); it != missing_file_infos.end(); ++it) { FILE_INFO* fip = *it; if (fip) { err_stream << "Input file " << fip->name << " missing or invalid: " << retval; } else { err_stream << "Input file missing or invalid"; // We can't trigger a new download if we don't have // any file information. Just fail here as before. goto error; } fip->status = FILE_NOT_PRESENT; } } if (!missing_file_infos.empty()) { // Some files are missing and are set for re-transfer. // Update status and return without error. result->set_state(RESULT_FILES_DOWNLOADING, "start"); set_task_state(PROCESS_UNINITIALIZED, "start"); next_scheduler_state = PROCESS_UNINITIALIZED; return 0; } if (!full_init_done) { checkpoint_cpu_time = 0; checkpoint_wall_time = gstate.now; } current_cpu_time = checkpoint_cpu_time; episode_start_cpu_time = checkpoint_cpu_time; debt_interval_start_cpu_time = checkpoint_cpu_time; graphics_request_queue.init(result->name); // reset message queues process_control_queue.init(result->name); if (!app_client_shm.shm) { retval = get_shmem_seg_name(); if (retval) { err_stream << "Can't get shared memory segment name: " << boincerror(retval); goto error; } } // this must go AFTER creating shmem name, // since the shmem name is part of the file // retval = write_app_init_file(); if (retval) { err_stream << "Can't write init file: " << retval; goto error; } // set up applications files // strcpy(exec_name, ""); for (i=0; i<app_version->app_files.size(); i++) { fref = app_version->app_files[i]; FILE_INFO* fip = fref.file_info; std::string file_path = get_pathname(fip); if (fref.main_program) { if (is_image_file(fip->name)) { err_stream << "Main program " << fip->name << " is an image file"; retval = ERR_NO_SIGNATURE; goto error; } if (!fip->executable && !wup->project->anonymous_platform) { err_stream << "Main program " << fip->name << " is not executable"; retval = ERR_NO_SIGNATURE; goto error; } safe_strcpy(exec_name, fip->name.c_str()); safe_strcpy(exec_path, file_path.c_str()); } // anonymous platform may use different files than // when the result was started, so link files even if not first time if ((!full_init_done) || (wup->project->anonymous_platform)) { retval = setup_file(result->project, fip, fref, file_path, slot_dir, true); if (retval) { err_stream << "Can't link input file"; goto error; } } } if (!strlen(exec_name)) { err_stream << "No main program specified"; retval = ERR_NOT_FOUND; goto error; } // set up input, output files if (!full_init_done) { for (i=0; i<wup->input_files.size(); i++) { fref = wup->input_files[i]; const FILE_INFO* fip = fref.file_info; std::string file_path = get_pathname(fref.file_info); retval = setup_file(result->project, fip, fref, file_path, slot_dir, true); if (retval) { err_stream << "Can't link input file"; goto error; } } for (i=0; i<result->output_files.size(); i++) { fref = result->output_files[i]; if (fref.copy_file) continue; const FILE_INFO* fip = fref.file_info; std::string file_path = get_pathname(fref.file_info); retval = setup_file(result->project, fip, fref, file_path, slot_dir, false); if (retval) { err_stream << "Can't link output file"; goto error; } } full_init_done = true; } link_user_files(); if (gstate.exit_before_start) { exit(0); } #ifdef _WIN32 PROCESS_INFORMATION process_info; STARTUPINFO startup_info; LPVOID environment_block = NULL; char error_msg[1024]; char error_msg2[1024]; memset(&process_info, 0, sizeof(process_info)); memset(&startup_info, 0, sizeof(startup_info)); startup_info.cb = sizeof(startup_info); // suppress 2-sec rotating hourglass cursor on startup // startup_info.dwFlags = STARTF_FORCEOFFFEEDBACK; app_client_shm.reset_msgs(); if (config.run_apps_manually) { // fill in core client's PID so we won't think app has exited pid = GetCurrentProcessId(); pid_handle = GetCurrentProcess(); set_task_state(PROCESS_EXECUTING, "start"); return 0; } // NOTE: in Windows, stderr is redirected in boinc_init_diagnostics(); cmd_line = exec_path + std::string(" ") + wup->command_line; if (strlen(app_version->cmdline)) { cmd_line += std::string(" ") + app_version->cmdline; } slotdirpath = relative_to_absolute(slot_dir); bool success = false; for (i=0; i<5; i++) { if (sandbox_account_service_token != NULL) { // Find CreateEnvironmentBlock/DestroyEnvironmentBlock pointers tCEB pCEB = NULL; tDEB pDEB = NULL; HMODULE hUserEnvLib = NULL; hUserEnvLib = LoadLibrary("userenv.dll"); if (hUserEnvLib) { pCEB = (tCEB) GetProcAddress(hUserEnvLib, "CreateEnvironmentBlock"); pDEB = (tDEB) GetProcAddress(hUserEnvLib, "DestroyEnvironmentBlock"); } if (!pCEB(&environment_block, sandbox_account_service_token, FALSE)) { if (log_flags.task) { windows_error_string(error_msg, sizeof(error_msg)); msg_printf(wup->project, MSG_INFO, "Process environment block creation failed: %s", error_msg ); } } if (CreateProcessAsUser( sandbox_account_service_token, exec_path, (LPSTR)cmd_line.c_str(), NULL, NULL, FALSE, CREATE_NEW_PROCESS_GROUP|CREATE_NO_WINDOW|IDLE_PRIORITY_CLASS|CREATE_UNICODE_ENVIRONMENT, environment_block, slotdirpath.c_str(), &startup_info, &process_info )) { success = true; break; } else { windows_error_string(error_msg, sizeof(error_msg)); msg_printf(wup->project, MSG_INTERNAL_ERROR, "Process creation failed: %s", error_msg ); } if (!pDEB(environment_block)) { if (log_flags.task) { windows_error_string(error_msg, sizeof(error_msg2)); msg_printf(wup->project, MSG_INFO, "Process environment block cleanup failed: %s", error_msg2 ); } } if (hUserEnvLib) { pCEB = NULL; pDEB = NULL; FreeLibrary(hUserEnvLib); } } else { if (CreateProcess( exec_path, (LPSTR)cmd_line.c_str(), NULL, NULL, FALSE, CREATE_NEW_PROCESS_GROUP|CREATE_NO_WINDOW|IDLE_PRIORITY_CLASS, NULL, slotdirpath.c_str(), &startup_info, &process_info )) { success = true; break; } else { windows_error_string(error_msg, sizeof(error_msg)); msg_printf(wup->project, MSG_INTERNAL_ERROR, "Process creation failed: %s", error_msg ); } } boinc_sleep(drand()); } if (!success) { err_stream << "CreateProcess() failed - " << error_msg; retval = ERR_EXEC; goto error; } pid = process_info.dwProcessId; pid_handle = process_info.hProcess; CloseHandle(process_info.hThread); // thread handle is not used #else // Unix/Linux/Mac case // Set up core/app shared memory seg if needed // if (!app_client_shm.shm) { if (app_version->api_major_version() >= 6) { // Use mmap() shared memory std::string buf = slot_dir + std::string("/") + std::string(MMAPPED_FILE_NAME); if (g_use_sandbox) { if (!boinc_file_exists(buf.c_str())) { int fd = open(buf.c_str(), O_RDWR | O_CREAT, 0660); if (fd >= 0) { close (fd); #ifdef SANDBOX set_to_project_group(buf.c_str()); #endif } } } retval = create_shmem_mmap( buf.c_str(), sizeof(SHARED_MEM), (void**)&app_client_shm.shm ); } else { // Use shmget() shared memory retval = create_shmem( shmem_seg_name, sizeof(SHARED_MEM), gstate.boinc_project_gid, (void**)&app_client_shm.shm ); if (retval) { needs_shmem = true; destroy_shmem(shmem_seg_name); return retval; } } needs_shmem = false; } app_client_shm.reset_msgs(); #if (defined (__APPLE__) && (defined(__i386__) || defined(__x86_64__))) // PowerPC apps emulated on i386 Macs crash if running graphics powerpc_emulated_on_i386 = ! is_native_i386_app(exec_path); #endif if (config.run_apps_manually) { pid = getpid(); // use the client's PID set_task_state(PROCESS_EXECUTING, "start"); return 0; } // Prepare command line for the science app: cmdline << wup->command_line; if (strlen(app_version->cmdline)) { cmdline << ' ' << app_version->cmdline; } argv = parse_command_line(cmdline.str().c_str()); if (log_flags.task_debug) { debug_print_argv(argv); } pid = fork(); if (pid == -1) { err_stream << "fork() failed: " << strerror(errno); retval = ERR_FORK; goto error; } if (pid == 0) { // from here on we're running in a new process. // If an error happens, // exit nonzero so that the core client knows there was a problem. // don't pass stdout to the app // int fd = open("/dev/null", O_RDWR); dup2(fd, STDOUT_FILENO); close(fd); // add to library path: // - the project dir (../../projects/X) // - the slot dir (.) // - the Synecdoche dir (../..) // We use relative paths in case higher-level dirs // are not readable to the account under which app runs // std::string pdir = get_project_dir(wup->project); std::ostringstream libpath; const char* env_lib_path = getenv("LD_LIBRARY_PATH"); if (env_lib_path) { libpath << env_lib_path << ':'; } libpath << "../../" << pdir << ":.:../.."; setenv("LD_LIBRARY_PATH", libpath.str().c_str(), 1); retval = chdir(slot_dir.c_str()); if (retval) { perror("chdir"); fflush(NULL); _exit(errno); } #if 0 // set stack size limit to the max. // Some BOINC apps have reported problems with exceeding // small stack limits (e.g. 8 MB) // and it seems like the best thing to raise it as high as possible // struct rlimit rlim; #define MIN_STACK_LIMIT 64000000 getrlimit(RLIMIT_STACK, &rlim); if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur <= MIN_STACK_LIMIT) { if (rlim.rlim_max == RLIM_INFINITY || rlim.rlim_max > MIN_STACK_LIMIT) { rlim.rlim_cur = MIN_STACK_LIMIT; } else { rlim.rlim_cur = rlim.rlim_max; } setrlimit(RLIMIT_STACK, &rlim); } #endif // hook up stderr to a specially-named file // freopen(STDERR_FILE, "a", stderr); // set idle process priority #ifdef HAVE_SETPRIORITY if (setpriority(PRIO_PROCESS, 0, PROCESS_IDLE_PRIORITY)) { perror("setpriority"); } #endif std::string path = std::string("../../") + std::string(exec_path); if (g_use_sandbox) { std::ostringstream switcher_path; switcher_path << "../../" << SWITCHER_DIR << '/' << SWITCHER_FILE_NAME; argv.push_front(exec_name); argv.push_front(path); argv.push_front(SWITCHER_FILE_NAME); // Files written by projects have user boinc_project and group boinc_project, // so they must be world-readable so Synecdoche can read them. umask(2); retval = do_execv(switcher_path.str(), argv); } else { argv.push_front(exec_name); retval = do_execv(path, argv); } msg_printf(wup->project, MSG_INTERNAL_ERROR, "Process creation (%s) failed: %s, errno=%d\n", path.c_str(), boincerror(retval), errno ); perror("execv"); fflush(NULL); _exit(errno); } if (log_flags.task_debug) { msg_printf(wup->project, MSG_INFO, "[task_debug] ACTIVE_TASK::start(): forked process: pid %d\n", pid ); } #endif set_task_state(PROCESS_EXECUTING, "start"); return 0; // go here on error; "error_msg" contains error message, "retval" is nonzero // error: // if something failed, it's possible that the executable was munged. // Verify it to trigger another download. // gstate.input_files_available(result, true); gstate.report_result_error(*result, "%s", err_stream.str().c_str()); set_task_state(PROCESS_COULDNT_START, "start"); return retval; }
int main(int argc, char **argv) { struct image_header hdr; struct stat st; uint8_t *buf; FILE *fpout; FILE *fpin; int crc_field_off; int crc_start; int crc_len; int rc; if (argc < 4) { print_usage(stderr); return 1; } memset(&hdr, 0, sizeof hdr); fpin = fopen(argv[1], "rb"); if (fpin == NULL) { fprintf(stderr, "* error: could not open input file %s\n", argv[1]); print_usage(stderr); return 1; } if (is_image_file(argv[1])) { fprintf(stderr, "* error: source file is already an image (%s)\n", argv[1]); print_usage(stderr); return 1; } fpout = fopen(argv[2], "wb"); if (fpout == NULL) { fprintf(stderr, "* error: could not open output file %s\n", argv[1]); print_usage(stderr); return 1; } rc = imgr_ver_parse(argv[3], &hdr.ih_ver); if (rc != 0) { print_usage(stderr); return 1; } rc = stat(argv[1], &st); if (rc != 0) { perror("stat"); print_usage(stderr); return 1; } buf = malloc(sizeof hdr + st.st_size); assert(buf != NULL); rc = fread(buf + sizeof hdr, st.st_size, 1, fpin); if (rc != 1) { if (ferror(fpin)) { fprintf(stderr, "* error: file read error (%s) (file=%s)\n", strerror(errno), argv[1]); } else { fprintf(stderr, "* error: file read error (inconsistent length) " "(file=%s)\n", argv[1]); } print_usage(stderr); return 1; } hdr.ih_magic = IMAGE_MAGIC; hdr.ih_hdr_size = sizeof hdr; hdr.ih_img_size = st.st_size; memcpy(buf, &hdr, sizeof hdr); crc_field_off = offsetof(struct image_header, ih_crc32); crc_start = crc_field_off + sizeof hdr.ih_crc32; crc_len = sizeof hdr - crc_start + st.st_size; hdr.ih_crc32 = crc32(0, buf + crc_start, crc_len); memcpy(buf + crc_field_off, &hdr.ih_crc32, sizeof hdr.ih_crc32); rc = fwrite(buf, sizeof hdr + st.st_size, 1, fpout); if (rc != 1) { fprintf(stderr, "* error: file write error (file=%s)\n", argv[2]); print_usage(stderr); return 1; } return 0; }