1<!DOCTYPE Article PUBLIC "-//Davenport//DTD DocBook V3.0//EN">
2
3<Article>
4
5<ArtHeader>
6
7<Title>EXT2ED - The Extended-2 filesystem editor - Design and implementation</Title>
8<AUTHOR
9>
10<FirstName>Programmed by Gadi Oxman, with the guide of Avner Lottem</FirstName>
11</AUTHOR
12>
13<PubDate>v0.1, August 3 1995</PubDate>
14
15</ArtHeader>
16
17<Sect1>
18<Title>About EXT2ED documentation</Title>
19
20<Para>
21The EXT2ED documentation consists of three parts:
22
23<ItemizedList>
24<ListItem>
25
26<Para>
27	The ext2 filesystem overview.
28</Para>
29</ListItem>
30<ListItem>
31
32<Para>
33	The EXT2ED user's guide.
34</Para>
35</ListItem>
36<ListItem>
37
38<Para>
39	The EXT2ED design and implementation.
40</Para>
41</ListItem>
42
43</ItemizedList>
44
45</Para>
46
47<Para>
48This document is not the user's guide. If you just intend to use EXT2ED, you
49may not want to read it.
50</Para>
51
52<Para>
53However, if you intend to browse and modify the source code, this document is
54for you.
55</Para>
56
57<Para>
58In any case, If you intend to read this article, I strongly suggest that you
59will be familiar with the material presented in the other two articles as well.
60</Para>
61
62</Sect1>
63
64<Sect1>
65<Title>Preface</Title>
66
67<Para>
68In this document I will try to explain how EXT2ED is constructed.
69At this time of writing, the initial version is finished and ready
70for distribution; It is fully functional. However, this was not always the
71case.
72</Para>
73
74<Para>
75At first, I didn't know much about Unix, much less about Unix filesystems,
76and even less about Linux and the extended-2 filesystem. While working
77on this project, I gradually acquired knowledge about all of the above
78subjects. I can think of two ways in which I could have made my project:
79
80<OrderedList>
81<ListItem>
82
83<Para>
84	The "Engineer" way
85
86Learn the subject throughly before I get to the programming itself.
87Then, I could easily see the entire picture and select the best
88course of action, taking all the factors into account.
89</Para>
90</ListItem>
91<ListItem>
92
93<Para>
94	The "Explorer - Progressive" way.
95
96Jump immediately into the cold water - Start programming and
97learning the material parallelly.
98</Para>
99</ListItem>
100
101</OrderedList>
102
103</Para>
104
105<Para>
106I guess that the above dilemma is typical and appears all through science and
107technology.
108</Para>
109
110<Para>
111However, I didn't have the luxury of choice when I started my project -
112Linux is a relatively new (and great!) operating system. The extended-2
113filesystem is even newer - Its first release lies somewhere in 1993 - Only
114passed two years until I started working on my project.
115</Para>
116
117<Para>
118The situation I found myself at the beginning was that I didn't have a fully
119detailed document which describes the ext2 filesystem. In fact, I didn't
120have any ext2 document at all. When I asked Avner about documentation, he
121suggested two references:
122
123<ItemizedList>
124<ListItem>
125
126<Para>
127	A general Unix book - THE DESIGN OF THE UNIX OPERATING SYSTEM, by
128Maurice J. Bach.
129</Para>
130</ListItem>
131<ListItem>
132
133<Para>
134	The kernel sources.
135</Para>
136</ListItem>
137
138</ItemizedList>
139
140I read the relevant parts of the book before I started my project - It is a
141bit old now, but the principles are still the same. However, I needed
142more than just the principles.
143</Para>
144
145<Para>
146The kernel sources are a rare bonus! You don't get everyday the full
147sources of the operating system. There is so much that can be learned from
148them, and it is the ultimate source - The exact answer how the kernel
149works is there, with all the fine details. At the first week I started to
150look at random at the relevant parts of the sources. However, it is difficult
151to understand the global picture from direct reading of over one hundred
152page sources. Then, I started to do some programming. I didn't know
153yet what I was looking for, and I started to work on the project like a kid
154who starts to build a large puzzle.
155</Para>
156
157<Para>
158However, this was exactly the interesting part! It is frustrating to know
159it all from advance - I think that the discovery itself, bit by bit, is the
160key to a true learning and understanding.
161</Para>
162
163<Para>
164Now, in this document, I am trying to present the subject. Even though I
165developed EXT2ED progressively, I now can see the entire subject much
166brighter than I did before, and though I do have the option of presenting it
167only in the "engineer" way. However, I will not do that.
168</Para>
169
170<Para>
171My presentation will be mixed - Sometimes I will present a subject with an
172incremental perspective, and sometimes from a "top down" view. I'll leave
173you to decide if my presentation choice was wise :-)
174</Para>
175
176<Para>
177In addition, you'll notice that the sections tend to get shorter as we get
178closer to the end. The reason is simply that I started to feel that I was
179repeating myself so I decided to present only the new ideas.
180</Para>
181
182</Sect1>
183
184<Sect1>
185<Title>Getting started ...</Title>
186
187<Para>
188Getting started is almost always the most difficult task. Once you get
189started, things start "running" ...
190</Para>
191
192<Sect2>
193<Title>Before the actual programming</Title>
194
195<Para>
196From mine talking with Avner, I understood that Linux, like any other Unix
197system, provides accesses to the entire disk as though it were a general
198file - Accessing the device. It is surely a nice idea. Avner suggested two
199ways of action:
200
201<ItemizedList>
202<ListItem>
203
204<Para>
205	Opening the device like a regular file in the user space.
206</Para>
207</ListItem>
208<ListItem>
209
210<Para>
211	Constructing a device driver which will run in the kernel space and
212provide hooks for the user space program. The advantage is that it
213will be a part of the kernel, and would be able to use the ext2
214kernel functions to do some of the work.
215</Para>
216</ListItem>
217
218</ItemizedList>
219
220I chose the first way. I think that the basic reason was simplicity - Learning
221the ext2 filesystem was complicated enough, and adding to it the task of
222learning how to program in the kernel space was too much. I still don't know
223how to program a device driver, and this is perhaps the bad part, but
224concerning the project in a back-perspective, I think that the first way is
225superior to the second; Ironically, because of the very reason I chose it -
226Simplicity. EXT2ED can now run entirely in the user space (which I think is
227a point in favor, because it doesn't require the user to recompile its
228kernel), and the entire hard work is mine, which fitted nicely into the
229learning experience - I didn't use other code to do the job (aside from
230looking at the sources, of-course).
231</Para>
232
233</Sect2>
234
235<Sect2>
236<Title>Jumping into the cold water</Title>
237
238<Para>
239I didn't know almost anything of the structure of the ext2 filesystem.
240Reading the sources was not enough - I needed to experiment. However, a tool
241for experiments in the ext2 filesystem was exactly my project! - Kind of a
242paradox.
243</Para>
244
245<Para>
246I started immediately with constructing a simple <Literal remap="tt">hex editor</Literal> - It would
247open the device as a regular file, provide means of moving inside the
248filesystem with a simple <Literal remap="tt">offset</Literal> method, and just show a
249<Literal remap="tt"> hex dump</Literal> of the contents at this point. Programming this was trivially
250simple of-course. At this point, the user-interface didn't matter to me - I
251wanted a fast way to interact. As a result, I chose a simple command line
252parser. Of course, there where no windows at this point.
253</Para>
254
255<Para>
256A hex editor is nice, but is not enough. It indeed enabled me to see each part
257of the filesystem, but the format of the viewed data was difficult to
258analyze. I wanted to see the data in a more intuitive way.
259</Para>
260
261<Para>
262At this point of time, the most helpful file in the sources was the ext2
263main include file - <Literal remap="tt">/usr/include/linux/ext2&lowbar;fs.h</Literal>. Among its contents
264there were various structures which I assumed they are disk images - Appear
265exactly like that on the disk.
266</Para>
267
268<Para>
269I wanted a <Literal remap="tt">quick</Literal> way to get going. I didn't have the patience to learn
270each of the structures use in the code. Rather, I wanted to see them in action,
271so that I could explore the connections between them - Test my assumptions,
272and reach other assumptions.
273</Para>
274
275<Para>
276So after the <Literal remap="tt">hex editor</Literal>, EXT2ED progressed into a tool which has some
277elements of a compiler. I programmed EXT2ED to <Literal remap="tt">dynamically read the kernel
278ext2 main include file in run time</Literal>, and process the information. The goal
279was to <Literal remap="tt">imply a structure-definition on the current offset at the
280filesystem</Literal>. EXT2ED would then display the structure as a list of its
281variables names and contents, instead of a meaningless hex dump.
282</Para>
283
284<Para>
285The format of the include file is not very complicated - The structures
286are mostly <Literal remap="tt">flat</Literal> - Didn't contain a lot of recursive structure; Only a
287global structure definition, and some variables. There were cases of
288structures inside structures, I treated them in a somewhat non-elegant way - I
289made all the structures flat, and expanded the arrays. As a result, the parser
290was very simple. After all, this was not an exercise in compiling, and I
291wanted to quickly get some results.
292</Para>
293
294<Para>
295To handle the task, I constructed the <Literal remap="tt">struct&lowbar;descriptor</Literal> structure.
296Each <Literal remap="tt">struct&lowbar;descriptor instance</Literal> contained information which is needed
297in order to format a block of data according to the C structure contained in 
298the kernel source. The information contained:
299
300<ItemizedList>
301<ListItem>
302
303<Para>
304	The descriptor name, used to reference to the structure in EXT2ED.
305</Para>
306</ListItem>
307<ListItem>
308
309<Para>
310	The name of each variable.
311</Para>
312</ListItem>
313<ListItem>
314
315<Para>
316	The relative offset of the each variable in the data block.
317</Para>
318</ListItem>
319<ListItem>
320
321<Para>
322	The length, in bytes, of each variable.
323</Para>
324</ListItem>
325
326</ItemizedList>
327
328Since I didn't want to limit the number of structures, I chose a simple
329double linked list to store the information. One variable contained the
330<Literal remap="tt">current structure type</Literal> - A pointer to the relevant
331<Literal remap="tt">struct&lowbar;descriptor</Literal>.
332</Para>
333
334<Para>
335Now EXT2ED contained basically three command line operations:
336
337<ItemizedList>
338<ListItem>
339
340<Para>
341	setdevice
342
343Used to open a device for reading only. Write access was postponed
344to a very advanced state in the project, simply because I didn't
345know a thing of the filesystem structure, and I believed that
346making actual changes would do nothing but damage :-)
347</Para>
348</ListItem>
349<ListItem>
350
351<Para>
352	setoffset
353
354Used to move in the device.
355</Para>
356</ListItem>
357<ListItem>
358
359<Para>
360	settype
361
362Used to imply a structure definition on the current place.
363</Para>
364</ListItem>
365<ListItem>
366
367<Para>
368	show
369
370Used to display the data. It displayed the data in a simple hex dump
371if there was no type set, or in a nice formatted way - As a list of
372the variable contents, if there was.
373</Para>
374</ListItem>
375
376</ItemizedList>
377
378</Para>
379
380<Para>
381Command line analyzing was primitive back then - A simple switch, as far as
382I can remember - Nothing alike the current flow control, but it was enough
383at the time.
384</Para>
385
386<Para>
387At the end, I had something to start working with. It knew to format many
388structures - None of which I understood - and provided me, without too much
389work, something to start with.
390</Para>
391
392</Sect2>
393
394</Sect1>
395
396<Sect1>
397<Title>Starting to explore</Title>
398
399<Para>
400With the above tool in my pocket, I started to explore the ext2 filesystem
401structure. From the brief reading in Bach's book, I got familiar to some
402basic concepts - The <Literal remap="tt">superblock</Literal>, for example. It seems that the
403superblock is an important part of the filesystem. I decided to start
404exploring with that.
405</Para>
406
407<Para>
408I realized that the superblock should be at a fixed location in the
409filesystem - Probably near the beginning. There can be no other way -
410The kernel should start at some place to find it. A brief looking in
411the kernel sources revealed that the superblock is signed by a special
412signature - A <Literal remap="tt">magic number</Literal> - EXT2&lowbar;SUPER&lowbar;MAGIC (0xEF53 - EF probably
413stands for Extended Filesystem). I quickly found the superblock at the
414fixed offset 1024 in the filesystem - The <Literal remap="tt">s&lowbar;magic</Literal> variable in the
415superblock was set exactly to the above value.
416</Para>
417
418<Para>
419It seems that starting with the <Literal remap="tt">superblock</Literal> was a good bet - Just from
420the list of variables, one can learn a lot. I didn't understand all of them
421at the time, but it seemed that the following keywords were repeating themself
422in various variables:
423
424<ItemizedList>
425<ListItem>
426
427<Para>
428	block
429</Para>
430</ListItem>
431<ListItem>
432
433<Para>
434	inode
435</Para>
436</ListItem>
437<ListItem>
438
439<Para>
440	group
441</Para>
442</ListItem>
443
444</ItemizedList>
445
446At this point, I started to explore the block groups. I will not detail here
447the technical design of the ext2 filesystem. I have written a special
448article which explains just that, in the "engineering" way. Please refer to it
449if you feel that you are lacking knowledge in the structure of the ext2
450filesystem.
451</Para>
452
453<Para>
454I was exploring the filesystem in this way for some time, along with reading
455the sources. This lead naturally to the next step.
456</Para>
457
458</Sect1>
459
460<Sect1>
461<Title>Object specific commands</Title>
462
463<Para>
464What has become clear is that the above way of exploring is not powerful
465enough - I found myself doing various calculations manually in order to pass
466between related structures. I needed to replace some tasks with an automated
467procedure.
468</Para>
469
470<Para>
471In addition, it also became clear that (of-course) each key object in the
472filesystem has its special place in regard to the overall ext2 filesystem
473design, and needs a <Literal remap="tt">fine tuned handling</Literal>. It is at this point that the
474structure definitions <Literal remap="tt">came to life</Literal> - They became <Literal remap="tt">object
475definitions</Literal>, making EXT2ED <Literal remap="tt">object oriented</Literal>.
476</Para>
477
478<Para>
479The actual meaning of the breathtaking words above, is that each structure
480now had a list of <Literal remap="tt">private commands</Literal>, which ended up in
481<Literal remap="tt">calling special fine-tuned C functions</Literal>. This approach was
482found to be very powerful and is <Literal remap="tt">the heart of EXT2ED even now</Literal>.
483</Para>
484
485<Para>
486In order to implement the above concepts, I added the structure
487<Literal remap="tt">struct&lowbar;commands</Literal>. The role of this structure is to group together a
488group of commands, which can be later assigned to a specific type. Each
489structure had:
490
491<ItemizedList>
492<ListItem>
493
494<Para>
495	A list of command names.
496</Para>
497</ListItem>
498<ListItem>
499
500<Para>
501	A list of pointers to functions, which binds each command to its
502special fine-tuned C function.
503</Para>
504</ListItem>
505
506</ItemizedList>
507
508In order to relate a list of commands to a type definition, each
509<Literal remap="tt">struct&lowbar;descriptor</Literal> structure (explained earlier) was added a private
510<Literal remap="tt">struct&lowbar;commands</Literal> structure.
511</Para>
512
513<Para>
514Follows the current definitions of <Literal remap="tt">struct&lowbar;descriptor</Literal> and of
515<Literal remap="tt">struct&lowbar;command</Literal>:
516
517<ProgramListing>
518struct struct_descriptor {
519        unsigned long length;
520        unsigned char name [60];
521        unsigned short fields_num;
522        unsigned char field_names [MAX_FIELDS][80];
523        unsigned short field_lengths [MAX_FIELDS];
524        unsigned short field_positions [MAX_FIELDS];
525        struct struct_commands type_commands;
526        struct struct_descriptor *prev,*next;
527};
528
529typedef void (*PF) (char *);
530
531struct struct_commands {
532        int last_command;
533        char *names [MAX_COMMANDS_NUM];
534        char *descriptions [MAX_COMMANDS_NUM];
535        PF callback [MAX_COMMANDS_NUM];
536};
537</ProgramListing>
538
539
540</Para>
541
542</Sect1>
543
544<Sect1 id="flow-control">
545<Title>Program flow control</Title>
546
547<Para>
548Obviously the above approach lead to a major redesign of EXT2ED. The
549main engine of the resulting design is basically the same even now.
550</Para>
551
552<Para>
553I redesigned the program flow control. Up to now, I analyzed the user command
554line with the simple switch method. Now I used the far superior callback
555method.
556</Para>
557
558<Para>
559I divided the available user commands into two groups:
560
561<OrderedList>
562<ListItem>
563
564<Para>
565	General commands.
566</Para>
567</ListItem>
568<ListItem>
569
570<Para>
571	Type specific commands.
572</Para>
573</ListItem>
574
575</OrderedList>
576
577As a result, at each point in time, the user was able to enter a
578<Literal remap="tt">general command</Literal>, selectable from a list of general commands which was
579always available, or a <Literal remap="tt">type specific command</Literal>, selectable from a list of
580commands which <Literal remap="tt">changed in time</Literal> according to the current type that the
581user was editing. The special <Literal remap="tt">type specific command</Literal> "knew" how to
582handle the object in the best possible way - It was "fine tuned" for the
583object's place in the ext2 filesystem design.
584</Para>
585
586<Para>
587In order to implement the above idea, I constructed a global variable of
588type <Literal remap="tt">struct&lowbar;commands</Literal>, which contained the <Literal remap="tt">general commands</Literal>.
589The <Literal remap="tt">type specific commands</Literal> were accessible through the <Literal remap="tt">struct
590descriptors</Literal>, as explained earlier.
591</Para>
592
593<Para>
594The program flow was now done according to the following algorithm:
595
596<OrderedList>
597<ListItem>
598
599<Para>
600	Ask the user for a command line.
601</Para>
602</ListItem>
603<ListItem>
604
605<Para>
606	Analyze the user command - Separate it into <Literal remap="tt">command</Literal> and
607<Literal remap="tt">arguments</Literal>.
608</Para>
609</ListItem>
610<ListItem>
611
612<Para>
613	Trace the list of known objects to match the command name to a type.
614If the type is found, call the callback function, with the arguments
615as a parameter. Then go back to step (1).
616</Para>
617</ListItem>
618<ListItem>
619
620<Para>
621	If the command is not type specific, try to find it in the general
622commands, and call it if found. Go back to step (1).
623</Para>
624</ListItem>
625<ListItem>
626
627<Para>
628	If the command is not found, issue a short error message, and return
629to step (1).
630</Para>
631</ListItem>
632
633</OrderedList>
634
635Note the <Literal remap="tt">order</Literal> of the above steps. In particular, note that a command
636is first assumed to be a type-specific command and only if this fails, a
637general command is searched. The "<Literal remap="tt">side-effect</Literal>" (main effect, actually)
638is that when we have two commands with the <Literal remap="tt">same name</Literal> - One that is a 
639type specific command, and one that is a general command, the dispatching
640algorithm will call the <Literal remap="tt">type specific command</Literal>. This allows
641<Literal remap="tt">overriding</Literal> of a command to provide <Literal remap="tt">fine-tuned</Literal> operation.
642For example, the <Literal remap="tt">show</Literal> command is overridden nearly everywhere,
643to accommodate for the different ways in which different objects are displayed,
644in order to provide an intuitive fine-tuned display.
645</Para>
646
647<Para>
648The above is done in the <Literal remap="tt">dispatch</Literal> function, in <Literal remap="tt">main.c</Literal>. Since
649it is a very important function in EXT2ED, and it is relatively short, I will
650list it entirely here. Note that a redesign was made since then - Another
651level was added between the two described, but I'll elaborate more on this
652later. However, the basic structure follows the explanation described above.
653
654<ProgramListing>
655int dispatch (char *command_line)
656
657{
658	int i,found=0;
659	char command [80];
660
661	parse_word (command_line,command);
662			
663	if (strcmp (command,"quit")==0) return (1);	
664
665	/* 1. Search for type specific commands FIRST - Allows overriding of a general command */
666
667	if (current_type != NULL)
668		for (i=0;i&#60;=current_type-&#62;type_commands.last_command &#38;&#38; !found;i++) {
669			if (strcmp (command,current_type-&#62;type_commands.names [i])==0) {
670				(*current_type-&#62;type_commands.callback [i]) (command_line);
671				found=1;
672			}
673		}
674
675	/* 2. Now search for ext2 filesystem general commands */
676
677	if (!found)
678		for (i=0;i&#60;=ext2_commands.last_command &#38;&#38; !found;i++) {
679			if (strcmp (command,ext2_commands.names [i])==0) {
680				(*ext2_commands.callback [i]) (command_line);
681				found=1;
682			}
683		}
684
685	
686	/* 3. If not found, search the general commands */
687	
688	if (!found)
689		for (i=0;i&#60;=general_commands.last_command &#38;&#38; !found;i++) {
690			if (strcmp (command,general_commands.names [i])==0) {
691				(*general_commands.callback [i]) (command_line);
692				found=1;
693			}
694		}
695
696	if (!found) {
697		wprintw (command_win,"Error: Unknown command\n");
698		refresh_command_win ();
699	}
700	
701	return (0);
702}
703</ProgramListing>
704
705</Para>
706
707</Sect1>
708
709<Sect1>
710<Title>Source files in EXT2ED</Title>
711
712<Para>
713The project was getting large enough to be splitted into several source
714files. I splitted the source as much as I could into self-contained
715source files. The source files consist of the following blocks:
716
717<ItemizedList>
718<ListItem>
719
720<Para>
721	<Literal remap="tt">Main include file - ext2ed.h</Literal>
722
723This file contains the definitions of the various structures,
724variables and functions used in EXT2ED. It is included by all source
725files in EXT2ED.
726
727</Para>
728</ListItem>
729<ListItem>
730
731<Para>
732	<Literal remap="tt">Main block - main.c</Literal>
733
734<Literal remap="tt">main.c</Literal> handles the upper level of the program flow control.
735It contains the <Literal remap="tt">parser</Literal> and the <Literal remap="tt">dispatcher</Literal>. Its task is
736to ask the user for a required action, and to pass control to other
737lower level functions in order to do the actual job.
738
739</Para>
740</ListItem>
741<ListItem>
742
743<Para>
744	<Literal remap="tt">Initialization - init.c</Literal>
745
746The init source is responsible for the various initialization
747actions which need to be done through the program. For example,
748auto detection of an ext2 filesystem when selecting a device and
749initialization of the filesystem-specific structures described
750earlier.
751
752</Para>
753</ListItem>
754<ListItem>
755
756<Para>
757	<Literal remap="tt">Disk activity - disk.c</Literal>
758
759<Literal remap="tt">disk.c</Literal> is handles the lower level interaction with the
760device. All disk activity is passed through this file - The various
761functions through the source code request disk actions from the
762functions in this file. In this way, for example, we can easily block
763the write access to the device.
764
765</Para>
766</ListItem>
767<ListItem>
768
769<Para>
770	<Literal remap="tt">Display output activity - win.c</Literal>
771	
772In a similar way to <Literal remap="tt">disk.c</Literal>, the user-interface functions and
773most of the interaction with the <Literal remap="tt">ncurses library</Literal> are done
774here. Nothing will be actually written to a specific window without
775calling a function from this file.
776
777</Para>
778</ListItem>
779<ListItem>
780
781<Para>
782	<Literal remap="tt">Commands available through dispatching - *&lowbar;com.c </Literal>
783
784The above file name is generic - Each file which ends with
785<Literal remap="tt">&lowbar;com.c</Literal> contains a group of related commands which can be
786called through <Literal remap="tt">the dispatching function</Literal>.
787
788Each object typically has its own file. A separate file is also
789available for the general commands.
790</Para>
791</ListItem>
792
793</ItemizedList>
794
795The entire list of source files available at this time is:
796
797<ItemizedList>
798<ListItem>
799
800<Para>
801	blockbitmap&lowbar;com.c
802</Para>
803</ListItem>
804<ListItem>
805
806<Para>
807	dir&lowbar;com.c
808</Para>
809</ListItem>
810<ListItem>
811
812<Para>
813	disk.c
814</Para>
815</ListItem>
816<ListItem>
817
818<Para>
819	ext2&lowbar;com.c
820</Para>
821</ListItem>
822<ListItem>
823
824<Para>
825	file&lowbar;com.c
826</Para>
827</ListItem>
828<ListItem>
829
830<Para>
831	general&lowbar;com.c
832</Para>
833</ListItem>
834<ListItem>
835
836<Para>
837	group&lowbar;com.c
838</Para>
839</ListItem>
840<ListItem>
841
842<Para>
843	init.c
844</Para>
845</ListItem>
846<ListItem>
847
848<Para>
849	inode&lowbar;com.c
850</Para>
851</ListItem>
852<ListItem>
853
854<Para>
855	inodebitmap&lowbar;com.c
856</Para>
857</ListItem>
858<ListItem>
859
860<Para>
861	main.c
862</Para>
863</ListItem>
864<ListItem>
865
866<Para>
867	super&lowbar;com.c
868</Para>
869</ListItem>
870<ListItem>
871
872<Para>
873	win.c
874</Para>
875</ListItem>
876
877</ItemizedList>
878
879</Para>
880
881</Sect1>
882
883<Sect1>
884<Title>User interface</Title>
885
886<Para>
887The user interface is text-based only and is based on the following
888libraries:
889</Para>
890
891<Para>
892
893<ItemizedList>
894<ListItem>
895
896<Para>
897	The <Literal remap="tt">ncurses</Literal> library, developed by <Literal remap="tt">Zeyd Ben-Halim</Literal>.
898</Para>
899</ListItem>
900<ListItem>
901
902<Para>
903	The <Literal remap="tt">GNU readline</Literal> library.
904</Para>
905</ListItem>
906
907</ItemizedList>
908
909</Para>
910
911<Para>
912The user interaction is command line based - The user enters a command
913line, which consists of a <Literal remap="tt">command</Literal> and of <Literal remap="tt">arguments</Literal>. This fits
914nicely with the program flow control described earlier - The <Literal remap="tt">command</Literal>
915is used by <Literal remap="tt">dispatch</Literal> to select the right function, and the
916<Literal remap="tt">arguments</Literal> are interpreted by the function itself.
917</Para>
918
919<Sect2>
920<Title>The ncurses library</Title>
921
922<Para>
923The <Literal remap="tt">ncurses</Literal> library enables me to divide the screen into "windows".
924The main advantage is that I treat the "window" in a virtual way, asking
925the ncurses library to "write to a window". However, the ncurses
926library internally buffers the requests, and nothing is actually passed to the
927terminal until an explicit refresh is requested. When the refresh request is
928made, ncurses compares the current terminal state (as known in the last time
929that a refresh was done) with the new to be shown state, and passes to the
930terminal the minimal information required to update the display. As a
931result, the display output is optimized behind the scenes by the
932<Literal remap="tt">ncurses</Literal> library, while I can still treat it in a virtual way.
933</Para>
934
935<Para>
936There are two basic concepts in the <Literal remap="tt">ncurses</Literal> library:
937
938<ItemizedList>
939<ListItem>
940
941<Para>
942	A window.
943</Para>
944</ListItem>
945<ListItem>
946
947<Para>
948	A pad.
949</Para>
950</ListItem>
951
952</ItemizedList>
953
954A window can be no bigger than the actual terminal size. A pad, however, is
955not limited in its size.
956</Para>
957
958<Para>
959The user screen is divided by EXT2ED into three windows and one pad:
960
961<ItemizedList>
962<ListItem>
963
964<Para>
965	Title window.
966</Para>
967</ListItem>
968<ListItem>
969
970<Para>
971	Status window.
972</Para>
973</ListItem>
974<ListItem>
975
976<Para>
977	Main display pad.
978</Para>
979</ListItem>
980<ListItem>
981
982<Para>
983	Command window.
984</Para>
985</ListItem>
986
987</ItemizedList>
988
989</Para>
990
991<Para>
992The <Literal remap="tt">title window</Literal> is static - It just displays the current version
993of EXT2ED.
994</Para>
995
996<Para>
997The user interaction is done in the <Literal remap="tt">command window</Literal>. The user enters a
998<Literal remap="tt">command line</Literal>, feedback is usually displayed there, and then relevant
999data is usually displayed in the main display and in the status window.
1000</Para>
1001
1002<Para>
1003The <Literal remap="tt">main display</Literal> is using a <Literal remap="tt">pad</Literal> instead of a window because
1004the amount of information which is written to it is not known in advance.
1005Therefor, the user treats the main display as a "window" into a bigger
1006display and can <Literal remap="tt">scroll vertically</Literal> using the <Literal remap="tt">pgdn</Literal> and <Literal remap="tt">pgup</Literal>
1007commands. Although the <Literal remap="tt">pad</Literal> mechanism enables me to use horizontal
1008scrolling, I have not utilized this.
1009</Para>
1010
1011<Para>
1012When I need to show something to the user, I use the ncurses <Literal remap="tt">wprintw</Literal>
1013command. Then an explicit refresh command is required. As explained before,
1014the refresh commands is piped through <Literal remap="tt">win.c</Literal>. For example, to update
1015the command window, <Literal remap="tt">refresh&lowbar;command&lowbar;win ()</Literal> is used.
1016</Para>
1017
1018</Sect2>
1019
1020<Sect2>
1021<Title>The readline library</Title>
1022
1023<Para>
1024Avner suggested me to integrate the GNU <Literal remap="tt">readline</Literal> library in my project.
1025The <Literal remap="tt">readline</Literal> library is designed specifically for programs which use
1026command line interface. It provides a nice package of <Literal remap="tt">command line editing
1027tools</Literal> - Inserting, deleting words, and the whole package of editing tools
1028which are normally available in the <Literal remap="tt">bash</Literal> shell (Refer to the readline
1029documentation for details). In addition, I utilized the <Literal remap="tt">history</Literal>
1030feature of the readline library - The entered commands are saved in a
1031<Literal remap="tt">command history</Literal>, and can be called later by whatever means that the
1032readline package provides. Command completion is also supported - When the
1033user enters a partial command name, EXT2ED will provide the readline library
1034with the possible completions.
1035</Para>
1036
1037</Sect2>
1038
1039</Sect1>
1040
1041<Sect1>
1042<Title>Possible support of other filesystems</Title>
1043
1044<Para>
1045The entire ext2 layer is provided through specific objects. Given another
1046set of objects, support of other filesystem can be provided using the same
1047dispatching mechanism. In order to prepare the surface for this option, I
1048added yet another layer to the two-layer structure presented earlier. EXT2ED
1049commands now consist of three layers:
1050
1051<ItemizedList>
1052<ListItem>
1053
1054<Para>
1055	The general commands.
1056</Para>
1057</ListItem>
1058<ListItem>
1059
1060<Para>
1061	The ext2 general commands.
1062</Para>
1063</ListItem>
1064<ListItem>
1065
1066<Para>
1067	The ext2 object specific commands.
1068</Para>
1069</ListItem>
1070
1071</ItemizedList>
1072
1073The general commands are provided by the <Literal remap="tt">general&lowbar;com.c</Literal> source file,
1074and are always available. The two other levels are not present when EXT2ED
1075loads - They are dynamically added by <Literal remap="tt">init.c</Literal> when EXT2ED detects an
1076ext2 filesystem on the device.
1077</Para>
1078
1079<Para>
1080The abstraction levels presented above helps to extend EXT2ED to fully
1081support a new filesystem, with its own specific type commands. 
1082</Para>
1083
1084<Para>
1085Even without any source code modification, the user is free to add structure
1086definitions in a separate file (specified in the configuration file), 
1087which will be added to the list of available objects. The added objects will
1088consist only of variables, of-course, and will be used through the more
1089primitive <Literal remap="tt">setoffset</Literal> and <Literal remap="tt">settype</Literal> commands.
1090</Para>
1091
1092</Sect1>
1093
1094<Sect1>
1095<Title>On the implementation of the various commands</Title>
1096
1097<Para>
1098This section points out some typical programming style that I used in many
1099places at the code.
1100</Para>
1101
1102<Sect2>
1103<Title>The explicit use of the dispatch function</Title>
1104
1105<Para>
1106The various commands are reached by the user through the <Literal remap="tt">dispatch</Literal>
1107function. This is not surprising. The fact that can be surprising, at least in
1108a first look, is that <Literal remap="tt">you'll find the dispatch call in many of my
1109own functions!</Literal>.
1110</Para>
1111
1112<Para>
1113I am in fact using my own implemented functions to construct higher
1114level operations. I am heavily using the fact that the dispatching mechanism
1115is object oriented ant that the <Literal remap="tt">overriding</Literal> principle takes place and
1116selects the proper function to call when several commands with the same name
1117are accessible.
1118</Para>
1119
1120<Para>
1121Sometimes, however, I call the explicit command directly, without passing
1122through <Literal remap="tt">dispatch</Literal>. This is typically done when I want to bypass the
1123<Literal remap="tt">overriding</Literal> effect.
1124</Para>
1125
1126<Para>
1127
1128This is used, for example, in the interaction between the global cd command
1129and the dir object specific cd command. You will see there that in order
1130to implement the "entire" cd command, the type specific cd command uses both
1131a dispatching mechanism to call itself recursively if a relative path is
1132used, or a direct call of the general cd handling function if an explicit path
1133is used.
1134
1135</Para>
1136
1137</Sect2>
1138
1139<Sect2>
1140<Title>Passing information between handling functions</Title>
1141
1142<Para>
1143Typically, every source code file which handles one object type has a global
1144structure specifically designed for it which is used by most of the
1145functions in that file. This is used to pass information between the various
1146functions there, and to physically provide the link to other related
1147objects, typically for initialization use.
1148</Para>
1149
1150<Para>
1151
1152For example, in order to edit a file, information about the
1153inode is needed - The file command is available only when editing an
1154inode. When the file command is issued, the handling function (found,
1155according to the source division outlined above, in inode_com.c) will
1156store the necessary information about the inode in a specific structure
1157of type struct_file_info which will be available for use by the file_com.c
1158functions. Only then it will set the type to file. This is also the reason
1159that a direct asynchronic set of the object type to a file through a settype
1160command will fail - The above data structure will not be initialized
1161properly because the user never was at the inode of the file. 
1162
1163</Para>
1164
1165</Sect2>
1166
1167<Sect2>
1168<Title>A very simplified overview of a typical command handling function</Title>
1169
1170<Para>
1171This is a very simplified overview. Detailed information will follow
1172where appropriate.
1173</Para>
1174
1175<Sect3>
1176<Title>The prototype of a typical handling function</Title>
1177
1178<Para>
1179
1180<OrderedList>
1181<ListItem>
1182
1183<Para>
1184 	I chose a unified <Literal remap="tt">naming convention</Literal> for the various object
1185specific commands. It is perhaps best showed with an example:
1186
1187The prototype of the handling function of the command <Literal remap="tt">next</Literal> of
1188the type <Literal remap="tt">file</Literal> is:
1189
1190<Screen>
1191		extern void type_file___next (char *command_line);
1192	
1193</Screen>
1194
1195
1196For other types and commands, the words <Literal remap="tt">file</Literal> and <Literal remap="tt">next</Literal>
1197should 	be replaced accordingly.
1198
1199</Para>
1200</ListItem>
1201<ListItem>
1202
1203<Para>
1204	The ext2 general commands syntax is similar. For example, the ext2
1205general command <Literal remap="tt">super</Literal> results in calling:
1206
1207<Screen>
1208		extern void type_ext2___super (char *command_line);
1209	
1210</Screen>
1211
1212Those functions are available in <Literal remap="tt">ext2&lowbar;com.c</Literal>.
1213</Para>
1214</ListItem>
1215<ListItem>
1216
1217<Para>
1218	The general commands syntax is even simpler - The name of the
1219handling function is exactly the name of the commands. Those
1220functions are available in <Literal remap="tt">general&lowbar;com.c</Literal>.
1221</Para>
1222</ListItem>
1223
1224</OrderedList>
1225
1226</Para>
1227
1228</Sect3>
1229
1230<Sect3>
1231<Title>"Typical" algorithm</Title>
1232
1233<Para>
1234This section can't of-course provide meaningful information - Each
1235command is handled differently, but the following frame is typical:
1236
1237<OrderedList>
1238<ListItem>
1239
1240<Para>
1241	Parse command line arguments and analyze them. Return with an error
1242message if the syntax is wrong.
1243</Para>
1244</ListItem>
1245<ListItem>
1246
1247<Para>
1248	"Act accordingly", perhaps making use of the global variable available
1249to this type.
1250</Para>
1251</ListItem>
1252<ListItem>
1253
1254<Para>
1255	Use some <Literal remap="tt">dispatch / direct </Literal> calls in order to pass control to
1256other lower-level user commands.
1257</Para>
1258</ListItem>
1259<ListItem>
1260
1261<Para>
1262	Sometimes <Literal remap="tt">dispatch</Literal> to the object's <Literal remap="tt">show</Literal> command to
1263display the resulting data to the user.
1264</Para>
1265</ListItem>
1266
1267</OrderedList>
1268
1269I told you it is meaningless :-)
1270</Para>
1271
1272</Sect3>
1273
1274</Sect2>
1275
1276</Sect1>
1277
1278<Sect1>
1279<Title>Initialization overview</Title>
1280
1281<Para>
1282In this section I will discuss some aspects of the various initialization
1283routines available in the source file <Literal remap="tt">init.c</Literal>.
1284</Para>
1285
1286<Sect2>
1287<Title>Upon startup</Title>
1288
1289<Para>
1290Follows the function <Literal remap="tt">main</Literal>, appearing of-course in <Literal remap="tt">main.c</Literal>:
1291
1292
1293<ProgramListing>
1294int main (void)
1295
1296{
1297	if (!init ()) return (0);	/* Perform some initial initialization */
1298					/* Quit if failed */
1299
1300	parser ();			/* Get and parse user commands */
1301	
1302	prepare_to_close ();		/* Do some cleanup */
1303	printf ("Quitting ...\n");
1304	return (1);			/* And quit */
1305}
1306</ProgramListing>
1307
1308</Para>
1309
1310<Para>
1311The two initialization functions, which are called by <Literal remap="tt">main</Literal>, are:
1312
1313<ItemizedList>
1314<ListItem>
1315
1316<Para>
1317	init
1318</Para>
1319</ListItem>
1320<ListItem>
1321
1322<Para>
1323	prepare&lowbar;to&lowbar;close
1324</Para>
1325</ListItem>
1326
1327</ItemizedList>
1328
1329</Para>
1330
1331<Sect3>
1332<Title>The init function</Title>
1333
1334<Para>
1335<Literal remap="tt">init</Literal> is called from <Literal remap="tt">main</Literal> upon startup. It initializes the
1336following tasks / subsystems:
1337
1338<OrderedList>
1339<ListItem>
1340
1341<Para>
1342	Processing of the <Literal remap="tt">user configuration file</Literal>, by using the
1343<Literal remap="tt">process&lowbar;configuration&lowbar;file</Literal> function. Failing to complete the
1344configuration file processing is considered a <Literal remap="tt">fatal error</Literal>,
1345and EXT2ED is aborted. I did it this way because the configuration
1346file has some sensitive user options like write access behavior, and
1347I wanted to be sure that the user is aware of them.
1348</Para>
1349</ListItem>
1350<ListItem>
1351
1352<Para>
1353	Registration of the <Literal remap="tt">general commands</Literal> through the use of
1354the <Literal remap="tt">add&lowbar;general&lowbar;commands</Literal> function.
1355</Para>
1356</ListItem>
1357<ListItem>
1358
1359<Para>
1360	Reset of the object memory rotating lifo structure.
1361</Para>
1362</ListItem>
1363<ListItem>
1364
1365<Para>
1366	Reset of the device parameters and of the current type.
1367</Para>
1368</ListItem>
1369<ListItem>
1370
1371<Para>
1372	Initialization of the windows subsystem - The interface between the
1373ncurses library and EXT2ED, through the use of the <Literal remap="tt">init&lowbar;windows</Literal>
1374function, available in <Literal remap="tt">win.c</Literal>.
1375</Para>
1376</ListItem>
1377<ListItem>
1378
1379<Para>
1380	Initialization of the interface between the readline library and
1381EXT2ED, through <Literal remap="tt">init&lowbar;readline</Literal>.
1382</Para>
1383</ListItem>
1384<ListItem>
1385
1386<Para>
1387	Initialization of the <Literal remap="tt">signals</Literal> subsystem, through
1388<Literal remap="tt">init&lowbar;signals</Literal>.
1389</Para>
1390</ListItem>
1391<ListItem>
1392
1393<Para>
1394	Disabling write access. Write access needs to be explicitly enabled
1395using a user command, to prevent accidental user mistakes.
1396</Para>
1397</ListItem>
1398
1399</OrderedList>
1400
1401When <Literal remap="tt">init</Literal> is finished, it dispatches the <Literal remap="tt">help</Literal> command in order
1402to show the available commands to the user. Note that the ext2 layer is still
1403not added; It will be added if and when EXT2ED will detect an ext2
1404filesystem on a device.
1405</Para>
1406
1407</Sect3>
1408
1409<Sect3>
1410<Title>The prepare&lowbar;to&lowbar;close function</Title>
1411
1412<Para>
1413The <Literal remap="tt">prepare&lowbar;to&lowbar;close</Literal> function reverses some of the actions done
1414earlier in EXT2ED and freeing the dynamically allocated memory.
1415Specifically, it:
1416
1417<OrderedList>
1418<ListItem>
1419
1420<Para>
1421	Closes the open device, if any.
1422</Para>
1423</ListItem>
1424<ListItem>
1425
1426<Para>
1427	Removes the first level - Removing the general commands, through
1428the use of <Literal remap="tt">free&lowbar;user&lowbar;commands</Literal>, with a pointer to the
1429general&lowbar;commands structure as a parameter.
1430</Para>
1431</ListItem>
1432<ListItem>
1433
1434<Para>
1435	Removes of the second level - Removing the ext2 ext2 general
1436commands, in much the same way.
1437</Para>
1438</ListItem>
1439<ListItem>
1440
1441<Para>
1442	Removes of the third level - Removing the objects and the object
1443specific commands, by using <Literal remap="tt">free&lowbar;struct&lowbar;descriptors</Literal>.
1444</Para>
1445</ListItem>
1446<ListItem>
1447
1448<Para>
1449	Closes the window subsystem, and deattaches EXT2ED from the ncurses
1450library, through the use of the <Literal remap="tt">close&lowbar;windows</Literal> function,
1451available in <Literal remap="tt">win.c</Literal>.
1452</Para>
1453</ListItem>
1454
1455</OrderedList>
1456
1457</Para>
1458
1459</Sect3>
1460
1461</Sect2>
1462
1463<Sect2>
1464<Title>Registration of commands</Title>
1465
1466<Para>
1467Addition of a user command is done through the <Literal remap="tt">add&lowbar;user&lowbar;command</Literal>
1468function. The prototype is:
1469
1470<Screen>
1471void add_user_command (struct struct_commands *ptr,char *name,char
1472*description,PF callback);
1473</Screen>
1474
1475The function receives a pointer to a structure of type
1476<Literal remap="tt">struct&lowbar;commands</Literal>, a desired name for the command which will be used by
1477the user to identify the command, a short description which is utilized by the
1478<Literal remap="tt">help</Literal> subsystem, and a pointer to a C function which will be called if
1479<Literal remap="tt">dispatch</Literal> decides that this command was requested.
1480</Para>
1481
1482<Para>
1483The <Literal remap="tt">add&lowbar;user&lowbar;command</Literal> is a <Literal remap="tt">low level function</Literal> used in the three
1484levels to add user commands. For example, addition of the <Literal remap="tt">ext2
1485general commands is done by:</Literal>
1486
1487<ProgramListing>
1488void add_ext2_general_commands (void)
1489
1490{
1491	add_user_command (&amp;ext2_commands,"super","Moves to the superblock of the filesystem",type_ext2___super);
1492	add_user_command (&amp;ext2_commands,"group","Moves to the first group descriptor",type_ext2___group);
1493	add_user_command (&amp;ext2_commands,"cd","Moves to the directory specified",type_ext2___cd);
1494}
1495</ProgramListing>
1496
1497</Para>
1498
1499</Sect2>
1500
1501<Sect2>
1502<Title>Registration of objects</Title>
1503
1504<Para>
1505Registration of objects is based, as explained earlier, on the "compilation"
1506of an external user file, which has a syntax similar to the C language
1507<Literal remap="tt">struct</Literal> keyword. The primitive parser I have implemented detects the
1508definition of structures, and calls some lower level functions to actually
1509register the new detected object. The parser's prototype is:
1510
1511<Screen>
1512int set_struct_descriptors (char *file_name)
1513</Screen>
1514
1515It opens the given file name, and calls, when appropriate:
1516
1517<ItemizedList>
1518<ListItem>
1519
1520<Para>
1521	add&lowbar;new&lowbar;descriptor
1522</Para>
1523</ListItem>
1524<ListItem>
1525
1526<Para>
1527	add&lowbar;new&lowbar;variable
1528</Para>
1529</ListItem>
1530
1531</ItemizedList>
1532
1533<Literal remap="tt">add&lowbar;new&lowbar;descriptor</Literal> is a low level function which adds a new descriptor
1534to the doubly linked list of the available objects. It will then call
1535<Literal remap="tt">fill&lowbar;type&lowbar;commands</Literal>, which will add specific commands to the object,
1536if the object is known.
1537</Para>
1538
1539<Para>
1540<Literal remap="tt">add&lowbar;new&lowbar;variable</Literal> will add a new variable of the requested length to the
1541specified descriptor.
1542</Para>
1543
1544</Sect2>
1545
1546<Sect2>
1547<Title>Initialization upon specification of a device</Title>
1548
1549<Para>
1550When the general command <Literal remap="tt">setdevice</Literal> is used to open a device, some
1551initialization sequence takes place, which is intended to determine two
1552factors:
1553
1554<ItemizedList>
1555<ListItem>
1556
1557<Para>
1558	Are we dealing with an ext2 filesystem ?
1559</Para>
1560</ListItem>
1561<ListItem>
1562
1563<Para>
1564	What are the basic filesystem parameters, such as its total size and
1565its block size ?
1566</Para>
1567</ListItem>
1568
1569</ItemizedList>
1570
1571This questions are answered by the <Literal remap="tt">set&lowbar;file&lowbar;system&lowbar;info</Literal>, possibly
1572using some <Literal remap="tt">help from the user</Literal>, through the configuration file.
1573The answers are placed in the <Literal remap="tt">file&lowbar;system&lowbar;info</Literal> structure, which is of
1574type <Literal remap="tt">struct&lowbar;file&lowbar;system&lowbar;info</Literal>:
1575
1576<ProgramListing>
1577struct struct_file_system_info {
1578	unsigned long file_system_size;
1579	unsigned long super_block_offset;
1580	unsigned long first_group_desc_offset;
1581	unsigned long groups_count;
1582	unsigned long inodes_per_block;
1583	unsigned long blocks_per_group;		/* The name is misleading; beware */
1584	unsigned long no_blocks_in_group;
1585	unsigned short block_size;
1586	struct ext2_super_block super_block;
1587};
1588</ProgramListing>
1589
1590</Para>
1591
1592<Para>
1593Autodetection of an ext2 filesystem is usually recommended. However, on a damaged
1594filesystem I can't assure a success. That's were the user comes in - He can
1595<Literal remap="tt">override</Literal> the auto detection procedure and force an ext2 filesystem, by
1596selecting the proper options in the configuration file.
1597</Para>
1598
1599<Para>
1600If auto detection succeeds, the second question above is automatically
1601answered - I get all the information I need from the filesystem itself. In
1602any case, default parameters can be supplied in the configuration file and
1603the user can select the required behavior.
1604</Para>
1605
1606<Para>
1607If we decide to treat the filesystem as an ext2 filesystem, <Literal remap="tt">registration of
1608the ext2 specific objects</Literal> is done at this point, by calling the
1609<Literal remap="tt">set&lowbar;struct&lowbar;descriptors</Literal> outlined earlier, with the name of the file
1610which describes the ext2 objects, and is basically based on the ext2 sources
1611main include file. At this point, EXT2ED can be fully used by the user.
1612</Para>
1613
1614<Para>
1615If we do not register the ext2 specific objects, the user can still provide
1616object definitions in a separate file, and will be able to use EXT2ED in a
1617<Literal remap="tt">limited form</Literal>, but more sophisticated than a simple hex editor.
1618</Para>
1619
1620</Sect2>
1621
1622</Sect1>
1623
1624<Sect1>
1625<Title>main.c</Title>
1626
1627<Para>
1628As described earlier, <Literal remap="tt">main.c</Literal> is used as a front-head to the entire
1629program. <Literal remap="tt">main.c</Literal> contains the following elements:
1630</Para>
1631
1632<Sect2>
1633<Title>The main routine</Title>
1634
1635<Para>
1636The <Literal remap="tt">main</Literal> routine was displayed above. Its task is to pass control to
1637the initialization routines and to the parser.
1638</Para>
1639
1640</Sect2>
1641
1642<Sect2>
1643<Title>The parser</Title>
1644
1645<Para>
1646The parser consists of the following functions:
1647
1648<ItemizedList>
1649<ListItem>
1650
1651<Para>
1652	The <Literal remap="tt">parser</Literal> function, which reads the command line from the
1653user and saves it in readline's history buffer and in the internal
1654last-command buffer.
1655</Para>
1656</ListItem>
1657<ListItem>
1658
1659<Para>
1660	The <Literal remap="tt">parse&lowbar;word</Literal> function, which receives a string and parses
1661the first word from it, ignoring whitespaces, and returns a pointer
1662to the rest of the string.
1663</Para>
1664</ListItem>
1665<ListItem>
1666
1667<Para>
1668	The <Literal remap="tt">complete&lowbar;command</Literal> function, which is used by the readline
1669library for command completion. It scans the available commands at
1670this point and determines the possible completions.
1671</Para>
1672</ListItem>
1673
1674</ItemizedList>
1675
1676</Para>
1677
1678</Sect2>
1679
1680<Sect2>
1681<Title>The dispatcher</Title>
1682
1683<Para>
1684The dispatcher was already explained in the flow control section - section
1685<XRef LinkEnd="flow-control">. Its task is to pass control to the proper command
1686handling function, based on the command line's command.
1687</Para>
1688
1689</Sect2>
1690
1691<Sect2>
1692<Title>The self-sanity control</Title>
1693
1694<Para>
1695This is not fully implemented.
1696</Para>
1697
1698<Para>
1699The general idea was to provide a control system which will supervise the
1700internal work of EXT2ED. Since I am pretty sure that bugs exist, I have
1701double checked myself in a few instances, and issued an <Literal remap="tt">internal
1702error</Literal> warning if I reached the conclusion that something is not logical.
1703The internal error is reported by the function <Literal remap="tt">internal&lowbar;error</Literal>,
1704available in <Literal remap="tt">main.c</Literal>. 
1705</Para>
1706
1707<Para>
1708The self sanity check is compiled only if the compile time option
1709<Literal remap="tt">DEBUG</Literal> is selected.
1710</Para>
1711
1712</Sect2>
1713
1714</Sect1>
1715
1716<Sect1>
1717<Title>The windows interface</Title>
1718
1719<Para>
1720Screen handling and interfacing to the <Literal remap="tt">ncurses</Literal> library is done in
1721<Literal remap="tt">win.c</Literal>.
1722</Para>
1723
1724<Sect2>
1725<Title>Initialization</Title>
1726
1727<Para>
1728Opening of the windows is done in <Literal remap="tt">init&lowbar;windows</Literal>. In
1729<Literal remap="tt">close&lowbar;windows</Literal>, we just close our windows. The various window lengths
1730with an exception to the <Literal remap="tt">show pad</Literal> are defined in the main header file.
1731The rest of the display will be used by the <Literal remap="tt">show pad</Literal>.
1732</Para>
1733
1734</Sect2>
1735
1736<Sect2>
1737<Title>Display output</Title>
1738
1739<Para>
1740Each actual refreshing of the terminal monitor is done by using the
1741appropriate refresh function from this file: <Literal remap="tt">refresh&lowbar;title&lowbar;win</Literal>,
1742<Literal remap="tt">refresh&lowbar;show&lowbar;win</Literal>, <Literal remap="tt">refresh&lowbar;show&lowbar;pad</Literal> and
1743<Literal remap="tt">refresh&lowbar;command&lowbar;win</Literal>.
1744</Para>
1745
1746<Para>
1747With the exception of the <Literal remap="tt">show pad</Literal>, each function simply calls the
1748<Literal remap="tt">ncurses refresh command</Literal>. In order to provide to <Literal remap="tt">scrolling</Literal> in
1749the <Literal remap="tt">show pad</Literal>, some information about its status is constantly updated
1750by the various functions which display output in it. <Literal remap="tt">refresh&lowbar;show&lowbar;pad</Literal>
1751passes this information to <Literal remap="tt">ncurses</Literal> so that the correct part of the pad
1752is actually copied to the display.
1753</Para>
1754
1755<Para>
1756The above information is saved in a global variable of type <Literal remap="tt">struct
1757struct&lowbar;pad&lowbar;info</Literal>:
1758</Para>
1759
1760<Para>
1761
1762<ProgramListing>
1763struct struct_pad_info {
1764	int display_lines,display_cols;
1765	int line,col;
1766	int max_line,max_col;
1767	int disable_output;
1768};
1769</ProgramListing>
1770
1771</Para>
1772
1773</Sect2>
1774
1775<Sect2>
1776<Title>Screen redraw</Title>
1777
1778<Para>
1779The <Literal remap="tt">redraw&lowbar;all</Literal> function will just reopen the windows. This action is
1780necessary if the display gets garbled from some reason.
1781</Para>
1782
1783</Sect2>
1784
1785</Sect1>
1786
1787<Sect1>
1788<Title>The disk interface</Title>
1789
1790<Para>
1791All the disk activity with regard to the filesystem passes through the file
1792<Literal remap="tt">disk.c</Literal>. This is done that way to provide additional levels of safety
1793concerning the disk access. This way, global decisions considering the disk
1794can be easily accomplished. The benefits of this isolation will become even
1795clearer in the next sections.
1796</Para>
1797
1798<Sect2>
1799<Title>Low level functions</Title>
1800
1801<Para>
1802Read requests are ultimately handled by <Literal remap="tt">low&lowbar;read</Literal> and write requests
1803are handled by <Literal remap="tt">low&lowbar;write</Literal>. They just receive the length of the data
1804block, the offset in the filesystem and a pointer to the buffer and pass the
1805request to the <Literal remap="tt">fread</Literal> or <Literal remap="tt">fwrite</Literal> standard library functions.
1806</Para>
1807
1808</Sect2>
1809
1810<Sect2>
1811<Title>Mounted filesystems</Title>
1812
1813<Para>
1814EXT2ED design assumes that the edited filesystem is not mounted. Even if
1815a <Literal remap="tt">reasonably simple</Literal> way to handle mounted filesystems exists, it is
1816probably <Literal remap="tt">too complicated</Literal> :-)
1817</Para>
1818
1819<Para>
1820Write access to a mounted filesystem will be denied. Read access can be
1821allowed by using a configuration file option. The mount status is determined
1822by reading the file /etc/mtab.
1823</Para>
1824
1825</Sect2>
1826
1827<Sect2>
1828<Title>Write access</Title>
1829
1830<Para>
1831Write access is the most sensitive part in the program. This program is
1832intended for <Literal remap="tt">editing filesystems</Literal>. It is obvious that a small mistake
1833in this regard can make the filesystem not usable anymore.
1834</Para>
1835
1836<Para>
1837The following safety measures are added, of-course, to the general Unix
1838permission protection - The user can always disable write access on the
1839device file itself.
1840</Para>
1841
1842<Para>
1843Considering the user, the following safety measures were taken:
1844
1845<OrderedList>
1846<ListItem>
1847
1848<Para>
1849	The filesystem is <Literal remap="tt">never</Literal> opened with write-access enables.
1850Rather, the user must explicitly request to enable write-access.
1851</Para>
1852</ListItem>
1853<ListItem>
1854
1855<Para>
1856	The user can <Literal remap="tt">disable</Literal> write access entirely by using a
1857<Literal remap="tt">configuration file option</Literal>.
1858</Para>
1859</ListItem>
1860<ListItem>
1861
1862<Para>
1863	Changes are never done automatically - Whenever the user makes
1864changes, they are done in memory. An explicit <Literal remap="tt">writedata</Literal>
1865command should be issued to make the changes active in the disk.
1866</Para>
1867</ListItem>
1868
1869</OrderedList>
1870
1871Considering myself, I tried to protect against my bugs by:
1872
1873<ItemizedList>
1874<ListItem>
1875
1876<Para>
1877	Opening the device in read-only mode until a write request is
1878issued by the user.
1879</Para>
1880</ListItem>
1881<ListItem>
1882
1883<Para>
1884	Limiting <Literal remap="tt">actual</Literal> filesystem access to two functions only -
1885<Literal remap="tt">low&lowbar;read</Literal> for reading, and <Literal remap="tt">low&lowbar;write</Literal> for writing. Those
1886functions were programmed carefully, and I added the self
1887sanity checks there. In addition, this is the only place in which I
1888need to check the user options described above - There can be no
1889place in which I can "forget" to check them.
1890
1891Note that The disabling of write-access through the configuration file
1892is double checked here only as a <Literal remap="tt">self-sanity</Literal> check - If
1893<Literal remap="tt">DEBUG</Literal> is selected, since write enable should have been refused
1894and write-access is always disabled at startup, hence finding
1895<Literal remap="tt">here</Literal> that the user has write access disabled through the
1896configuration file clearly indicates that I have a bug somewhere.
1897</Para>
1898</ListItem>
1899
1900</ItemizedList>
1901
1902</Para>
1903
1904<Para>
1905The following safety measure can provide protection against <Literal remap="tt">both</Literal> user
1906mistakes and my own bugs:
1907
1908<ItemizedList>
1909<ListItem>
1910
1911<Para>
1912	I added a <Literal remap="tt">logging option</Literal>, which logs every actual write
1913access to the disk in the lowest level - In <Literal remap="tt">low&lowbar;write</Literal> itself.
1914
1915The logging has nothing to do with the current type and the various
1916other higher level operations of EXT2ED - It is simply a hex dump of
1917the contents which will be overwritten; Both the original contents
1918and the new written data.
1919
1920In that case, even if the user makes a 	mistake, the original data
1921can be retrieved.
1922
1923Even If I have a bug somewhere which causes incorrect data to be
1924written to the disk, the logging option will still log exactly the
1925original contents at the place were data was incorrectly overwritten.
1926(This assumes, of-course, that <Literal remap="tt">low-write</Literal> and the <Literal remap="tt">logging
1927itself</Literal> work correctly. I have done my best to verify that this is
1928indeed the case).
1929
1930The <Literal remap="tt">logging</Literal> option is implemented in the <Literal remap="tt">log&lowbar;changes</Literal>
1931function.
1932</Para>
1933</ListItem>
1934
1935</ItemizedList>
1936
1937</Para>
1938
1939</Sect2>
1940
1941<Sect2>
1942<Title>Reading / Writing objects</Title>
1943
1944<Para>
1945Usually <Literal remap="tt">(not always)</Literal>, the current object data is available in the
1946global variable <Literal remap="tt">type&lowbar;data</Literal>, which is of the type:
1947
1948<ProgramListing>
1949struct struct_type_data {
1950	long offset_in_block;
1951
1952	union union_type_data {
1953		char buffer [EXT2_MAX_BLOCK_SIZE];
1954		struct ext2_acl_header t_ext2_acl_header;
1955		struct ext2_acl_entry t_ext2_acl_entry;
1956		struct ext2_old_group_desc t_ext2_old_group_desc;
1957		struct ext2_group_desc t_ext2_group_desc;
1958		struct ext2_inode t_ext2_inode;
1959		struct ext2_super_block t_ext2_super_block;
1960		struct ext2_dir_entry t_ext2_dir_entry;
1961	} u;
1962};
1963</ProgramListing>
1964
1965The above union enables me, in the program, to treat the data as raw data or
1966as a meaningful filesystem object.
1967</Para>
1968
1969<Para>
1970The reading and writing, if done to this global variable, are done through
1971the functions <Literal remap="tt">load&lowbar;type&lowbar;data</Literal> and <Literal remap="tt">write&lowbar;type&lowbar;data</Literal>, available in
1972<Literal remap="tt">disk.c</Literal>.
1973</Para>
1974
1975</Sect2>
1976
1977</Sect1>
1978
1979<Sect1>
1980<Title>The general commands</Title>
1981
1982<Para>
1983The <Literal remap="tt">general commands</Literal> are handled in the file <Literal remap="tt">general&lowbar;com.c</Literal>.
1984</Para>
1985
1986<Sect2>
1987<Title>The help system</Title>
1988
1989<Para>
1990The help command is handled by the function <Literal remap="tt">help</Literal>. The algorithm is as
1991follows:
1992</Para>
1993
1994<Para>
1995
1996<OrderedList>
1997<ListItem>
1998
1999<Para>
2000	Check the command line arguments. If there is an argument, pass
2001control to the <Literal remap="tt">detailed&lowbar;help</Literal> function, in order to provide
2002help on the specific command.
2003</Para>
2004</ListItem>
2005<ListItem>
2006
2007<Para>
2008	If general help was requested, display a list of the available
2009commands at this point. The three levels are displayed in reverse
2010order - First the commands which are specific to the current type
2011(If a current type is defined), then the ext2 general commands (If
2012we decided that the filesystem should be treated like an ext2
2013filesystem), then the general commands.
2014</Para>
2015</ListItem>
2016<ListItem>
2017
2018<Para>
2019	Display information about EXT2ED - Current version, general
2020information about the project, etc.
2021</Para>
2022</ListItem>
2023
2024</OrderedList>
2025
2026</Para>
2027
2028</Sect2>
2029
2030<Sect2>
2031<Title>The setdevice command</Title>
2032
2033<Para>
2034The <Literal remap="tt">setdevice</Literal> commands result in calling the <Literal remap="tt">set&lowbar;device</Literal>
2035function. The algorithm is:
2036</Para>
2037
2038<Para>
2039
2040<OrderedList>
2041<ListItem>
2042
2043<Para>
2044	Parse the command line argument. If it isn't available report the
2045error and return.
2046</Para>
2047</ListItem>
2048<ListItem>
2049
2050<Para>
2051	Close the current open device, if there is one.
2052</Para>
2053</ListItem>
2054<ListItem>
2055
2056<Para>
2057	Open the new device in read-only mode. Update the global variables
2058<Literal remap="tt">device&lowbar;name</Literal> and <Literal remap="tt">device&lowbar;handle</Literal>.
2059</Para>
2060</ListItem>
2061<ListItem>
2062
2063<Para>
2064	Disable write access.
2065</Para>
2066</ListItem>
2067<ListItem>
2068
2069<Para>
2070	Empty the object memory.
2071</Para>
2072</ListItem>
2073<ListItem>
2074
2075<Para>
2076	Unregister the ext2 general commands, using
2077<Literal remap="tt">free&lowbar;user&lowbar;commands</Literal>.
2078</Para>
2079</ListItem>
2080<ListItem>
2081
2082<Para>
2083	Unregister the current objects, using <Literal remap="tt">free&lowbar;struct&lowbar;descriptors</Literal>
2084</Para>
2085</ListItem>
2086<ListItem>
2087
2088<Para>
2089	Call <Literal remap="tt">set&lowbar;file&lowbar;system&lowbar;info</Literal> to auto-detect an ext2 filesystem
2090and set the basic filesystem values.
2091</Para>
2092</ListItem>
2093<ListItem>
2094
2095<Para>
2096	Add the <Literal remap="tt">alternate descriptors</Literal>, supplied by the user.
2097</Para>
2098</ListItem>
2099<ListItem>
2100
2101<Para>
2102	Set the device offset to the filesystem start by dispatching
2103<Literal remap="tt">setoffset 0</Literal>.
2104</Para>
2105</ListItem>
2106<ListItem>
2107
2108<Para>
2109	Show the new available commands by dispatching the <Literal remap="tt">help</Literal>
2110command.
2111</Para>
2112</ListItem>
2113
2114</OrderedList>
2115
2116</Para>
2117
2118</Sect2>
2119
2120<Sect2>
2121<Title>Basic maneuvering</Title>
2122
2123<Para>
2124Basic maneuvering is done using the <Literal remap="tt">setoffset</Literal> and the <Literal remap="tt">settype</Literal>
2125user commands.
2126</Para>
2127
2128<Para>
2129<Literal remap="tt">set&lowbar;offset</Literal> accepts some alternative forms of specifying the new
2130offset. They all ultimately lead to changing the <Literal remap="tt">device&lowbar;offset</Literal>
2131global variable and seeking to the new position. <Literal remap="tt">set&lowbar;offset</Literal> also
2132calls <Literal remap="tt">load&lowbar;type&lowbar;data</Literal> to read a block ahead of the new position into
2133the <Literal remap="tt">type&lowbar;data</Literal> global variable.
2134</Para>
2135
2136<Para>
2137<Literal remap="tt">set&lowbar;type</Literal> will point the global variable <Literal remap="tt">current&lowbar;type</Literal> to the
2138correct entry in the double linked list of the known objects. If the
2139requested type is <Literal remap="tt">hex</Literal> or <Literal remap="tt">none</Literal>, <Literal remap="tt">current&lowbar;type</Literal> will be
2140initialized to <Literal remap="tt">NULL</Literal>. <Literal remap="tt">set&lowbar;type</Literal> will also dispatch <Literal remap="tt">show</Literal>,
2141so that the object data will be re-formatted in the new format.
2142</Para>
2143
2144<Para>
2145When editing an ext2 filesystem, it is not intended that those commands will
2146be used directly, and it is usually not required. My implementation of the
2147ext2 layer, on the other hand, uses this lower level commands on countless
2148occasions.
2149</Para>
2150
2151</Sect2>
2152
2153<Sect2>
2154<Title>The display functions</Title>
2155
2156<Para>
2157The general command version of <Literal remap="tt">show</Literal> is handled by the <Literal remap="tt">show</Literal>
2158function. This command is overridden by various objects to provide a display
2159which is better suited to the object.
2160</Para>
2161
2162<Para>
2163The general show command will format the data in <Literal remap="tt">type&lowbar;data</Literal> according
2164to the structure definition of the current type and show it on the <Literal remap="tt">show
2165pad</Literal>. If there is no current type, the data will be shown as a simple hex
2166dump; Otherwise, the list of variables, along with their values will be shown.
2167</Para>
2168
2169<Para>
2170A call to <Literal remap="tt">show&lowbar;info</Literal> is also made - <Literal remap="tt">show&lowbar;info</Literal> will provide
2171<Literal remap="tt">general statistics</Literal> on the <Literal remap="tt">show&lowbar;window</Literal>, such as the current
2172block, current type, current offset and current page.
2173</Para>
2174
2175<Para>
2176The <Literal remap="tt">pgup</Literal> and <Literal remap="tt">pgdn</Literal> general commands just update the
2177<Literal remap="tt">show&lowbar;pad&lowbar;info</Literal> global variable - We just increment
2178<Literal remap="tt">show&lowbar;pad&lowbar;info.line</Literal> with the number of lines in the screen -
2179<Literal remap="tt">show&lowbar;pad&lowbar;info.display&lowbar;lines</Literal>, which was initialized in
2180<Literal remap="tt">init&lowbar;windows</Literal>.
2181</Para>
2182
2183</Sect2>
2184
2185<Sect2>
2186<Title>Changing data</Title>
2187
2188<Para>
2189Data change is done in memory only. An update to the disk if followed by an
2190explicit <Literal remap="tt">writedata</Literal> command to the disk. The <Literal remap="tt">write&lowbar;data</Literal>
2191function simple calls the <Literal remap="tt">write&lowbar;type&lowbar;data</Literal> function, outlined earlier.
2192</Para>
2193
2194<Para>
2195The <Literal remap="tt">set</Literal> command is used for changing the data.
2196</Para>
2197
2198<Para>
2199If there is no current type, control is passed to the <Literal remap="tt">hex&lowbar;set</Literal> function,
2200which treats the data as a block of bytes and uses the
2201<Literal remap="tt">type&lowbar;data.offset&lowbar;in&lowbar;block</Literal> variable to write the new text or hex string
2202to the correct place in the block.
2203</Para>
2204
2205<Para>
2206If a current type is defined, the requested variable is searched in the
2207current object, and the desired new valued is entered.
2208</Para>
2209
2210<Para>
2211The <Literal remap="tt">enablewrite</Literal> commands just sets the global variable
2212<Literal remap="tt">write&lowbar;access</Literal> to <Literal remap="tt">1</Literal> and re-opens the filesystem in read-write
2213mode, if possible.
2214</Para>
2215
2216<Para>
2217If the current type is NULL, a hex-mode is assumed - The <Literal remap="tt">next</Literal> and
2218<Literal remap="tt">prev</Literal> commands will just update <Literal remap="tt">type&lowbar;data.offset&lowbar;in&lowbar;block</Literal>.
2219</Para>
2220
2221<Para>
2222If the current type is not NULL, the The <Literal remap="tt">next</Literal> and <Literal remap="tt">prev</Literal> command
2223are usually overridden anyway. If they are not overridden, it will be assumed
2224that the user is editing an array of such objects, and they will just pass
2225to the next / prev element by dispatching to <Literal remap="tt">setoffset</Literal> using the
2226<Literal remap="tt">setoffset type + / - X</Literal> syntax.
2227</Para>
2228
2229</Sect2>
2230
2231</Sect1>
2232
2233<Sect1>
2234<Title>The ext2 general commands</Title>
2235
2236<Para>
2237The ext2 general commands are contained in the <Literal remap="tt">ext2&lowbar;general&lowbar;commands</Literal>
2238global variable (which is of type <Literal remap="tt">struct struct&lowbar;commands</Literal>).
2239</Para>
2240
2241<Para>
2242The handling functions are implemented in the source file <Literal remap="tt">ext2&lowbar;com.c</Literal>.
2243I will include the entire source code since it is relatively short.
2244</Para>
2245
2246<Sect2>
2247<Title>The super command</Title>
2248
2249<Para>
2250The super command just "brings the user" to the main superblock and set the
2251type to ext2&lowbar;super&lowbar;block. The implementation is trivial:
2252</Para>
2253
2254<Para>
2255
2256<ProgramListing>
2257void type_ext2___super (char *command_line)
2258
2259{
2260	char buffer [80];
2261	
2262	super_info.copy_num=0;
2263	sprintf (buffer,"setoffset %ld",file_system_info.super_block_offset);dispatch (buffer);
2264	sprintf (buffer,"settype ext2_super_block");dispatch (buffer);
2265}
2266</ProgramListing>
2267
2268It involves only setting the <Literal remap="tt">copy&lowbar;num</Literal> variable to indicate the main
2269copy, dispatching a <Literal remap="tt">setoffset</Literal> command to reach the superblock, and
2270dispatching a <Literal remap="tt">settype</Literal> to enable the superblock specific commands.
2271This last command will also call the <Literal remap="tt">show</Literal> command of the
2272<Literal remap="tt">ext2&lowbar;super&lowbar;block</Literal> type, through dispatching at the general command
2273<Literal remap="tt">settype</Literal>.
2274</Para>
2275
2276</Sect2>
2277
2278<Sect2>
2279<Title>The group command</Title>
2280
2281<Para>
2282The group command will bring the user to the specified group descriptor in
2283the main copy of the group descriptors. The type will be set to
2284<Literal remap="tt">ext2&lowbar;group&lowbar;desc</Literal>:
2285
2286<ProgramListing>
2287void type_ext2___group (char *command_line)
2288
2289{
2290	long group_num=0;
2291	char *ptr,buffer [80];
2292	
2293	ptr=parse_word (command_line,buffer);
2294	if (*ptr!=0) {
2295		ptr=parse_word (ptr,buffer);
2296		group_num=atol (buffer);
2297	}
2298
2299	group_info.copy_num=0;group_info.group_num=0;
2300	sprintf (buffer,"setoffset %ld",file_system_info.first_group_desc_offset);dispatch (buffer);
2301	sprintf (buffer,"settype ext2_group_desc");dispatch (buffer);
2302	sprintf (buffer,"entry %ld",group_num);dispatch (buffer);
2303}
2304</ProgramListing>
2305
2306The implementation is as trivial as the <Literal remap="tt">super</Literal> implementation. Note
2307the use of the <Literal remap="tt">entry</Literal> command, which is a command of the
2308<Literal remap="tt">ext2&lowbar;group&lowbar;desc</Literal> object, to pass to the correct group descriptor.
2309</Para>
2310
2311</Sect2>
2312
2313<Sect2>
2314<Title>The cd command</Title>
2315
2316<Para>
2317The <Literal remap="tt">cd</Literal> command performs the usual cd function. The path to the global
2318cd command is a path from <Literal remap="tt">/</Literal>.
2319</Para>
2320
2321<Para>
2322<Literal remap="tt">This is one of the best examples of the power of the object oriented
2323design and of the dispatching mechanism. The operation is complicated, yet the
2324implementation is surprisingly short!</Literal>
2325</Para>
2326
2327<Para>
2328
2329<ProgramListing>
2330void type_ext2___cd (char *command_line)
2331
2332{
2333	char temp [80],buffer [80],*ptr;
2334	
2335	ptr=parse_word (command_line,buffer);
2336	if (*ptr==0) {
2337		wprintw (command_win,"Error - No argument specified\n");
2338		refresh_command_win ();return;
2339	}
2340	ptr=parse_word (ptr,buffer);
2341	
2342	if (buffer [0] != '/') {
2343		wprintw (command_win,"Error - Use a full pathname (begin with '/')\n");
2344		refresh_command_win ();return;
2345	}
2346
2347	dispatch ("super");dispatch ("group");dispatch ("inode");
2348	dispatch ("next");dispatch ("dir");
2349	if (buffer [1] != 0) {
2350		sprintf (temp,"cd %s",buffer+1);dispatch (temp);
2351	}
2352}
2353</ProgramListing>
2354
2355</Para>
2356
2357<Para>
2358Note the number of the dispatch calls! 
2359</Para>
2360
2361<Para>
2362<Literal remap="tt">super</Literal> is used to get to the superblock. <Literal remap="tt">group</Literal> to get to the
2363first group descriptor. <Literal remap="tt">inode</Literal> brings us to the first inode - The bad
2364blocks inode. A <Literal remap="tt">next</Literal> is command to pass to the root directory inode,
2365a <Literal remap="tt">dir</Literal> command "enters" the directory, and then we let the <Literal remap="tt">object
2366specific cd command</Literal> to take us from there (The object is <Literal remap="tt">dir</Literal>, so
2367that <Literal remap="tt">dispatch</Literal> will call the <Literal remap="tt">cd</Literal> command of the <Literal remap="tt">dir</Literal> type).
2368Note that a symbolic link following could bring us back to the root directory,
2369thus the innocent calls above treats nicely such a recursive case!
2370</Para>
2371
2372<Para>
2373I feel that the above is <Literal remap="tt">intuitive</Literal> - I was expressing myself "in the
2374language" of the ext2 filesystem - (Go to the inode, etc), and the code was
2375written exactly in this spirit!
2376</Para>
2377
2378<Para>
2379I can write more at this point, but I guess I am already a bit carried
2380away with the self compliments :-)
2381</Para>
2382
2383</Sect2>
2384
2385</Sect1>
2386
2387<Sect1>
2388<Title>The superblock</Title>
2389
2390<Para>
2391This section details the handling of the superblock.
2392</Para>
2393
2394<Sect2>
2395<Title>The superblock variables</Title>
2396
2397<Para>
2398The superblock object is <Literal remap="tt">ext2&lowbar;super&lowbar;block</Literal>. The definition is just
2399taken from the kernel ext2 main include file - /usr/include/linux/ext2&lowbar;fs.h.
2400<FOOTNOTE>
2401
2402<Para>
2403Those lines of source are copyrighted by <Literal remap="tt">Remy Card</Literal> - The author of the
2404ext2 filesystem, and by <Literal remap="tt">Linus Torvalds</Literal> - The first author of the Linux
2405operating system. Please cross reference the section Acknowledgments for the
2406full copyright.
2407</Para>
2408
2409</FOOTNOTE>
2410
2411
2412
2413<ProgramListing>
2414struct ext2_super_block {
2415	__u32	s_inodes_count;		/* Inodes count */
2416	__u32	s_blocks_count;		/* Blocks count */
2417	__u32	s_r_blocks_count;	/* Reserved blocks count */
2418	__u32	s_free_blocks_count;	/* Free blocks count */
2419	__u32	s_free_inodes_count;	/* Free inodes count */
2420	__u32	s_first_data_block;	/* First Data Block */
2421	__u32	s_log_block_size;	/* Block size */
2422	__s32	s_log_frag_size;	/* Fragment size */
2423	__u32	s_blocks_per_group;	/* # Blocks per group */
2424	__u32	s_frags_per_group;	/* # Fragments per group */
2425	__u32	s_inodes_per_group;	/* # Inodes per group */
2426	__u32	s_mtime;		/* Mount time */
2427	__u32	s_wtime;		/* Write time */
2428	__u16	s_mnt_count;		/* Mount count */
2429	__s16	s_max_mnt_count;	/* Maximal mount count */
2430	__u16	s_magic;		/* Magic signature */
2431	__u16	s_state;		/* File system state */
2432	__u16	s_errors;		/* Behavior when detecting errors */
2433	__u16	s_pad;
2434	__u32	s_lastcheck;		/* time of last check */
2435	__u32	s_checkinterval;	/* max. time between checks */
2436	__u32	s_creator_os;		/* OS */
2437	__u32	s_rev_level;		/* Revision level */
2438	__u16	s_def_resuid;		/* Default uid for reserved blocks */
2439	__u16	s_def_resgid;		/* Default gid for reserved blocks */
2440	__u32	s_reserved[0];		/* Padding to the end of the block */
2441	__u32	s_reserved[1];		/* Padding to the end of the block */
2442	.
2443	.
2444	.
2445	__u32	s_reserved[234];	/* Padding to the end of the block */
2446};
2447</ProgramListing>
2448
2449</Para>
2450
2451<Para>
2452Note that I <Literal remap="tt">expanded</Literal> the array due to my primitive parser
2453implementation. The various fields are described in the <Literal remap="tt">technical
2454document</Literal>.
2455</Para>
2456
2457</Sect2>
2458
2459<Sect2>
2460<Title>The superblock commands</Title>
2461
2462<Para>
2463This section explains the commands available in the <Literal remap="tt">ext2&lowbar;super&lowbar;block</Literal>
2464type. They all appear in <Literal remap="tt">super&lowbar;com.c</Literal>
2465</Para>
2466
2467<Sect3>
2468<Title>The show command</Title>
2469
2470<Para>
2471The <Literal remap="tt">show</Literal> command is overridden here in order to provide more
2472information than just the list of variables. A <Literal remap="tt">show</Literal> command will end
2473up in calling <Literal remap="tt">type&lowbar;super&lowbar;block&lowbar;&lowbar;&lowbar;show</Literal>.
2474</Para>
2475
2476<Para>
2477The first thing that we do is calling the <Literal remap="tt">general show command</Literal> in
2478order to display the list of variables.
2479</Para>
2480
2481<Para>
2482We then add some interpretation to the various lines to make the data
2483somewhat more intuitive (Expansion of the time variables and the creator
2484operating system code, for example).
2485</Para>
2486
2487<Para>
2488We also display the <Literal remap="tt">backup copy number</Literal> of the superblock in the status
2489window. This copy number is saved in the <Literal remap="tt">super&lowbar;info</Literal> global variable -
2490<Literal remap="tt">super&lowbar;info.copy&lowbar;num</Literal>. Currently, this is the only variable there ...
2491but this type of internal variable saving is typical through my
2492implementation.
2493</Para>
2494
2495</Sect3>
2496
2497<Sect3>
2498<Title>The backup copies handling commands</Title>
2499
2500<Para>
2501The <Literal remap="tt">current copy number</Literal> is available in <Literal remap="tt">super&lowbar;info.copy&lowbar;num</Literal>. It
2502was initialized in the ext2 command <Literal remap="tt">super</Literal>, and is used by the various
2503superblock routines.
2504</Para>
2505
2506<Para>
2507The <Literal remap="tt">gocopy</Literal> routine will pass to another copy of the superblock. The
2508new device offset will be computed with the aid of the variables in the
2509<Literal remap="tt">file&lowbar;system&lowbar;info</Literal> structure. Then the routine will <Literal remap="tt">dispatch</Literal> to
2510the <Literal remap="tt">setoffset</Literal> and the <Literal remap="tt">show</Literal> routines.
2511</Para>
2512
2513<Para>
2514The <Literal remap="tt">setactivecopy</Literal> routine will just save the current superblock data
2515in a temporary variable of type <Literal remap="tt">ext2&lowbar;super&lowbar;block</Literal>, and will dispatch
2516<Literal remap="tt">gocopy 0</Literal> to pass to the main superblock. Then it will place the saved
2517data in place of the actual data.
2518</Para>
2519
2520<Para>
2521The above two commands can be used if the main superblock is corrupted.
2522</Para>
2523
2524</Sect3>
2525
2526</Sect2>
2527
2528</Sect1>
2529
2530<Sect1>
2531<Title>The group descriptors</Title>
2532
2533<Para>
2534The group descriptors handling mechanism allows the user to take a tour in
2535the group descriptors table, stopping at each point, and examining the
2536relevant inode table, block allocation map or inode allocation map through
2537dispatching to the relevant objects.
2538</Para>
2539
2540<Para>
2541Some information about the group descriptors is available in the global
2542variable <Literal remap="tt">group&lowbar;info</Literal>, which is of type <Literal remap="tt">struct&lowbar;group&lowbar;info</Literal>:
2543</Para>
2544
2545<Para>
2546
2547<ProgramListing>
2548struct struct_group_info {
2549	unsigned long copy_num;
2550	unsigned long group_num;
2551};
2552</ProgramListing>
2553
2554</Para>
2555
2556<Para>
2557<Literal remap="tt">group&lowbar;num</Literal> is the index of the current descriptor in the table.
2558</Para>
2559
2560<Para>
2561<Literal remap="tt">copy&lowbar;num</Literal> is the number of the current backup copy.
2562</Para>
2563
2564<Sect2>
2565<Title>The group descriptor's variables</Title>
2566
2567<Para>
2568
2569<ProgramListing>
2570struct ext2_group_desc
2571{
2572	__u32	bg_block_bitmap;		/* Blocks bitmap block */
2573	__u32	bg_inode_bitmap;		/* Inodes bitmap block */
2574	__u32	bg_inode_table;			/* Inodes table block */
2575	__u16	bg_free_blocks_count;		/* Free blocks count */
2576	__u16	bg_free_inodes_count;		/* Free inodes count */
2577	__u16	bg_used_dirs_count;		/* Directories count */
2578	__u16	bg_pad;
2579	__u32	bg_reserved[3];
2580};
2581</ProgramListing>
2582
2583</Para>
2584
2585<Para>
2586The first three variables are used to provide the links to the
2587<Literal remap="tt">blockbitmap, inodebitmap and inode</Literal> objects.
2588</Para>
2589
2590</Sect2>
2591
2592<Sect2>
2593<Title>Movement in the table</Title>
2594
2595<Para>
2596Movement in the group descriptors table is done using the <Literal remap="tt">next, prev and
2597entry</Literal> commands. Note that the first two commands <Literal remap="tt">override</Literal> the
2598general commands of the same name. The <Literal remap="tt">next and prev</Literal> command are just
2599calling the <Literal remap="tt">entry</Literal> function to do the job. I will show <Literal remap="tt">next</Literal>,
2600for example:
2601</Para>
2602
2603<Para>
2604
2605<ProgramListing>
2606void type_ext2_group_desc___next (char *command_line)
2607 
2608{
2609	long entry_offset=1;
2610	char *ptr,buffer [80];
2611	
2612	ptr=parse_word (command_line,buffer);
2613	if (*ptr!=0) {
2614		ptr=parse_word (ptr,buffer);
2615		entry_offset=atol (buffer);
2616	}
2617
2618	sprintf (buffer,"entry %ld",group_info.group_num+entry_offset);
2619	dispatch (buffer);
2620}
2621</ProgramListing>
2622
2623The <Literal remap="tt">entry</Literal> function is also simple - It just calculates the offset
2624using the information in <Literal remap="tt">group&lowbar;info</Literal> and in <Literal remap="tt">file&lowbar;system&lowbar;info</Literal>,
2625and uses the usual <Literal remap="tt">setoffset / show</Literal> pair. 
2626</Para>
2627
2628</Sect2>
2629
2630<Sect2>
2631<Title>The show command</Title>
2632
2633<Para>
2634As usual, the <Literal remap="tt">show</Literal> command is overridden. The implementation is
2635similar to the superblock's show implementation - We just call the general
2636show command, and add some information in the status window - The contents of
2637the <Literal remap="tt">group&lowbar;info</Literal> structure.
2638</Para>
2639
2640</Sect2>
2641
2642<Sect2>
2643<Title>Moving between backup copies</Title>
2644
2645<Para>
2646This is done exactly like the superblock case. Please refer to explanation
2647there.
2648</Para>
2649
2650</Sect2>
2651
2652<Sect2>
2653<Title>Links to the available friends</Title>
2654
2655<Para>
2656From a group descriptor, one typically wants to reach an <Literal remap="tt">inode</Literal>, or
2657one of the <Literal remap="tt">allocation bitmaps</Literal>. This is done using the <Literal remap="tt">inode,
2658blockbitmap or inodebitmap</Literal> commands. The implementation is again trivial
2659- Get the necessary information from the group descriptor, initialize the
2660structures of the next type, and issue the <Literal remap="tt">setoffset / settype</Literal> pair.
2661</Para>
2662
2663<Para>
2664For example, here is the implementation of the <Literal remap="tt">blockbitmap</Literal> command:
2665</Para>
2666
2667<Para>
2668
2669<ProgramListing>
2670void type_ext2_group_desc___blockbitmap (char *command_line)
2671
2672{
2673	long block_bitmap_offset;
2674	char buffer [80];
2675	
2676	block_bitmap_info.entry_num=0;
2677	block_bitmap_info.group_num=group_info.group_num;
2678
2679	block_bitmap_offset=type_data.u.t_ext2_group_desc.bg_block_bitmap;
2680	sprintf (buffer,"setoffset block %ld",block_bitmap_offset);dispatch (buffer);
2681	sprintf (buffer,"settype block_bitmap");dispatch (buffer);
2682}
2683</ProgramListing>
2684
2685</Para>
2686
2687</Sect2>
2688
2689</Sect1>
2690
2691<Sect1>
2692<Title>The inode table</Title>
2693
2694<Para>
2695The inode handling enables the user to move in the inode table, edit the
2696various attributes of the inode, and follow to the next stage - A file or a
2697directory.
2698</Para>
2699
2700<Sect2>
2701<Title>The inode variables</Title>
2702
2703<Para>
2704
2705<ProgramListing>
2706struct ext2_inode {
2707	__u16	i_mode;		/* File mode */
2708	__u16	i_uid;		/* Owner Uid */
2709	__u32	i_size;		/* Size in bytes */
2710	__u32	i_atime;	/* Access time */
2711	__u32	i_ctime;	/* Creation time */
2712	__u32	i_mtime;	/* Modification time */
2713	__u32	i_dtime;	/* Deletion Time */
2714	__u16	i_gid;		/* Group Id */
2715	__u16	i_links_count;	/* Links count */
2716	__u32	i_blocks;	/* Blocks count */
2717	__u32	i_flags;	/* File flags */
2718	union {
2719		struct {
2720			__u32  l_i_reserved1;
2721		} linux1;
2722		struct {
2723			__u32  h_i_translator;
2724		} hurd1;
2725		struct {
2726			__u32  m_i_reserved1;
2727		} masix1;
2728	} osd1;				/* OS dependent 1 */
2729	__u32	i_block[EXT2_N_BLOCKS];	/* Pointers to blocks */
2730	__u32	i_version;		/* File version (for NFS) */
2731	__u32	i_file_acl;		/* File ACL */
2732	__u32	i_dir_acl;		/* Directory ACL */
2733	__u32	i_faddr;		/* Fragment address */
2734	union {
2735		struct {
2736			__u8	l_i_frag;	/* Fragment number */
2737			__u8	l_i_fsize;	/* Fragment size */
2738			__u16	i_pad1;
2739			__u32	l_i_reserved2[2];
2740		} linux2;
2741		struct {
2742			__u8	h_i_frag;	/* Fragment number */
2743			__u8	h_i_fsize;	/* Fragment size */
2744			__u16	h_i_mode_high;
2745			__u16	h_i_uid_high;
2746			__u16	h_i_gid_high;
2747			__u32	h_i_author;
2748		} hurd2;
2749		struct {
2750			__u8	m_i_frag;	/* Fragment number */
2751			__u8	m_i_fsize;	/* Fragment size */
2752			__u16	m_pad1;
2753			__u32	m_i_reserved2[2];
2754		} masix2;
2755	} osd2;				/* OS dependent 2 */
2756};
2757</ProgramListing>
2758
2759</Para>
2760
2761<Para>
2762The above is the original source code definition. We can see that the inode
2763supports <Literal remap="tt">Operating systems specific structures</Literal>. In addition to the
2764expansion of the arrays, I have <Literal remap="tt">"flattened</Literal> the inode to support only
2765the <Literal remap="tt">Linux</Literal> declaration. It seemed that this one occasion of multiple
2766variable aliases didn't justify the complication of generally supporting
2767aliases. In any case, the above system specific variables are not used
2768internally by EXT2ED, and the user is free to change the definition in
2769<Literal remap="tt">ext2.descriptors</Literal> to accommodate for his needs.
2770</Para>
2771
2772</Sect2>
2773
2774<Sect2>
2775<Title>The handling functions</Title>
2776
2777<Para>
2778The user interface to <Literal remap="tt">movement</Literal> is the usual <Literal remap="tt">next / prev /
2779entry</Literal> interface. There is really nothing special in those functions - The
2780size of the inode is fixed, the total number of inodes is known from the
2781superblock information, and the current entry can be figured up from the
2782device offset and the inode table start offset, which is known from the
2783corresponding group descriptor. Those functions are a bit older then some
2784other implementations of <Literal remap="tt">next</Literal> and <Literal remap="tt">prev</Literal>, and they do not save
2785information in a special structure. Rather, they recompute it when
2786necessary.
2787</Para>
2788
2789<Para>
2790The <Literal remap="tt">show</Literal> command is overridden here, and provides a lot of additional
2791information about the inode - Its type, interpretation of the permissions,
2792special ext2 attributes (Immutable file, for example), and a lot more.
2793Again, the <Literal remap="tt">general show</Literal> is called first, and then the additional
2794information is written.
2795</Para>
2796
2797</Sect2>
2798
2799<Sect2>
2800<Title>Accessing files and directories</Title>
2801
2802<Para>
2803From the inode, a <Literal remap="tt">file</Literal> or a <Literal remap="tt">directory</Literal> can typically be reached.
2804In order to treat a file, for example, its inode needs to be constantly
2805accessed. To satisfy that need, when editing a file or a directory, the
2806inode is still saved in memory - <Literal remap="tt">type&lowbar;data</Literal> is not overwritten.
2807Rather, the following takes place:
2808
2809<ItemizedList>
2810<ListItem>
2811
2812<Para>
2813	An internal global structure which is used by the types <Literal remap="tt">file</Literal>
2814and <Literal remap="tt">dir</Literal> handling functions is initialized by calling the
2815appropriate function.
2816</Para>
2817</ListItem>
2818<ListItem>
2819
2820<Para>
2821	The type is changed accordingly.
2822</Para>
2823</ListItem>
2824
2825</ItemizedList>
2826
2827The result is that a <Literal remap="tt">settype ext2&lowbar;inode</Literal> is the only action necessary
2828to return to the inode - We actually never left it.
2829</Para>
2830
2831<Para>
2832Follows the implementation of the inode's <Literal remap="tt">file</Literal> command:
2833</Para>
2834
2835<Para>
2836
2837<ProgramListing>
2838void type_ext2_inode___file (char *command_line)
2839
2840{
2841	char buffer [80];
2842	
2843	if (!S_ISREG (type_data.u.t_ext2_inode.i_mode)) {
2844		wprintw (command_win,"Error - Inode type is not file\n");
2845		refresh_command_win ();	return;		
2846	}
2847	
2848	if (!init_file_info ()) {
2849		wprintw (command_win,"Error - Unable to show file\n");
2850		refresh_command_win ();return;
2851	}
2852	
2853	sprintf (buffer,"settype file");dispatch (buffer);
2854}
2855</ProgramListing>
2856
2857</Para>
2858
2859<Para>
2860As we can see - We just call <Literal remap="tt">init&lowbar;file&lowbar;info</Literal> to get the necessary
2861information from the inode, and set the type to <Literal remap="tt">file</Literal>. The next call
2862to <Literal remap="tt">show</Literal>, will dispatch to the <Literal remap="tt">file's show</Literal> implementation.
2863</Para>
2864
2865</Sect2>
2866
2867</Sect1>
2868
2869<Sect1>
2870<Title>Viewing a file</Title>
2871
2872<Para>
2873There isn't an ext2 kernel structure which corresponds to a file - A file is
2874just a series of blocks which are determined by its inode. As explained in
2875the last section, the inode is never actually left - The type is changed to
2876<Literal remap="tt">file</Literal> - A type which contains no variables, and a special structure is
2877initialized:
2878</Para>
2879
2880<Para>
2881
2882<ProgramListing>
2883struct struct_file_info {
2884
2885	struct ext2_inodes *inode_ptr;
2886	
2887	long inode_offset;
2888	long global_block_num,global_block_offset;
2889	long block_num,blocks_count;
2890	long file_offset,file_length;
2891	long level;
2892	unsigned char buffer [EXT2_MAX_BLOCK_SIZE];
2893	long offset_in_block;
2894
2895	int display;
2896	/* The following is used if the file is a directory */
2897	
2898	long dir_entry_num,dir_entries_count;
2899	long dir_entry_offset;
2900};
2901</ProgramListing>
2902
2903</Para>
2904
2905<Para>
2906The <Literal remap="tt">inode&lowbar;ptr</Literal> will just point to the inode in <Literal remap="tt">type&lowbar;data</Literal>, which
2907is not overwritten while the user is editing the file, as the
2908<Literal remap="tt">setoffset</Literal> command is not internally used. The <Literal remap="tt">buffer</Literal>
2909will contain the current viewed block of the file. The other variables
2910contain information about the current place in the file. For example,
2911<Literal remap="tt">global&lowbar;block&lowbar;num</Literal> just contains the current block number.
2912</Para>
2913
2914<Para>
2915The general idea is that the above data structure will provide the file
2916handling functions all the accurate information which is needed to accomplish
2917their task.
2918</Para>
2919
2920<Para>
2921The global structure of the above type, <Literal remap="tt">file&lowbar;info</Literal>, is initialized by
2922<Literal remap="tt">init&lowbar;file&lowbar;info</Literal> in <Literal remap="tt">file&lowbar;com.c</Literal>, which is called by the
2923<Literal remap="tt">type&lowbar;ext2&lowbar;inode&lowbar;&lowbar;&lowbar;file</Literal> function when the user requests to watch the
2924file. <Literal remap="tt">It is updated as necessary to provide accurate information as long as
2925the file is edited.</Literal>
2926</Para>
2927
2928<Sect2>
2929<Title>Returning to the file's inode</Title>
2930
2931<Para>
2932Concerning the method I used to handle files, the above task is trivial:
2933
2934<ProgramListing>
2935void type_file___inode (char *command_line)
2936
2937{
2938	dispatch ("settype ext2_inode");
2939}
2940</ProgramListing>
2941
2942</Para>
2943
2944</Sect2>
2945
2946<Sect2>
2947<Title>File movement</Title>
2948
2949<Para>
2950EXT2ED keeps track of the current position in the file. Movement inside the
2951current block is done using <Literal remap="tt">next, prev and offset</Literal> - They just change
2952<Literal remap="tt">file&lowbar;info.offset&lowbar;in&lowbar;block</Literal>.
2953</Para>
2954
2955<Para>
2956Movement between blocks is done using <Literal remap="tt">nextblock, prevblock and block</Literal>.
2957To accomplish this, the direct blocks, indirect blocks, etc, need to be
2958traced. This is done by <Literal remap="tt">file&lowbar;block&lowbar;to&lowbar;global&lowbar;block</Literal>, which accepts a
2959file's internal block number, and converts it to the actual filesystem block
2960number.
2961</Para>
2962
2963<Para>
2964
2965<ProgramListing>
2966long file_block_to_global_block (long file_block,struct struct_file_info *file_info_ptr)
2967
2968{
2969	long last_direct,last_indirect,last_dindirect;
2970	long f_indirect,s_indirect;
2971	
2972	last_direct=EXT2_NDIR_BLOCKS-1;
2973	last_indirect=last_direct+file_system_info.block_size/4;
2974	last_dindirect=last_indirect+(file_system_info.block_size/4) \
2975		*(file_system_info.block_size/4);
2976
2977	if (file_block &#60;= last_direct) {
2978		file_info_ptr-&#62;level=0;
2979		return (file_info_ptr-&#62;inode_ptr-&#62;i_block [file_block]);
2980	}
2981	
2982	if (file_block &#60;= last_indirect) {
2983		file_info_ptr-&#62;level=1;
2984		file_block=file_block-last_direct-1;
2985		return (return_indirect (file_info_ptr-&#62;inode_ptr-&#62; \
2986			i_block [EXT2_IND_BLOCK],file_block));
2987	}
2988
2989	if (file_block &#60;= last_dindirect) {
2990		file_info_ptr-&#62;level=2;
2991		file_block=file_block-last_indirect-1;
2992		return (return_dindirect (file_info_ptr-&#62;inode_ptr-&#62; \
2993			i_block [EXT2_DIND_BLOCK],file_block));
2994	}
2995
2996	file_info_ptr-&#62;level=3;
2997	file_block=file_block-last_dindirect-1;
2998	return (return_tindirect (file_info_ptr-&#62;inode_ptr-&#62; \
2999		i_block [EXT2_TIND_BLOCK],file_block));
3000}
3001</ProgramListing>
3002
3003<Literal remap="tt">last&lowbar;direct, last&lowbar;indirect, etc</Literal>, contain the last internal block number
3004which is accessed by this method - If the requested block is smaller then
3005<Literal remap="tt">last&lowbar;direct</Literal>, for example, it is a direct block.
3006</Para>
3007
3008<Para>
3009If the block is a direct block, its number is just taken from the inode.
3010A non-direct block is handled by <Literal remap="tt">return&lowbar;indirect, return&lowbar;dindirect and
3011return&lowbar;tindirect</Literal>, which correspond to indirect, double-indirect and
3012triple-indirect. Each of the above functions is constructed using the lower
3013level functions. For example, <Literal remap="tt">return&lowbar;dindirect</Literal> is constructed as
3014follows:
3015</Para>
3016
3017<Para>
3018
3019<ProgramListing>
3020long return_dindirect (long table_block,long block_num)
3021
3022{
3023	long f_indirect;
3024	
3025	f_indirect=block_num/(file_system_info.block_size/4);
3026	f_indirect=return_indirect (table_block,f_indirect);
3027	return (return_indirect (f_indirect,block_num%(file_system_info.block_size/4)));
3028}
3029</ProgramListing>
3030
3031</Para>
3032
3033</Sect2>
3034
3035<Sect2>
3036<Title>Object memory</Title>
3037
3038<Para>
3039The <Literal remap="tt">remember</Literal> command is overridden here and in the <Literal remap="tt">dir</Literal> type -
3040We just remember the inode of the file. It is just simpler to implement, and
3041doesn't seem like a big limitation.
3042</Para>
3043
3044</Sect2>
3045
3046<Sect2>
3047<Title>Changing data</Title>
3048
3049<Para>
3050The <Literal remap="tt">set</Literal> command is overridden, and provides the same functionality
3051like the usage of the <Literal remap="tt">general set</Literal> command with no type declared. The
3052<Literal remap="tt">writedata</Literal> is overridden so that we'll write the edited block
3053(file&lowbar;info.buffer) and not <Literal remap="tt">type&lowbar;data</Literal> (Which contains the inode).
3054</Para>
3055
3056</Sect2>
3057
3058</Sect1>
3059
3060<Sect1>
3061<Title>Directories</Title>
3062
3063<Para>
3064A directory is just a file which is formatted according to a special format.
3065As such, EXT2ED handles directories and files quite alike. Specifically, the
3066same variable of type <Literal remap="tt">struct&lowbar;file&lowbar;info</Literal> which is used in the
3067<Literal remap="tt">file</Literal>, is used here.
3068</Para>
3069
3070<Para>
3071The <Literal remap="tt">dir</Literal> type uses all the variables in the above structure, as
3072opposed to the <Literal remap="tt">file</Literal> type, which didn't use the last ones.
3073</Para>
3074
3075<Sect2>
3076<Title>The search&lowbar;dir&lowbar;entries function</Title>
3077
3078<Para>
3079The entire situation is similar to that which was described in the
3080<Literal remap="tt">file</Literal> type, with one main change:
3081</Para>
3082
3083<Para>
3084The main function in <Literal remap="tt">dir&lowbar;com.c</Literal> is <Literal remap="tt">search&lowbar;dir&lowbar;entries</Literal>. This
3085function will <Literal remap="tt">"run"</Literal> on the entire entries in the directory, and will
3086call a client's function each time. The client's function is supplied as an
3087argument, and will check the current entry for a match, based on its own
3088criterion. It will then signal <Literal remap="tt">search&lowbar;dir&lowbar;entries</Literal> whether to
3089<Literal remap="tt">ABORT</Literal> the search, whether it <Literal remap="tt">FOUND</Literal> the entry it was looking
3090for, or that the entry is still not found, and we should <Literal remap="tt">CONTINUE</Literal>
3091searching. Follows the declaration:
3092
3093<ProgramListing>
3094struct struct_file_info search_dir_entries \ 
3095	(int (*action) (struct struct_file_info *info),int *status)
3096
3097/*
3098	This routine runs on all directory entries in the current directory.
3099	For each entry, action is called. The return code of action is one of
3100	the following:
3101	
3102		ABORT		-	Current dir entry is returned.
3103		CONTINUE	-	Continue searching.
3104		FOUND		-	Current dir entry is returned.
3105		
3106	If the last entry is reached, it is returned, along with an ABORT status.
3107	
3108	status is updated to the returned code of action.	
3109*/
3110</ProgramListing>
3111
3112</Para>
3113
3114<Para>
3115With the above tool in hand, many operations are simple to perform - Here is
3116the way I counted the entries in the current directory:
3117</Para>
3118
3119<Para>
3120
3121<ProgramListing>
3122long count_dir_entries (void)
3123
3124{
3125	int status;
3126	
3127	return (search_dir_entries (&amp;action_count,&amp;status).dir_entry_num);
3128}
3129
3130int action_count (struct struct_file_info *info)
3131
3132{
3133	return (CONTINUE);
3134}
3135</ProgramListing>
3136
3137It will just <Literal remap="tt">CONTINUE</Literal> until the last entry. The returned structure
3138(of type <Literal remap="tt">struct&lowbar;file&lowbar;info</Literal>) will have its number in the
3139<Literal remap="tt">dir&lowbar;entry&lowbar;num</Literal> field, and this is exactly the required number!
3140</Para>
3141
3142</Sect2>
3143
3144<Sect2>
3145<Title>The cd command</Title>
3146
3147<Para>
3148The <Literal remap="tt">cd</Literal> command accepts a relative path, and moves there ...
3149The implementation is of-course a bit more complicated:
3150
3151<OrderedList>
3152<ListItem>
3153
3154<Para>
3155	The path is checked that it is not an absolute path (from <Literal remap="tt">/</Literal>).
3156If it is, we let the <Literal remap="tt">general cd</Literal> to do the job by calling
3157directly <Literal remap="tt">type&lowbar;ext2&lowbar;&lowbar;&lowbar;cd</Literal>.
3158</Para>
3159</ListItem>
3160<ListItem>
3161
3162<Para>
3163	The path is divided into the nearest path and the rest of the path.
3164For example, cd 1/2/3/4 is divided into <Literal remap="tt">1</Literal> and into
3165<Literal remap="tt">2/3/4</Literal>.
3166</Para>
3167</ListItem>
3168<ListItem>
3169
3170<Para>
3171	It is the first part of the path that we need to search for in the
3172current directory. We search for it using <Literal remap="tt">search&lowbar;dir&lowbar;entries</Literal>,
3173which accepts the <Literal remap="tt">action&lowbar;name</Literal> function as the user defined
3174function.
3175</Para>
3176</ListItem>
3177<ListItem>
3178
3179<Para>
3180	<Literal remap="tt">search&lowbar;dir&lowbar;entries</Literal> will scan the entire entries and will call
3181our <Literal remap="tt">action&lowbar;name</Literal> function for each entry. In
3182<Literal remap="tt">action&lowbar;name</Literal>, the required name will be checked against the
3183name of the current entry, and <Literal remap="tt">FOUND</Literal> will be returned when a
3184match occurs.
3185</Para>
3186</ListItem>
3187<ListItem>
3188
3189<Para>
3190	If the required entry is found, we dispatch a <Literal remap="tt">remember</Literal>
3191command to insert the current <Literal remap="tt">inode</Literal> into the object memory.
3192This is required to easily support <Literal remap="tt">symbolic links</Literal> - If we
3193find later that the inode pointed by the entry is actually a
3194symbolic link, we'll need to return to this point, and the above
3195inode doesn't have (and can't have, because of <Literal remap="tt">hard links</Literal>) the
3196information necessary to "move back".
3197</Para>
3198</ListItem>
3199<ListItem>
3200
3201<Para>
3202	We then dispatch a <Literal remap="tt">followinode</Literal> command to reach the inode
3203pointed by the required entry. This command will automatically
3204change the type to <Literal remap="tt">ext2&lowbar;inode</Literal> - We are now at an inode, and
3205all the inode commands are available.
3206</Para>
3207</ListItem>
3208<ListItem>
3209
3210<Para>
3211	We check the inode's type to see if it is a directory. If it is, we
3212dispatch a <Literal remap="tt">dir</Literal> command to "enter the directory", and
3213recursively call ourself (The type is <Literal remap="tt">dir</Literal> again) by
3214dispatching a <Literal remap="tt">cd</Literal> command, with the rest of the path as an
3215argument.
3216</Para>
3217</ListItem>
3218<ListItem>
3219
3220<Para>
3221	If the inode's type is a symbolic link (only fast symbolic link were
3222meanwhile implemented. I guess this is typically the case.), we note
3223the path it is pointing at, the	saved inode is recalled, we dispatch
3224<Literal remap="tt">dir</Literal> to get back to the original directory, and we call
3225ourself again with the <Literal remap="tt">link path/rest of the path</Literal> argument.
3226</Para>
3227</ListItem>
3228<ListItem>
3229
3230<Para>
3231	In any other case, we just stop at the resulting inode.
3232</Para>
3233</ListItem>
3234
3235</OrderedList>
3236
3237</Para>
3238
3239</Sect2>
3240
3241</Sect1>
3242
3243<Sect1>
3244<Title>The block and inode allocation bitmaps</Title>
3245
3246<Para>
3247The block allocation bitmap is reached by the corresponding group descriptor.
3248The group descriptor handling functions will save the necessary information
3249into a structure of the <Literal remap="tt">struct&lowbar;block&lowbar;bitmap&lowbar;info</Literal> type:
3250</Para>
3251
3252<Para>
3253
3254<ProgramListing>
3255struct struct_block_bitmap_info {
3256	unsigned long entry_num;
3257	unsigned long group_num;
3258};
3259</ProgramListing>
3260
3261</Para>
3262
3263<Para>
3264The <Literal remap="tt">show</Literal> command is overridden, and will show the block as a series of
3265bits, each bit corresponding to a block. The main variable is the
3266<Literal remap="tt">entry&lowbar;num</Literal> variable, declared above, which is just the current block
3267number in this block group. The current entry is highlighted, and the
3268<Literal remap="tt">next, prev and entry</Literal> commands just change the above variable.
3269</Para>
3270
3271<Para>
3272The <Literal remap="tt">allocate and deallocate</Literal> change the specified bits. Nothing
3273special about them - They just contain code which converts between bit and
3274byte locations.
3275</Para>
3276
3277<Para>
3278The <Literal remap="tt">inode allocation bitmap</Literal> is treated in much the same fashion, with
3279the same commands available.
3280</Para>
3281
3282</Sect1>
3283
3284<Sect1>
3285<Title>Filesystem size limitation</Title>
3286
3287<Para>
3288While an ext2 filesystem has a size limit of <Literal remap="tt">4 TB</Literal>, EXT2ED currently
3289<Literal remap="tt">can't</Literal> handle filesystems which are <Literal remap="tt">bigger than 2 GB</Literal>.
3290</Para>
3291
3292<Para>
3293This limitation results from my usage of <Literal remap="tt">32 bit long variables</Literal> and
3294of the <Literal remap="tt">fseek</Literal> filesystem call, which can't seek up to 4 TB.
3295</Para>
3296
3297<Para>
3298By looking in the <Literal remap="tt">ext2 library</Literal> source code by <Literal remap="tt">Theodore Ts'o</Literal>,
3299I discovered the <Literal remap="tt">llseek</Literal> system call which can seek to a
3300<Literal remap="tt">64 bit unsigned long long</Literal> offset. Correcting the situation is not
3301difficult in concept - I need to change long into unsigned long long where
3302appropriate and modify <Literal remap="tt">disk.c</Literal> to use the llseek system call.
3303</Para>
3304
3305<Para>
3306However, fixing the above limitation involves making changes in many places
3307in the code and will obviously make the entire code less stable. For that
3308reason, I chose to release EXT2ED as it is now and to postpone the above fix
3309to the next release.
3310</Para>
3311
3312</Sect1>
3313
3314<Sect1>
3315<Title>Conclusion</Title>
3316
3317<Para>
3318Had I known in advance the structure of the ext2 filesystem, I feel that
3319the resulting design would have been quite different from the presented
3320design above.
3321</Para>
3322
3323<Para>
3324EXT2ED has now two levels of abstraction - A <Literal remap="tt">general</Literal> filesystem, and an
3325<Literal remap="tt">ext2</Literal> filesystem, and the surface is more or less prepared for additions
3326of other filesystems. Had I approached the design in the "engineering" way,
3327I guess that the first level above would not have existed.
3328</Para>
3329
3330</Sect1>
3331
3332<Sect1>
3333<Title>Copyright</Title>
3334
3335<Para>
3336EXT2ED is Copyright (C) 1995 Gadi Oxman.
3337</Para>
3338
3339<Para>
3340EXT2ED is hereby placed under the GPL - Gnu Public License. You are free and
3341welcome to copy, view and modify the sources. My only wish is that my
3342copyright presented above will be left and that a list of the bug fixes,
3343added features, etc, will be provided.
3344</Para>
3345
3346<Para>
3347The entire EXT2ED project is based, of-course, on the kernel sources. The
3348<Literal remap="tt">ext2.descriptors</Literal> distributed with EXT2ED is a slightly modified
3349version of the main ext2 include file, /usr/include/linux/ext2&lowbar;fs.h. Follows
3350the original copyright:
3351</Para>
3352
3353<Para>
3354
3355<ProgramListing>
3356/*
3357 *  linux/include/linux/ext2_fs.h
3358 *
3359 * Copyright (C) 1992, 1993, 1994, 1995
3360 * Remy Card (card@masi.ibp.fr)
3361 * Laboratoire MASI - Institut Blaise Pascal
3362 * Universite Pierre et Marie Curie (Paris VI)
3363 *
3364 *  from
3365 *
3366 *  linux/include/linux/minix_fs.h
3367 *
3368 *  Copyright (C) 1991, 1992  Linus Torvalds
3369 */
3370
3371</ProgramListing>
3372
3373</Para>
3374
3375</Sect1>
3376
3377<Sect1>
3378<Title>Acknowledgments</Title>
3379
3380<Para>
3381EXT2ED was constructed as a student project in the software
3382laboratory of the faculty of electrical-engineering in the
3383<Literal remap="tt">Technion - Israel's institute of technology</Literal>.
3384</Para>
3385
3386<Para>
3387At first, I would like to thank <Literal remap="tt">Avner Lottem</Literal> and <Literal remap="tt">Doctor Ilana
3388David</Literal> for their interest and assistance in this project.
3389</Para>
3390
3391<Para>
3392I would also like to thank the following people, who were involved in the
3393design and implementation of the ext2 filesystem kernel code and support
3394utilities:
3395
3396<ItemizedList>
3397<ListItem>
3398
3399<Para>
3400	<Literal remap="tt">Remy Card</Literal>
3401
3402Who designed, implemented and maintains the ext2 filesystem kernel
3403code, and some of the ext2 utilities. <Literal remap="tt">Remy Card</Literal> is also the
3404author 	of several helpful slides concerning the ext2 filesystem.
3405Specifically, he is the author of <Literal remap="tt">File Management in the Linux
3406Kernel</Literal> and of <Literal remap="tt">The Second Extended File System - Current
3407State, Future Development</Literal>.
3408
3409</Para>
3410</ListItem>
3411<ListItem>
3412
3413<Para>
3414	<Literal remap="tt">Wayne Davison</Literal>
3415
3416Who designed the ext2 filesystem.
3417</Para>
3418</ListItem>
3419<ListItem>
3420
3421<Para>
3422	<Literal remap="tt">Stephen Tweedie</Literal>
3423
3424Who helped designing the ext2 filesystem kernel code and wrote the
3425slides <Literal remap="tt">Optimizations in File Systems</Literal>.
3426</Para>
3427</ListItem>
3428<ListItem>
3429
3430<Para>
3431	<Literal remap="tt">Theodore Ts'o</Literal>
3432
3433Who is the author of several ext2 utilities and of the ext2 library
3434<Literal remap="tt">libext2fs</Literal> (which I didn't use, simply because I didn't know
3435it exists when I started to work on my project).
3436</Para>
3437</ListItem>
3438
3439</ItemizedList>
3440
3441</Para>
3442
3443<Para>
3444Lastly, I would like to thank, of-course, <Literal remap="tt">Linus Torvalds</Literal> and the
3445<Literal remap="tt">Linux community</Literal> for providing all of us with such a great operating
3446system.
3447</Para>
3448
3449<Para>
3450Please contact me in a case of bug report, suggestions, or just about
3451anything concerning EXT2ED.
3452</Para>
3453
3454<Para>
3455Enjoy,
3456</Para>
3457
3458<Para>
3459Gadi Oxman &lt;tgud@tochnapc2.technion.ac.il&gt;
3460</Para>
3461
3462<Para>
3463Haifa, August 95
3464</Para>
3465
3466</Sect1>
3467
3468</Article>
3469