File: nttools.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 (177 lines) | stat: -rw-r--r-- 3,679 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
/*
 *  nttools.c
 *  Helper functions for the tools
 *
 *  Copyright (C) 1995-1997 Martin von Lwis
 *  Copyright (C) 1997 Rgis Duchesne
 */

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

#include "types.h"
#include "struct.h"
#include "nttools.h"

#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdarg.h>
#include <time.h>
#include <ctype.h>
#include <stdlib.h>
#include "super.h"
#include "inode.h"
#include "dir.h"
#include "macros.h"
#include "support.h"
#include "util.h"

ntfs_size_t ntfs_lseek(int fd, ntfs_size_t offset, int whence)
{
#ifdef __linux__
        /* Linux should have llseek, although glibc does not have a prototype */
	loff_t llseek(int,loff_t, int);
	return llseek(fd,(loff_t)offset,whence);
#else
	return lseek(fd,(off_t)offset,whence);
#endif
}

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;
}

ntfs_volume *ntfs_open_volume(char *file, int bias, int silent, int no_inodes)
{
	int fd;
	char cluster0[512];
	ntfs_io io;
	ntfs_volume *newvol;

	io.fn_put=0;
	io.fn_get=0; /* we don't need copy functions here */
	newvol=malloc(sizeof(ntfs_volume));
        newvol->partition_bias=bias;
#ifdef NTFS_VOLUME
	NTFS_FD(newvol)=fd=open_volume(file=file?file:NTFS_VOLUME);
#else
	if(!file){
		fprintf(stderr,"No volume specified or configured\n");
		return 0;
	}
	NTFS_FD(newvol)=fd=open_volume(file);
#endif
	if(fd<0)
	{	
		return 0;
	}
	/* read the boot sector */
	io.do_read=1;
	io.param=cluster0;
	io.size=512;
	ntfs_getput_clusters(newvol,0,0,&io);
	if(!IS_NTFS_VOLUME(cluster0)){
		fprintf(stderr,"Not a NTFS volume:%s\n",file);
		return 0;
	}
	ntfs_init_volume(newvol,cluster0);
	/* read the first mft record */
	newvol->mft=malloc(newvol->mft_recordsize);
	io.do_read=1;
	io.param=newvol->mft;
	io.size=newvol->mft_recordsize;
	ntfs_getput_clusters(newvol,newvol->mft_cluster,0,&io);
	/* fix it */
	if(!ntfs_check_mft_record(newvol,newvol->mft)){
		fprintf(stderr,"MFT record not at cluster 0x%X\n",newvol->mft_cluster);
		return 0;
	}
	if(!silent)
		fprintf(stderr,"MFT record at block 0x%X, offset 0x%X\n",newvol->mft_cluster,
			newvol->mft_cluster*newvol->clustersize);
	if(!no_inodes)
		ntfs_load_special_files(newvol);
	return newvol;
}

/* 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);
}

/* 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;
	free(walk.name);
	return *(int*)item;
}

/*
 * Local variables:
 * c-file-style: "linux"
 * End:
 */