File: ntchange.c

package info (click to toggle)
ntfs 971218-4
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 692 kB
  • ctags: 670
  • sloc: ansic: 7,774; sh: 1,509; makefile: 232
file content (270 lines) | stat: -rw-r--r-- 6,510 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 *  ntchange.c
 *
 *  Copyright (C) 1995-1997 Martin von Lwis
 *  Copyright (C) 1997 Rgis Duchesne
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#else
#define getopt_long(a,v,o,ol,x)        getopt(a,v,o)
#endif
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include "types.h"
#include "struct.h"
#include "util.h"
#include "nttools.h"
#include "inode.h"
#include "super.h"
#include "attr.h"

char *short_opts="f:s:a:p:d:W:t:i:A:L:n:V";
#ifdef HAVE_GETOPT_H
struct 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}
};
#endif

void my_perror(const char* msg,int error)
{
	fprintf(stderr,"%s:%s\n",msg,strerror(error));
	exit(1);
}

int 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;
	io.size=count;
	ntfs_write_attr(ino,ino->vol->at_data,NULL,offset,&io);
}

int main(int argc,char *argv[])
{
	int c,error;
	char *device=0;
	char *source=0;
	char *name=0;
	void *buf;
	int fsource;
	ntfs_volume *volume;
	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);
		}
	volume=ntfs_open_volume(device,0,1,0);
	if(!volume)return 1;
	if(alloc_start!=-2){
		error=ntfs_allocate_clusters(volume,&alloc_start,&alloc_size,0);
		if(error)my_perror("allocate",error);
		printf("Got range %x+%x\n",alloc_start,alloc_size);
		return 0;
	}
	if(dealloc_start!=-2){
		error=ntfs_deallocate_clusters(volume,dealloc_start,alloc_size);
		if(error)my_perror("deallocate",error);
		return 0;
	}
	if(Winum){
		error=ntfs_init_inode(&ino,volume,Winum);
		if(error)my_perror("winum",error);
		error=ntfs_update_inode(&ino);
		if(error)my_perror("winum",error);
		return 0;
	}
	if(truncate!=-2){
		if(inum==-1){
			usage();
			return 1;
		}
		error=ntfs_init_inode(&ino,volume,inum);
		if(error)my_perror("init",error);
		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)my_perror("resize",error);
		error=ntfs_update_inode(&ino);
		if(error)my_perror("writeback",error);
		return 0;
	}
	if(symlink){
		unsigned short *data;
		int nlen;
		ntfs_attribute* aresult;
		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);
		error=ntfs_init_inode(&ino,volume,inum);
		if(error)my_perror("init",error);
		error=ntfs_create_attr(&ino,volume->at_symlink,0,
				       data,nlen*2,&aresult);
		if(error)my_perror("create symlink",error);
		error=ntfs_update_inode(&ino);
		if(error)my_perror("writeback",error);
		return 0;
	}
	if(newfile){
		ntfs_inode dir;
		if(inum<0){
			fprintf(stderr,"no dir given for newfile\n");
			return 1;
		}
		error=ntfs_init_inode(&dir,volume,inum); /* FIXME: check for dir */
		if(error)my_perror("init",error);
		error=ntfs_alloc_inode(&dir,&ino,newfile,strlen(newfile));
		if(error)my_perror("create new file",error);
		error=ntfs_update_inode(&ino);
		if(error)my_perror("writeback",error);
		error=ntfs_update_inode(&ino);
		if(error)my_perror("directory writeback",error);
		fprintf(stderr,"allocated inode %x\n",ino.i_number);
		/*error=ntfs_dir_add1(&dir,newfile,strlen(newfile),&ino);
		if(error){
			fprintf(stderr,"error %x when modifying directory\n",
				error);
			return 1;
		}
		*/
		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;
		error=ntfs_init_inode(&ino,volume,inum);
		if(error)my_perror("init",error);
		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:
 */