⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fatfs.c

📁 linux下开发的针对所有磁盘的数据恢复的源码
💻 C
📖 第 1 页 / 共 4 页
字号:
                free(fat_fsinfo_buf);                return 1;            }            fat_info = (fatfs_fsinfo *) fat_fsinfo_buf;            tsk_fprintf(hFile,                "Next Free Sector (FS Info): %" PRIuDADDR "\n",                FATFS_CLUST_2_SECT(fatfs, tsk_getu32(fs->endian,                        fat_info->nextfree)));            tsk_fprintf(hFile,                "Free Sector Count (FS Info): %" PRIu32 "\n",                (tsk_getu32(fs->endian,                        fat_info->freecnt) * fatfs->csize));            free(fat_fsinfo_buf);        }    }    free(data_buf);    tsk_fprintf(hFile, "\nSectors before file system: %" PRIu32 "\n",        tsk_getu32(fs->endian, sb->prevsect));    tsk_fprintf(hFile, "\nFile System Layout (in sectors)\n");    tsk_fprintf(hFile, "Total Range: %" PRIuDADDR " - %" PRIuDADDR "\n",        fs->first_block, fs->last_block);    if (fs->last_block != fs->last_block_act)        tsk_fprintf(hFile,            "Total Range in Image: %" PRIuDADDR " - %" PRIuDADDR "\n",            fs->first_block, fs->last_block_act);    tsk_fprintf(hFile, "* Reserved: 0 - %" PRIuDADDR "\n",        fatfs->firstfatsect - 1);    tsk_fprintf(hFile, "** Boot Sector: 0\n");    if (fatfs->fs_info.ftype == TSK_FS_TYPE_FAT32) {        tsk_fprintf(hFile, "** FS Info Sector: %" PRIu16 "\n",            tsk_getu16(fs->endian, sb->a.f32.fsinfo));        tsk_fprintf(hFile, "** Backup Boot Sector: %" PRIu32 "\n",            tsk_getu32(fs->endian, sb->a.f32.bs_backup));    }    for (i = 0; i < fatfs->numfat; i++) {        TSK_DADDR_T base = fatfs->firstfatsect + i * (fatfs->sectperfat);        tsk_fprintf(hFile, "* FAT %d: %" PRIuDADDR " - %" PRIuDADDR "\n",            i, base, (base + fatfs->sectperfat - 1));    }    tsk_fprintf(hFile, "* Data Area: %" PRIuDADDR " - %" PRIuDADDR "\n",        fatfs->firstdatasect, fs->last_block);    if (fatfs->fs_info.ftype != TSK_FS_TYPE_FAT32) {        TSK_DADDR_T x = fatfs->csize * fatfs->clustcnt;        tsk_fprintf(hFile,            "** Root Directory: %" PRIuDADDR " - %" PRIuDADDR "\n",            fatfs->firstdatasect, fatfs->firstclustsect - 1);        tsk_fprintf(hFile,            "** Cluster Area: %" PRIuDADDR " - %" PRIuDADDR "\n",            fatfs->firstclustsect, (fatfs->firstclustsect + x - 1));        if ((fatfs->firstclustsect + x - 1) != fs->last_block) {            tsk_fprintf(hFile,                "** Non-clustered: %" PRIuDADDR " - %" PRIuDADDR "\n",                (fatfs->firstclustsect + x), fs->last_block);        }    }    else {        TSK_LIST *list_seen = NULL;        TSK_DADDR_T x = fatfs->csize * (fatfs->lastclust - 1);        TSK_DADDR_T clust, clust_p;        tsk_fprintf(hFile,            "** Cluster Area: %" PRIuDADDR " - %" PRIuDADDR "\n",            fatfs->firstclustsect, (fatfs->firstclustsect + x - 1));        clust_p = fatfs->rootsect;        clust = FATFS_SECT_2_CLUST(fatfs, fatfs->rootsect);        while ((clust) && (0 == FATFS_ISEOF(clust, FATFS_32_MASK))) {            TSK_DADDR_T nxt;            clust_p = clust;            /* Make sure we do not get into an infinite loop */            if (tsk_list_find(list_seen, clust)) {                if (tsk_verbose)                    tsk_fprintf(stderr,                        "Loop found while determining root directory size\n");                break;            }            if (tsk_list_add(&list_seen, clust)) {                tsk_list_free(list_seen);                list_seen = NULL;                return 1;            }            if (fatfs_getFAT(fatfs, clust, &nxt))                break;            clust = nxt;        }        tsk_list_free(list_seen);        list_seen = NULL;        tsk_fprintf(hFile,            "*** Root Directory: %" PRIuDADDR " - %" PRIuDADDR "\n",            fatfs->rootsect, (FATFS_CLUST_2_SECT(fatfs, clust_p + 1) - 1));        if ((fatfs->firstclustsect + x - 1) != fs->last_block) {            tsk_fprintf(hFile,                "** Non-clustered: %" PRIuDADDR " - %" PRIuDADDR "\n",                (fatfs->firstclustsect + x), fs->last_block);        }    }    tsk_fprintf(hFile, "\nMETADATA INFORMATION\n");    tsk_fprintf(hFile, "--------------------------------------------\n");    tsk_fprintf(hFile, "Range: %" PRIuINUM " - %" PRIuINUM "\n",        fs->first_inum, fs->last_inum);    tsk_fprintf(hFile, "Root Directory: %" PRIuINUM "\n", fs->root_inum);    tsk_fprintf(hFile, "\nCONTENT INFORMATION\n");    tsk_fprintf(hFile, "--------------------------------------------\n");    tsk_fprintf(hFile, "Sector Size: %" PRIu16 "\n", fatfs->ssize);    tsk_fprintf(hFile, "Cluster Size: %" PRIu32 "\n",        (uint32_t) fatfs->csize << fatfs->ssize_sh);    tsk_fprintf(hFile, "Total Cluster Range: 2 - %" PRIuDADDR "\n",        fatfs->lastclust);    /* cycle via cluster and look at each cluster in the FAT      * for clusters marked as bad */    cnt = 0;    for (i = 2; i <= fatfs->lastclust; i++) {        TSK_DADDR_T entry;        TSK_DADDR_T sect;        /* Get the FAT table entry */        if (fatfs_getFAT(fatfs, i, &entry))            break;        if (FATFS_ISBAD(entry, fatfs->mask) == 0) {            continue;        }        if (cnt == 0)            tsk_fprintf(hFile, "Bad Sectors: ");        sect = FATFS_CLUST_2_SECT(fatfs, i);        for (a = 0; a < fatfs->csize; a++) {            tsk_fprintf(hFile, "%" PRIuDADDR " ", sect + a);            if ((++cnt % 8) == 0)                tsk_fprintf(hFile, "\n");        }    }    if ((cnt > 0) && ((cnt % 8) != 0))        tsk_fprintf(hFile, "\n");    /* Display the FAT Table */    tsk_fprintf(hFile, "\nFAT CONTENTS (in sectors)\n");    tsk_fprintf(hFile, "--------------------------------------------\n");    /* 'sstart' marks the first sector of the current run to print */    sstart = fatfs->firstclustsect;    /* cycle via cluster and look at each cluster in the FAT  to make runs */    for (i = 2; i <= fatfs->lastclust; i++) {        /* 'send' marks the end sector of the current run, which will extend         * when the current cluster continues to the next          */        send = FATFS_CLUST_2_SECT(fatfs, i + 1) - 1;        /* get the next cluster */        if (fatfs_getFAT(fatfs, i, &next))            break;        snext = FATFS_CLUST_2_SECT(fatfs, next);        /* we are also using the next sector (clust) */        if ((next & fatfs->mask) == (i + 1)) {            continue;        }        /* The next clust is either further away or the clust is available,         * print it if is further away          */        else if ((next & fatfs->mask)) {            if (FATFS_ISEOF(next, fatfs->mask))                tsk_fprintf(hFile,                    "%" PRIuDADDR "-%" PRIuDADDR " (%" PRIuDADDR                    ") -> EOF\n", sstart, send, send - sstart + 1);            else if (FATFS_ISBAD(next, fatfs->mask))                tsk_fprintf(hFile,                    "%" PRIuDADDR "-%" PRIuDADDR " (%" PRIuDADDR                    ") -> BAD\n", sstart, send, send - sstart + 1);            else                tsk_fprintf(hFile,                    "%" PRIuDADDR "-%" PRIuDADDR " (%" PRIuDADDR                    ") -> %" PRIuDADDR "\n", sstart, send,                    send - sstart + 1, snext);        }        /* reset the starting counter */        sstart = send + 1;    }    return 0;}/************************* istat *******************************//* Callback a_action for file_walk to print the sector addresses * of a file */typedef struct {    FILE *hFile;    int idx;    int istat_seen;} FATFS_PRINT_ADDR;static TSK_WALK_RET_ENUMprint_addr_act(TSK_FS_FILE * fs_file, TSK_OFF_T a_off, TSK_DADDR_T addr,    char *buf, size_t size, TSK_FS_BLOCK_FLAG_ENUM a_flags, void *a_ptr){    FATFS_PRINT_ADDR *print = (FATFS_PRINT_ADDR *) a_ptr;    tsk_fprintf(print->hFile, "%" PRIuDADDR " ", addr);    if (++(print->idx) == 8) {        tsk_fprintf(print->hFile, "\n");        print->idx = 0;    }    print->istat_seen = 1;    return TSK_WALK_CONT;}/** * Print details on a specific file to a file handle.  * * @param fs File system file is located in * @param hFile File handle to print text to * @param inum Address of file in file system * @param numblock The number of blocks in file to force print (can go beyond file size) * @param sec_skew Clock skew in seconds to also print times in *  * @returns 1 on error and 0 on success */static uint8_tfatfs_istat(TSK_FS_INFO * fs, FILE * hFile, TSK_INUM_T inum,    TSK_DADDR_T numblock, int32_t sec_skew){    TSK_FS_META *fs_meta;    TSK_FS_FILE *fs_file;    TSK_FS_META_NAME_LIST *fs_name_list;    FATFS_INFO *fatfs = (FATFS_INFO *) fs;    FATFS_PRINT_ADDR print;    // clean up any error messages that are lying around    tsk_error_reset();    if ((fs_file = tsk_fs_file_open_meta(fs, NULL, inum)) == NULL) {        return 1;    }    fs_meta = fs_file->meta;    tsk_fprintf(hFile, "Directory Entry: %" PRIuINUM "\n", inum);    tsk_fprintf(hFile, "%sAllocated\n",        (fs_meta->flags & TSK_FS_META_FLAG_UNALLOC) ? "Not " : "");    tsk_fprintf(hFile, "File Attributes: ");    /* This should only be null if we have the root directory or special file */    if (fatfs->dep == NULL) {        if (inum == FATFS_ROOTINO)            tsk_fprintf(hFile, "Directory\n");        else if (fs_file->meta->type == TSK_FS_META_TYPE_VIRT)            tsk_fprintf(hFile, "Virtual\n");        else            tsk_fprintf(hFile, "File\n");    }    else if ((fatfs->dep->attrib & FATFS_ATTR_LFN) == FATFS_ATTR_LFN) {        tsk_fprintf(hFile, "Long File Name\n");    }    else {        if (fatfs->dep->attrib & FATFS_ATTR_DIRECTORY)            tsk_fprintf(hFile, "Directory");        else if (fatfs->dep->attrib & FATFS_ATTR_VOLUME)            tsk_fprintf(hFile, "Volume Label");        else            tsk_fprintf(hFile, "File");        if (fatfs->dep->attrib & FATFS_ATTR_READONLY)            tsk_fprintf(hFile, ", Read Only");        if (fatfs->dep->attrib & FATFS_ATTR_HIDDEN)            tsk_fprintf(hFile, ", Hidden");        if (fatfs->dep->attrib & FATFS_ATTR_SYSTEM)            tsk_fprintf(hFile, ", System");        if (fatfs->dep->attrib & FATFS_ATTR_ARCHIVE)            tsk_fprintf(hFile, ", Archive");        tsk_fprintf(hFile, "\n");    }    tsk_fprintf(hFile, "Size: %" PRIuOFF "\n", fs_meta->size);    if (fs_meta->name2) {        fs_name_list = fs_meta->name2;        tsk_fprintf(hFile, "Name: %s\n", fs_name_list->name);    }    if (sec_skew != 0) {        tsk_fprintf(hFile, "\nAdjusted Directory Entry Times:\n");        fs_meta->mtime -= sec_skew;        fs_meta->atime -= sec_skew;        fs_meta->crtime -= sec_skew;        tsk_fprintf(hFile, "Written:\t%s", ctime(&fs_meta->mtime));        tsk_fprintf(hFile, "Accessed:\t%s", ctime(&fs_meta->atime));        tsk_fprintf(hFile, "Created:\t%s", ctime(&fs_meta->crtime));        fs_meta->mtime += sec_skew;        fs_meta->atime += sec_skew;        fs_meta->crtime += sec_skew;        tsk_fprintf(hFile, "\nOriginal Directory Entry Times:\n");    }    else        tsk_fprintf(hFile, "\nDirectory Entry Times:\n");    tsk_fprintf(hFile, "Written:\t%s", ctime(&fs_meta->mtime));    tsk_fprintf(hFile, "Accessed:\t%s", ctime(&fs_meta->atime));    tsk_fprintf(hFile, "Created:\t%s", ctime(&fs_meta->crtime));    tsk_fprintf(hFile, "\nSectors:\n");    /* A bad hack to force a specified number of blocks */    if (numblock > 0)        fs_meta->size = numblock * fs->block_size;    print.istat_seen = 0;    print.idx = 0;    print.hFile = hFile;    if (tsk_fs_file_walk(fs_file,            (TSK_FS_FILE_WALK_FLAG_AONLY | TSK_FS_FILE_WALK_FLAG_SLACK),            print_addr_act, (void *) &print)) {        tsk_fprintf(hFile, "\nError reading file\n");        tsk_error_print(hFile);        tsk_error_reset();    }    else if (print.idx != 0) {        tsk_fprintf(hFile, "\n");    }    tsk_fs_file_close(fs_file);    return 0;}/* return 1 on error and 0 on success */uint8_tfatfs_jopen(TSK_FS_INFO * fs, TSK_INUM_T inum){    tsk_error_reset();    tsk_errno = TSK_ERR_FS_UNSUPFUNC;    snprintf(tsk_errstr, TSK_ERRSTR_L, "FAT does not have a journal\n");    return 1;}/* return 1 on error and 0 on success */uint8_tfatfs_jentry_walk(TSK_FS_INFO * fs, int a_flags,    TSK_FS_JENTRY_WALK_CB a_action, void *a_ptr){    tsk_error_reset();    tsk_errno = TSK_ERR_FS_UNSUPFUNC;    snprintf(tsk_errstr, TSK_ERRSTR_L, "FAT does not have a journal\n");    return 1;}/* return 1 on error and 0 on success */uint8_tfatfs_jblk_walk(TSK_FS_INFO * fs, TSK_DADDR_T start, TSK_DADDR_T end,    int a_flags, TSK_FS_JBLK_WALK_CB a_action, void *a_ptr){    tsk_error_reset();    tsk_errno = TSK_ERR_FS_UNSUPFUNC;    snprintf(tsk_errstr, TSK_ERRSTR_L, "FAT does not have a journal\n");    return 1;}static TSK_FS_ATTR_TYPE_ENUMfatfs_get_default_attr_type(const TSK_FS_FILE * a_file){    return TSK_FS_ATTR_TYPE_DEFAULT;}/* fatfs_close - close an fatfs file system */static voidfatfs_close(TSK_FS_INFO * fs){    FATFS_INFO *fatfs = (FATFS_INFO *) fs;    fs->tag = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -