int main(int argc, char *argv[]) { xfs_mount_t *mp; xfs_inode_t *inode = NULL; xfs_off_t ofs; struct filldir_data filldata; char *progname; char *source_name; int r, fd; char *line; char path[FILENAME_MAX] = "/"; char newpath[FILENAME_MAX] = "/"; char *buffer[BUFSIZE]; off_t offset; if (argc != 2) { printf("Usage: xfs-cli raw_device\n"); return 1; } progname = argv[0]; source_name = argv[1]; mp = mount_xfs(progname, source_name); if (mp == NULL) return 1; while (line == fetchline(mp, path)) { inode = NULL; strip(line, ' '); if (strncmp(line, "cd ", 3) == 0) { if (strcmp(line+3, "..") == 0) { goto_parent(path); } else { strcpy(newpath, path); strcat(newpath, line+3); r = find_path(mp, newpath, &inode); if ((!r) && (xfs_is_dir(inode))) { //TODO: check if it is a directory strcpy(path, newpath); strcat(path, "/"); printf("%s\n", path); } else { if (r) { printf("No such directory\n"); } else { printf("Not a directory\n"); } } } } else if (strcmp(line, "ls") == 0) { r = find_path(mp, path, &inode); if (!r) { //TODO: check if it is a directory ofs=0; filldata.mp = mp; filldata.base = inode; r = xfs_readdir(inode, (void *)&filldata, 102400, &ofs, cli_ls_xfs_filldir); if (r != 0) { printf("Not a directory\n"); } } else { printf("No such directory\n"); } } else if (strncmp(line, "cat ", 4) == 0) { strcpy(newpath, path); strcat(newpath, line+4); r = find_path(mp, newpath, &inode); if (r) printf("File not found\n"); else if (xfs_is_regular(inode)) { //TODO: check if it is a file r = 10; offset = 0; while (r) { r = xfs_readfile(inode, buffer, offset, BUFSIZE, NULL); if (r) { write(1, buffer, r); offset += r; } } } else if (xfs_is_link(inode)) { r = 10; offset = 0; while (r) { r = xfs_readlink(inode, buffer, offset, BUFSIZE, NULL); if (r) { write(1, buffer, r); offset += r; } } } else { printf("Not a regular file\n"); } } else if (strncmp(line, "get ", 4) == 0) { strcpy(newpath, path); strcat(newpath, line+4); r = find_path(mp, newpath, &inode); if ((!r) && (xfs_is_regular(inode))) { //TODO: check if it is a file r = 10; offset = 0; fd = open(line+4, O_WRONLY|O_CREAT, 0660); if (fd < 0) { printf("Failed to open local file\n"); } else { while (r) { r = xfs_readfile(inode, buffer, offset, BUFSIZE, NULL); if (r) { write(fd, buffer, r); offset += r; } } close(fd); printf("Retrieved %ld bytes\n", offset); } } else { if (r) printf("File not found\n"); else printf("Not a regular file\n"); } } else if (strcmp(line, "exit") == 0) { libxfs_umount(mp); return 0; } else if (strcmp(line, "pwd") == 0) { printf("%s\n", path); } else { printf("Unknown command\n"); } free(line); if (inode) { libxfs_iput(inode, 0); } } libxfs_umount(mp); return 0; }
void HelpGotoParent() { goto_parent(node); start_loop(); }