📄 ntfstools.c
字号:
/* * ntfstools.c * Helper functions for the tools * Copyright (C) 1995 Martin von L鰓is */#include <stdio.h>#include <fcntl.h>#include <stdlib.h>#include <errno.h>#include <unistd.h>#include <stdarg.h>#include <string.h>#include <time.h>#include <ctype.h>#include "config.h"#include "ntfs.h"#include "version.h"/* glibc string.h does not define a memcpy prototype */void* memcpy(void*,const void*,size_t);ntfs_volume *the_vol;ntfs_volume myvol;/* pmemcpy is ignored here */int ntfs_getput_clusters(ntfs_volume *pvol,int cluster,ntfs_size_t offs, ntfs_size_t count,ntfs_io *buf){ int result; if(lseek(NTFS_FD(pvol), pvol->partition_bias+cluster*pvol->clustersize+offs,SEEK_SET)==-1) return 0; /* MAGIC: We *know* at this place that we were originally passed a plain pointer */ if(buf->do_read) result=read(NTFS_FD(pvol),buf->param,count)!=-1; else result=write(NTFS_FD(pvol),buf->param,count)!=-1; buf->param+=count; return result;}void ntfs_release_cluster(void *data){ free(data);}int open_volume(char *name){ int fd=open(name,O_RDWR); if(fd==-1 && errno==EACCES){ fprintf(stderr,"RW denied, trying RO\n"); fd=open(name,O_RDONLY); } if(fd==-1) { perror("open"); exit(0); } return fd;}void ntfs_error(const char*s,...){ va_list ap; va_start(ap,s); vfprintf(stderr,s,ap); va_end(ap); fputs("",stderr);}void ntfs_debug(const char*s,...){}void *ntfs_malloc(int size){ return malloc(size);}void ntfs_free(void *block){ free(block);}int ntfs_open_volume(char *file,int bias,int silent,int no_inodes){ int fd; char cluster0[512]; ntfs_io io; io.fn_put=0; io.fn_get=0; /* we don't need copy functions here */ the_vol=&myvol; myvol.partition_bias=bias; NTFS_FD(the_vol)=fd=open_volume(file=file?file:NTFS_VOLUME); if(fd<0) { return 0; } /* read the boot sector */ io.do_read=1; io.param=cluster0; ntfs_getput_clusters(the_vol,0,0,512,&io); if(!IS_NTFS_VOLUME(cluster0)){ fprintf(stderr,"Not a NTFS volume:%s\n",file); return 0; } ntfs_init_volume(the_vol,cluster0); /* read the first mft record */ the_vol->mft=malloc(the_vol->mft_recordsize); io.do_read=1; io.param=the_vol->mft; ntfs_getput_clusters(the_vol,the_vol->mft_cluster,0, the_vol->mft_recordsize,&io); /* fix it */ if(!ntfs_check_mft_record(the_vol,the_vol->mft)){ fprintf(stderr,"MFT record not at cluster 0x%X\n",the_vol->mft_cluster); return 0; } if(!silent) fprintf(stderr,"MFT record at block 0x%X, offset 0x%X\n",the_vol->mft_cluster, the_vol->mft_cluster*the_vol->clustersize); if(!no_inodes) ntfs_load_special_files(the_vol); return 1;}int ntfs_read_mft_record(ntfs_volume *vol,int mftno,char *buf){ ntfs_io io; io.fn_put=0; io.fn_get=0; /* record 0 of file 0 is always in memory */ if(mftno==0) { memcpy(buf,vol->mft,vol->mft_recordsize); return 0; } if(!vol->mft_ino) { fprintf(stderr,"Cannot load mft %x without mft 0\n",mftno); return -1; } io.param=buf; if(!ntfs_read_attr(vol->mft_ino,AT_DATA,NULL, mftno*vol->mft_recordsize,&io,vol->mft_recordsize)) return -1; if(!ntfs_check_mft_record(vol,buf)) { fprintf(stderr,"Inode not found\n"); return -1; } return 0;}void *ntfs_memcpy(void*a,const void*b,ntfs_size_t n){ return memcpy(a,b,n);}void ntfs_bzero(void*a,ntfs_size_t len){ bzero(a,len);}int ntfs_strlen(char *s){ return strlen(s);}/* print a unicode string */void uniprint(char *first,int length){ while(length--){ putchar(*first++); /* character above 255 or not a valid unicode string */ if(*first++){ printf("!!!!Error printing file name\n"); return; } }}void uniprint_lower(char *first,int length){ while(length--){ putchar(tolower(*first)); first++; /* character above 255 or not a valid unicode string */ if(*first++){ printf("!!!!Error printing file name\n"); return; } }}/* unit of 100ns since 1.1.1601 */void print_time(ntfs_time64_t t){ ntfs_time64_t sec; ntfs_time_t unix_utc; char *str; sec=t/10000000; unix_utc=sec-((ntfs_time64_t)369*365+89)*24*3600; str=ctime(&unix_utc); /* remove \n */ str[strlen(str)-1]='\0'; printf("%s",str);}ntfs_time64_t ntfs_now(){ return ntfs_unixutc2ntutc(time(0));}/* answer the MFT record number for file name in directory ino */int ntfs_find_file(ntfs_inode* ino,char *name){ char item[1000]; ntfs_iterate_s walk; int error=ntfs_decodeuni(ino->vol,name,strlen(name), &walk.name,&walk.namelen); if(error) return -1; walk.type=BY_NAME; walk.dir=ino; walk.result=item; if(!ntfs_getdir_byname(&walk))return -1; ntfs_free(walk.name); return *(int*)item;}/* * Local variables: * c-file-style: "linux" * End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -