📄 ntchange.c
字号:
/* * ntchange.c * * Copyright (C) 1995 Martin von L鰓is */#include "config.h"#include <stdio.h>#include <stdlib.h>#ifdef HAVE_GETOPT_LONG#include <getopt.h>#else#define getopt_long(a,v,o,ol,x) getopt(a,v,o)#endif#include <unistd.h>#include <string.h>#include <sys/stat.h>#include <fcntl.h>#include "ntfs.h"#include "version.h"char *short_opts="f:s:a:p:d:W:t:i:A:L:n:V";#ifdef HAVE_GETOPT_LONGstruct option options[]={ {"filesystem",1,0,'f'}, {"source",1,0,'s'}, {"alloc-clusters",1,0,'a'}, {"dealloc-clusters",1,0,'d'}, {"path",1,0,'p'}, {"write-inode",1,0,'W'}, {"truncate",1,0,'t'}, {"inum",1,0,'i'}, {"anum",1,0,'A'}, {"symlink",1,0,'L'}, {"new-file",1,0,'n'}, {"version",0,0,'V'}, {0,0,0,0}};#endifint usage(void){ fprintf(stderr,"ntchange [options]\n" "-f device Volume\n" "-s source Source file for modification\n" "-o offset Offset in file\n" "-p [directory/]filename File path to modify\n" "-a start size Allocate cluster range\n" "-d start size Deallocate cluster range\n" "-W inum Read and write inode inum\n" "-t newsize truncate inode to newsize\n" "-i inum operate on inode inum\n" "-A anum operate on attribute anum (80)\n" "-L string make inode a symlink with value string\n" "-n name create file name in dir inum\n" "-V print version\n" ); return 1;}/* print the file ino on stdout */void ntfs_change_file(ntfs_inode *ino,char *buf,int offset,int count){ int position; ntfs_io io; position=0; /* write to the unnamed data attribute */ io.fn_put=0; io.fn_get=0; /* we don't need copyfunctions here */ io.param=buf; ntfs_write_attr(ino,AT_DATA,NULL, offset,&io,count);}int main(int argc,char *argv[]){ int c,error; char *device=0; char *source=0; char *name=0; void *buf; int fsource; ntfs_inode ino; int inum=-1; int offset=0; int alloc_start=-2,alloc_size=0,dealloc_start=-2; int Winum=0; int truncate=-2,anum=0x80; char *symlink=0; char *newfile=0; struct stat sb; extern int optind,opterr; extern char* optarg; ntfs_attribute *attr; opterr=1; while((c=getopt_long(argc,argv,short_opts,options,NULL))>0) switch(c) { case 'f': device=strdup(optarg);break; case 's': source=strdup(optarg);break; case 'o': offset=strtol(optarg,NULL,0);break; case 'p': name=optarg;break; case 'a': alloc_start=strtol(optarg,NULL,0); alloc_size=strtol(argv[optind++],NULL,0); break; case 'd': dealloc_start=strtol(optarg,NULL,0); alloc_size=strtol(argv[optind++],NULL,0); break; case 'W': Winum=strtol(optarg,NULL,0);break; case 't': truncate=strtol(optarg,NULL,0);break; case 'i': inum=strtol(optarg,NULL,0);break; case 'A': anum=strtol(optarg,NULL,0);break; case 'L': symlink=optarg;break; case 'n': newfile=optarg;break; case 'V': printf("ntdir " NTFS_VERSION "\n");exit(0);break; default: usage();exit(0); } if(!ntfs_open_volume(device,0,1,0))return 1; if(alloc_start!=-2){ error=ntfs_allocate_clusters(the_vol,&alloc_start,&alloc_size); if(error) printf("error %x\n",error); else printf("Got range %x+%x\n",alloc_start,alloc_size); return 0; } if(dealloc_start!=-2){ error=ntfs_deallocate_clusters(the_vol,dealloc_start,alloc_size); if(error) printf("error %x\n",error); return 0; } if(Winum){ ntfs_init_inode(&ino,the_vol,Winum); error=update_inode(&ino); if(error) printf("error %x\n",error); return 0; } if(truncate!=-2){ if(inum==-1){ usage(); return 1; } ntfs_init_inode(&ino,the_vol,inum); attr=ntfs_find_attr(&ino,anum,0); if(!attr){ fprintf(stderr,"Inode %x has no attribute %x\n",inum,anum); return 1; } error=ntfs_resize_attr(&ino,attr,truncate); if(error){ fprintf(stderr,"error %x resizing\n",error); return 1; } error=update_inode(&ino); if(error){ fprintf(stderr,"error %x when writing back\n",error); return 1; } return 0; } if(symlink){ unsigned short *data; int nlen; if(inum<0){ fprintf(stderr,"No inum given to for symlink"); return 1; } nlen=strlen(symlink); data=malloc(nlen*2); ntfs_ascii2uni(data,symlink,nlen); ntfs_init_inode(&ino,the_vol,inum); error=ntfs_create_attr(&ino,AT_SYMBOLIC_LINK,0,data,nlen*2); if(error){ fprintf(stderr,"error %x when creating symlink\n",error); return 1; } error=update_inode(&ino); if(error){ fprintf(stderr,"error %x when writing back\n",error); return 1; } return 0; } if(newfile){ ntfs_inode dir; if(inum<0){ fprintf(stderr,"no dir given for newfile\n"); return 1; } ntfs_init_inode(&dir,the_vol,inum); /* FIXME: check for dir */ error=ntfs_alloc_inode(&dir,&ino,newfile,strlen(newfile)); if(error){ fprintf(stderr,"error %x while creating newfile\n",error); return 1; } error=update_inode(&ino); if(error){ fprintf(stderr,"error %x when writing back\n",error); return 1; } fprintf(stderr,"allocated inode %x\n",ino.i_number); return 0; } if(!source) return usage(); fsource=open(source,O_RDONLY); if(fsource==-1) { perror(source); return 1; } if(fstat(fsource,&sb)!=0) { perror(source); return 1; } buf=malloc(sb.st_size); if(!buf) { perror(argv[0]); return 1; } if(read(fsource,buf,sb.st_size)!=sb.st_size) { perror(argv[0]); return 1; } inum=5; /* walk the directory tree */ do{ char *next; ntfs_init_inode(&ino,the_vol,inum); if(!name || !*name)break; next=strchr(name,'/'); if(next){ *next='\0'; next++; } inum=ntfs_find_file(&ino,name); if(inum==-1){ printf("%s not found\n",name); return 1; } name=next; }while(1); ino.i_number=inum; ntfs_change_file(&ino,buf,offset,sb.st_size); return 0;}/* * Local variables: * c-file-style: "linux" * End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -