int filesearch(const char *path, my_dirent *root, int recursion) { struct dirent* ent = NULL; DIR* pDir; my_dirent *cusor, *p; pDir = opendir(path); if (pDir == NULL) return -1; cusor = root; while((ent = readdir(pDir)) != NULL) { if (ent->d_type == 4) { if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) continue; p = create_dirent(path, ent->d_name, 1, 0); if (!ent) return -1; cusor->next = p; cusor = p; if (recursion) { if (filesearch(p->path, cusor, recursion)) return -1; } } else if (ent->d_type == 8){ p = create_dirent(path, ent->d_name, 0, 0); if (!p) return -1; cusor->next = p; cusor = p; } } closedir(pDir); return 0; }
sc_retval create_segment(const sc_string &s, sc_segment_base *custom_seg) { sc_segment *seg = map_segment(s); if (seg) return RV_OK; return create_dirent(s, false, custom_seg) ? RV_OK : RV_ERR_GEN; }
sc_retval create_segment(const sc_string &s) { sc_segment *seg = map_segment(s); if (seg) return RV_OK; return create_dirent(s,false)?RV_OK:RV_ERR_GEN; }
int filesearch(const char *path, my_dirent *root, int recursion) { struct _finddata_t filefind; char *curr; int done = 0, handle; my_dirent *cusor, *ent; curr = (char *)alloca(strlen(path) + 5); if (!curr) return -1; strcpy(curr, path); strcat(curr, "\\*.*"); if((handle = _findfirst(curr, &filefind)) == -1) return -1; cusor = root; while(!(done = _findnext(handle, &filefind))) { if(!strcmp(filefind.name, "..")) continue; if ((_A_SUBDIR == filefind.attrib)) { ent = create_dirent(path, filefind.name, 1, filefind.time_write); if (!ent) return -1; cusor->next = ent; cusor = ent; if (recursion) { if (filesearch(ent->path, cusor, recursion)) return -1; } } else { ent = create_dirent(path, filefind.name, 0, filefind.time_write); if (!ent) return -1; cusor->next = ent; cusor = ent; } } _findclose(handle); return 0; }
/***********SAVE_ORPHANS***************/ void orphan_search(uint8_t *image_buf, struct bpb33* bpb, int * refcount, int FATsz) { int count = 0; //count orphans int i = 2; for(; i < FATsz; i++) { uint16_t cluster = get_fat_entry(i, image_buf, bpb); if(cluster != (FAT12_MASK & CLUST_FREE) && cluster != (FAT12_MASK & CLUST_BAD) && refcount[i] == 0) { printf("FOUND AN ORPHAN, CLUSTER # = %d\n", i); count++; int orphan_size = 1; refcount[i] = 1; uint16_t orphan_cluster = cluster; while(is_valid_cluster(orphan_cluster, bpb)) { if (refcount[orphan_cluster] == 1) { set_fat_entry(orphan_cluster, (FAT12_MASK & CLUST_EOFS), image_buf, bpb); } else if(refcount[orphan_cluster] > 1) { struct direntry *dirent = (struct direntry*)cluster_to_addr(cluster, image_buf, bpb); dirent->deName[0] = SLOT_DELETED; refcount[orphan_cluster]--; printf("DELETED\n"); } else if(refcount[orphan_cluster] < 1) { refcount[orphan_cluster]++; } orphan_cluster = get_fat_entry(orphan_cluster, image_buf, bpb); orphan_size++; } //got to concatanate stuff struct direntry *dirent = (struct direntry*)root_dir_addr(image_buf, bpb); char name[5]; sprintf(name, "%d", count); char str[1024] = ""; strcat(str, "located"); strcat(str, name); strcat(str, ".dat\0"); char * file_name = str; create_dirent(dirent, file_name, i, orphan_size * 512, image_buf, bpb); printf("added %s to directory \n", str); printf("orphan cluster chain size = %d \n", orphan_size); } } printf("orphans saved: %d\n", count); }
/* * helper function that fixes a single orphan and makes * a DAT file associated with the cluster */ int foster_single_orphan(int orphan_count, uint16_t curr_cluster, uint8_t *image_buf, struct bpb33* bpb) { orphan_count++; int cluster = 0; struct direntry* dirent = (struct direntry*) cluster_to_addr(cluster, image_buf, bpb); char filename[13]; char str[3]; // make file name memset(filename, '\0', 13); strcat(filename, "found"); memset(str, '\0', 3); sprintf(str, "%d", orphan_count); strcat(filename, str); strcat(filename, ".dat"); int clusters_size = size_of_cluster(curr_cluster, image_buf, bpb); append_clusters(cluster); create_dirent(dirent, filename, curr_cluster, clusters_size, image_buf, bpb); return orphan_count; }
sc_retval mkdir(const sc_string &s) { return create_dirent(s,true)?RV_OK:RV_ERR_GEN; }
int mk_new_directory(jfs_t *jfs, char *pathname, int parent_inodenum, int grandparent_inodenum) { /* we need to create this directory */ /* round size up to nearest 4 byte boundary */ int size, prev_size, bytes_done=0; int new_inodenum, new_blocknum; char block[BLOCKSIZE]; struct inode* new_inode, *parent_inode; /* calculate the size of the new parent dirent */ size = (((strlen(pathname)/4) + 1) * 4) + 16; /* does it fit?*/ if (bytes_done + size > BLOCKSIZE) { fprintf(stderr, "No more space in the directory to add another entry\n"); exit(1); } /* allocate an inode for this directory */ if (strcmp(pathname, ".")==0) { new_inodenum = parent_inodenum; } else if (strcmp(pathname, "..")==0) { new_inodenum = grandparent_inodenum; } else { new_inodenum = get_free_inode(jfs); } /* update the parent directories inode size field */ jfs_read_block(jfs, block, inode_to_block(parent_inodenum)); parent_inode = (struct inode*)(block + (parent_inodenum % INODES_PER_BLOCK) * INODE_SIZE); prev_size = parent_inode->size; parent_inode->size += size; jfs_write_block(jfs, block, inode_to_block(parent_inodenum)); /* create an entry in the parent directory */ create_dirent(jfs, pathname, DT_DIRECTORY, parent_inode->blockptrs[0], prev_size, size, new_inodenum); if (strcmp(pathname, ".")!=0 && strcmp(pathname, "..")!=0) { /* it's a real directory */ /* get a block to hold the directory */ new_blocknum = get_free_block(jfs); /* read in the block that contains our new inode */ jfs_read_block(jfs, block, inode_to_block(new_inodenum)); new_inode = (struct inode*)(block + (new_inodenum % INODES_PER_BLOCK) * INODE_SIZE); new_inode->flags = FLAG_DIR; new_inode->blockptrs[0] = new_blocknum; new_inode->size = 0; /* write back the inode block */ jfs_write_block(jfs, block, inode_to_block(new_inodenum)); return new_inodenum; } return parent_inodenum; }
struct handlelist *fill_lost_and_found(PVFS_fs_id cur_fs, struct handlelist *hl_all, PVFS_BMI_addr_t *addr_array, PVFS_credentials *creds) { int ret; int server_idx; PVFS_handle handle; struct handlelist *alt_hl; static char filename[64] = "lostfile."; static char dirname[64] = "lostdir."; /* TODO: DON'T DIRECTLY USE THESE MEMBERS... */ alt_hl = handlelist_initialize(hl_all->size_array, hl_all->server_ct); /* recall that return_handle removes from list */ while (handlelist_return_handle(hl_all, &handle, &server_idx) == 0) { PVFS_object_ref handle_ref; PVFS_sysresp_getattr getattr_resp; handle_ref.handle = handle; handle_ref.fs_id = cur_fs; ret = PVFS_sys_getattr(handle_ref, PVFS_ATTR_SYS_ALL_NOHINT, creds, &getattr_resp, NULL); if (ret) { printf("warning: problem calling getattr on %llu; assuming datafile for now.\n", llu(handle)); getattr_resp.attr.objtype = PVFS_TYPE_DATAFILE; } switch (getattr_resp.attr.objtype) { case PVFS_TYPE_METAFILE: printf("# trying to salvage %s %lld.\n", get_type_str(getattr_resp.attr.objtype), llu(handle)); if (verify_datafiles(cur_fs, hl_all, alt_hl, handle_ref, getattr_resp.attr.dfile_count, creds) != 0) { ret = remove_object(handle_ref, getattr_resp.attr.objtype, creds); assert(ret == 0); } else { sprintf(filename + 9, "%llu", llu(handle)); ret = create_dirent(laf_ref, filename, handle, creds); assert(ret == 0); } break; case PVFS_TYPE_DIRECTORY: /* assumption: we will often suceed in creating a new entry, * but if the file system is messed up we may not be able to * find dirdata, so match_dirdata before create_dirent */ if (match_dirdata(hl_all, alt_hl, handle_ref, creds) != 0) { ret = remove_object(handle_ref, getattr_resp.attr.objtype, creds); assert(ret == 0); } sprintf(dirname + 8, "%llu", llu(handle)); ret = create_dirent(laf_ref, dirname, handle, creds); if (ret != 0) { ret = remove_object(handle_ref, getattr_resp.attr.objtype, creds); } break; case PVFS_TYPE_DATAFILE: #if 0 printf("# saving %llu (datafile) for later.\n", llu(handle)); #endif handlelist_add_handle(alt_hl, handle, server_idx); break; case PVFS_TYPE_DIRDATA: #if 0 printf("# saving %llu (dirdata) for later.\n", llu(handle)); #endif handlelist_add_handle(alt_hl, handle, server_idx); break; case PVFS_TYPE_SYMLINK: default: /* delete on handle -- unknown type */ printf("* delete handle %llu (unknown type).\n", llu(handle)); break; } } return alt_hl; }
int main(int argc, char** argv) { uint8_t *image_buf; int fd; struct bpb33* bpb; if (argc != 2) { usage(); } image_buf = mmap_file(argv[1], &fd); bpb = check_bootsector(image_buf); int total_clusters = bpb->bpbSectors / bpb->bpbSecPerClust; int clust_size = bpb->bpbSecPerClust * bpb->bpbBytesPerSec; int used_clusters[total_clusters]; check_lost_files(used_clusters, 0, image_buf, bpb); int i; int shownPrefix = 0; for (i = 2; i < total_clusters; i++) { if (used_clusters[i] == 0 && get_fat_entry(i, image_buf, bpb) != CLUST_FREE) { if (!shownPrefix) { printf("Unreferenced:"); shownPrefix = 1; } printf(" %i", i); } if (i == total_clusters - 1 && shownPrefix) { printf("\n"); } } int foundCount = 1; shownPrefix = 0; for (i = 2; i < total_clusters; i++) { if (used_clusters[i] == 0 && get_fat_entry(i, image_buf, bpb) != CLUST_FREE) { if (!shownPrefix) { printf("Lost File: "); } uint16_t size = get_file_length(i, image_buf, bpb); printf("%i %i\n", i, size); struct direntry *dirent = (struct direntry*) cluster_to_addr(0, image_buf, bpb); uint32_t size_bytes = size * clust_size; const char base[] = "found"; const char extension[] = ".dat"; char filename [13]; sprintf(filename, "%s%i%s", base, foundCount++, extension); create_dirent(dirent, filename, i, size_bytes, image_buf, bpb); check_lost_files(used_clusters, 0, image_buf, bpb); } if (i == total_clusters - 1 && shownPrefix) { printf("\n"); } } check_file_length(0, image_buf, bpb); close(fd); exit(0); }