static afc_error_t cmd_mv(int argc, const char *argv[]) { afc_error_t result; const char *filename; char *source_path, *target_path; char **infolist; bool target_is_dir; if (argc != 2) { warnx("usage: mv <source> <target>"); return -1; } source_path = build_absolute_path(argv[0]); target_path = build_absolute_path(argv[1]); if (source_path && target_path) { result = afc_get_file_info(afc, target_path, &infolist); if (result == AFC_E_SUCCESS) { for (int i = 0; infolist[i] != NULL; i++) { if (str_is_equal(infolist[i], "st_ifmt")) target_is_dir = str_is_equal(infolist[i+1], "S_IFDIR"); free(infolist[i]); } free(infolist); if (target_is_dir) { filename = basename((char *) argv[0]); target_path = realloc(target_path, strlen(filename) + 2); strcat(target_path, "/"); strcat(target_path, filename); result = AFC_E_SUCCESS; } } else if (result == AFC_E_OBJECT_NOT_FOUND) { result = AFC_E_SUCCESS; } if (result == AFC_E_SUCCESS) { result = afc_rename_path(afc, source_path, target_path); if (result != AFC_E_SUCCESS) afc_warn(result, "rename %s", argv[0]); } } else { result = AFC_E_INTERNAL_ERROR; } free(source_path); free(target_path); return result; }
static afc_error_t cmd_rm(int argc, const char *argv[]) { afc_error_t result; char *path = NULL; bool recurse = false; if (argc < 1) { warnx("usage: rm [-r] <file> ..."); return AFC_E_INVALID_ARG; } if (argc > 1 && (recurse = str_is_equal("-r", argv[0]))) { argc--; argv++; } for (int i = 0; i < argc; i++) { path = build_absolute_path(argv[i]); if (!path) return AFC_E_INTERNAL_ERROR; result = remove_path(path, recurse); free(path); if (result == AFC_E_OBJECT_NOT_FOUND) afc_warn(result, "%s", argv[i]); return result; } return AFC_E_SUCCESS; }
static afc_error_t cmd_ls(int argc, const char *argv[]) { afc_error_t result; char *path; char **list = NULL; if (argc == 0) { path = strdup(cwd); } else if (argc == 1) { path = build_absolute_path(argv[0]); if (!path) return AFC_E_INTERNAL_ERROR; } else { warnx("usage: ls [<dir>]"); return AFC_E_INVALID_ARG; } result = afc_read_directory(afc, path, &list); if (result != AFC_E_SUCCESS) { afc_warn(result, "%s", argc == 1 ? argv[0] : cwd); return result; } for (int i = 0; list[i]; i++) { printf("%s\n", list[i]); free(list[i]); } free(list); return AFC_E_SUCCESS; }
static int cmd_cd(const char *path) { afc_error_t result; char *fullpath, *cleanpath; char **infolist; if (!path) { free(cwd); cwd = strdup("/"); return 0; } if ((fullpath = build_absolute_path(path)) == NULL) return AFC_E_INTERNAL_ERROR; cleanpath = cleanse_path(fullpath); result = afc_get_file_info(afc, cleanpath, &infolist); if (result != AFC_E_SUCCESS) { afc_warn(result, "%s", path); free(cleanpath); return result; } for (int i = 0; infolist[i] != NULL; i++) free(infolist[i]); free(infolist); free(cwd); cwd = cleanpath; return 0; }
static afc_error_t cmd_ln(int argc, const char *argv[]) { afc_error_t result; int type = AFC_HARDLINK; char *source_path = NULL; char *target_path = NULL; if (argc == 3 && str_is_equal("-s", argv[0])) { type = AFC_SYMLINK; argc--; argv++; } if (argc != 2) { warnx("usage: ln [-s] <source> <target>"); return AFC_E_INVALID_ARG; } if (type == AFC_HARDLINK) source_path = build_absolute_path(argv[0]); else source_path = strdup(argv[0]); target_path = build_absolute_path(argv[1]); if (!source_path || !target_path) { free(source_path); free(target_path); return AFC_E_INTERNAL_ERROR; } // warnx("%s: %s -> %s\n", type == AFC_HARDLINK ? "hard link" : "sym link", source_path, target_path); result = afc_make_link(afc, type, source_path, target_path); if (result == AFC_E_OBJECT_NOT_FOUND) afc_warn(result, "%s", argv[0]); else if (result != AFC_E_SUCCESS) afc_warn(result, "afc_make_link"); free(source_path); free(target_path); return result; }
static afc_error_t cmd_stat(int argc, const char *argv[]) { afc_error_t result; struct afc_stat st_buf; char *path; char *ifmt; time_t atime; char *modtime, *createtime; if (argc < 1) { warnx("usage: stat <file> ..."); return -1; } for (int i = 0; i < argc; i++) { path = build_absolute_path(argv[0]); result = afc_stat(afc, path, &st_buf); free(path); if (result != AFC_E_SUCCESS) { afc_warn(result, "%s", argv[i]); return result; } if (S_ISREG(st_buf.st_ifmt)) ifmt = "S_IFREG"; else if (S_ISDIR(st_buf.st_ifmt)) ifmt = "S_IFDIR"; else if (S_ISLNK(st_buf.st_ifmt)) ifmt = "S_IFLNK"; else if (S_ISBLK(st_buf.st_ifmt)) ifmt = "S_IFBLK"; else if (S_ISCHR(st_buf.st_ifmt)) ifmt = "S_IFCHR"; else if (S_ISFIFO(st_buf.st_ifmt)) ifmt = "S_IFIFO"; else if (S_ISSOCK(st_buf.st_ifmt)) ifmt = "S_IFSOCK"; // time_t may be 32 or 64 bits atime = (time_t) st_buf.st_modtime; modtime = strdup(ctime(&atime)); atime = (time_t) st_buf.st_createtime; createtime = strdup(ctime(&atime)); printf("%s:\n", argv[i]); printf("%14s %lld\n", "st_size", st_buf.st_size); printf("%14s %lld\n", "st_blocks", st_buf.st_blocks); printf("%14s %hd\n", "st_nlink", st_buf.st_nlink); printf("%14s %s\n", "st_ifmt", ifmt); printf("%14s %u - %s", "st_mtime", st_buf.st_modtime, modtime); printf("%14s %u - %s", "st_birthtime", st_buf.st_createtime, createtime); putc('\n', stdout); free(modtime); free(createtime); } return AFC_E_SUCCESS; }
static afc_error_t cmd_cat(int argc, const char *argv[]) { afc_error_t result; char *path; uint64_t handle, size; char **infolist; uint32_t readsize; char buffer[0x1000]; if (argc != 1) { warnx("usage: cat <file>"); return -1; } if ((path = build_absolute_path(argv[0])) == NULL) return AFC_E_INTERNAL_ERROR; result = afc_get_file_info(afc, path, &infolist); if (result != AFC_E_SUCCESS) { afc_warn(result, "%s", argv[0]); free(path); return result; } size = atoll(infolist[1]); for (int i = 0; infolist[i] != NULL; i++) free(infolist[i]); free(infolist); result = afc_file_open(afc, path, AFC_FOPEN_RDONLY, &handle); if (result != AFC_E_SUCCESS) { afc_warn(result, "%s", argv[0]); free(path); return result; } for (int i = 0; i < size; ) { result = afc_file_read(afc, handle, buffer, sizeof(buffer), &readsize); if (result != AFC_E_SUCCESS) { afc_warn(result, "%s", argv[0]); afc_file_close(afc, handle); free(path); return result; } fwrite(buffer, 1, readsize, stdout); i += readsize; } afc_file_close(afc, handle); free(path); return AFC_E_SUCCESS; }
static afc_error_t cmd_mkdir(int argc, const char *argv[]) { afc_error_t result; char *path; if (argc != 1) { warnx("usage: mkdir <dir>"); return AFC_E_INVALID_ARG; } if ((path = build_absolute_path(argv[0])) == NULL) return AFC_E_INTERNAL_ERROR; result = afc_make_directory(afc, path); if (result != AFC_E_SUCCESS) afc_warn(result, "%s", path); free(path); return result; }
void close_orphaned_processes( const std::list<std::string> &filenames ) { /*build absolute paths and convert to wide-char*/ std::list<std::string> absolute_paths; for( std::list<std::string>::const_iterator i = filenames.begin(); i != filenames.end(); i++ ) { char *absolute_path = build_absolute_path( i->c_str() ); if( absolute_path ) { absolute_paths.push_back( absolute_path ); delete absolute_path; } } /*look for processes matching these absolute paths, and kill them*/ PROCESSENTRY32 process_entry; process_entry.dwSize = sizeof( PROCESSENTRY32 ); HANDLE process_snapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, NULL ); if( Process32First( process_snapshot, &process_entry ) == TRUE ) { do{ bool terminated = false; if( process_entry.th32ProcessID > 0 ) { HANDLE module_snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, process_entry.th32ProcessID); MODULEENTRY32 module_entry; module_entry.dwSize = sizeof( MODULEENTRY32 ); if( Module32First( module_snapshot, &module_entry ) == TRUE ) { for( std::list<std::string>::const_iterator i = absolute_paths.begin(); i != absolute_paths.end(); i++ ) { std::wstring absolute_path_wide; absolute_path_wide.assign( i->begin(), i->end() ); if( absolute_path_wide == module_entry.szExePath ) { if( process_entry.th32ProcessID != GetCurrentProcessId() ) { HANDLE process_handle = OpenProcess( PROCESS_TERMINATE, FALSE, process_entry.th32ProcessID ); if( process_handle ) { if( TerminateProcess( process_handle, 0 ) != 0 ) { INTEGRA_TRACE_PROGRESS << "Killed orphaned process " << *i; terminated = true; } else { INTEGRA_TRACE_ERROR << "failed to terminate orphaned process. Error:" << GetLastError(); } CloseHandle( process_handle ); } else { INTEGRA_TRACE_ERROR << "failed to kill orphaned process - couldn't open process handle. Error: " << GetLastError(); } } } if( terminated ) { break; } } while( !terminated && Module32Next( module_snapshot, &module_entry ) == TRUE ); } CloseHandle( module_snapshot ); } } while( Process32Next( process_snapshot, &process_entry ) == TRUE ); } CloseHandle( process_snapshot ); }