5 A part of the extended file system 2 disk editor.
7 -------------------------------------------------
8 The filesystem's disk activity pass through here.
9 -------------------------------------------------
11 This file is acting as a filter - Before we pass an actual read or write request to the operating system, we
12 double check the various permissions and possible errors here.
14 The major update which needs to be done here is switching to the use of the llseek system call, so that we will
15 be able to support ext2 filesystems up to 4 TB. Currently, due to the standard fseek usage, we can't handle
16 filesystems bigger than 4 GB. The limit is actually 2 GB because I used long rather than unsigned long long at too
17 many places in the program. To conclude - This upgrade needs to be done carefully; There are many places to change.
19 First written on: April 9 1995
21 Copyright (C) 1995 Gadi Oxman
35 int low_read (unsigned char *buffer,unsigned long length,unsigned long offset)
39 This function is used when we need to read something from the filesystem.
49 if (device_handle==NULL) { /* Check that a device is indeed open */
50 internal_error ("No device opened yet read requested","disk","low_read");
53 if (offset > file_system_info.file_system_size) { /* Check that the offset is within limits */
54 sprintf (temp,"Seek offset %ld is out of range",offset);
55 internal_error (temp,"disk","low_read");
61 if ( (fseek (device_handle,offset,SEEK_SET))==-1) { /* Seek to the required offset */
62 wprintw (command_win,"Error - Failed to seek to offset %ld in device %s\n",offset,device_name);
63 refresh_command_win ();
67 if ( (fread (buffer,1,length,device_handle))==-1) { /* And do the actual reading */
68 wprintw (command_win,"Error - Failed to read from offset %ld in device %s\n",offset,device_name);
69 refresh_command_win ();return (0);
75 int low_write (unsigned char *buffer,unsigned long length,unsigned long offset)
79 This is used to change something in the filesystem.
80 write_access is checked to see if we are allowed to do the actual writing.
81 As a double safety measure, AllowChanges is rechecked here.
82 If logging is enabled, we log the change before writing it to the device.
89 wprintw (command_win,"Error - Write access not available (use enablewrite)\n");
96 internal_error ("AllowChanges=0 yet enablewrite succeeded","disk","low_write");
100 if (device_handle==NULL) {
101 internal_error ("No device opened yet read requested","disk","low_write");
105 if (offset > file_system_info.file_system_size) {
106 sprintf (temp,"Seek offset %ld is out of range",offset);
107 internal_error (temp,"disk","low_write");
114 if (!log_changes (buffer,length,offset))
117 if ( (fseek (device_handle,offset,SEEK_SET))==-1) {
118 wprintw (command_win,"Error - Failed to seek to offset %ld in device %s\n",offset,device_name);
119 refresh_command_win ();return (0);
123 if ( (fwrite (buffer,1,length,device_handle))==-1) {
124 wprintw (command_win,"Error - Failed to write to offset %ld in device %s\n",offset,device_name);
125 refresh_command_win ();return (0);
128 wprintw (command_win,"Data written");refresh_command_win ();
132 int log_changes (unsigned char *buffer,unsigned long length,unsigned long offset)
136 Log the change in a primitive form - An hex dump of the data before the change and after the change.
137 The hex bytes are converted to text, so that they will be readable with a standard text editor.
142 unsigned char *original;
148 if ((fp=fopen (LogFile,"a+"))==NULL) {
149 wprintw (command_win,"Error - Unable to open log file %s\n",LogFile);
150 refresh_command_win ();return (0);
153 current_time=time (NULL);
155 fprintf (fp,"\n----- EXT2ED log begin -----\n\n");
156 fprintf (fp,"Time: %s\nDevice: %s\n",ctime ((time_t *) ¤t_time),device_name);
157 fprintf (fp,"Offset: %lu\nLength: %lu\n",offset,length);
159 original=(unsigned char *) malloc (length*sizeof (unsigned char));
161 if (original==NULL) {
162 wprintw (command_win,"Fatal error - Can\'t allocate %lu bytes!");
163 refresh_command_win ();fclose (fp);return (0);
166 if (!low_read (original,length,offset)) {
167 fclose (fp);return (0);
170 fprintf (fp,"\nOriginal data:\n\n");
172 for (i=0;i<length;i++) {
173 if (i%16==0 && i!=0) fprintf (fp,"\n");
174 fprintf (fp,"%02x ",original [i]);
177 fprintf (fp,"\n\nNew data:\n\n");
179 for (i=0;i<length;i++) {
180 if (i%16==0 && i!=0) fprintf (fp,"\n");
181 fprintf (fp,"%02x ",buffer [i]);
184 fprintf (fp,"\n----- EXT2ED log end -----\n");
190 int load_type_data (void)
194 Just read from the current position into type data.
199 if (device_handle==NULL) {
200 printf ("Error - No device opened\n");
204 if (device_offset==-1) {
205 printf ("Error - No offset set\n");
209 if (low_read (type_data.u.buffer,EXT2_MAX_BLOCK_SIZE,device_offset)==0)
212 if (current_type!=NULL)
213 if (strcmp (current_type->name,"ext2_dir_entry")==0)
214 current_type->length=type_data.u.t_ext2_dir_entry.rec_len;
219 int write_type_data (void)
222 if (device_handle==NULL) {
223 wprintw (command_win,"Error - No device opened\n");
224 refresh_command_win ();
228 if (device_offset==-1) {
229 wprintw (command_win,"Error - No offset set\n");
230 refresh_command_win ();
234 if (low_write (type_data.u.buffer,file_system_info.block_size,device_offset)==0)