7. File System ZZ

Source: Internet
Author: User

Sender: gdtyy (gdtyy), email area: embedded
Subject: Lecture 7: File System
Mailing station: shuimu community (Mon Jun 25 23:32:53 2007), within the station

*******************
* Lecture 7 file system *
*******************
Asdjf@163.com www.armecos.com

The essence of a file system is "access by name", which maps the file name and data. For example
Extract file data by file name (various images, such as GIF and BMP, HTML and CGI ). You can use various methods to achieve this goal.
As long as it can be accessed by name, it is called a file system. For example, you can use arrays to save files, use linked list struct, and use comparison
Complex and complete fat. Name ing can greatly facilitate application programming, so you do not have to manage data yourself.
Storage, all detailed operations are handled by the file system, and you only need to access them by name. File System shield underlying layer
Details, users can achieve "name-based storage" on ROM, Ram, network, hard disk, SD card, CF card, USB, and other media
". Algorithms can be simple and complex as needed. The core idea is "access by name ".
As mentioned earlier, ECOs abstracts serial ports into serial device files and interrupts into a general virtual interrupt system.
The file system mentioned here refers to the abstraction of storage devices. Storage devices use physical media such as semiconductors, magnetism, and optics
It stores all information (video, audio, image, data, etc.) in the form of "0" or "1" encoding. Use
By mounting the name of a file to a physical medium and using a file system (that is, a data structure)
Word ing.
Common storage media include Rom, Ram, hard disk, SD card, CF card, USB, and network .....
Common file systems include ramfs, romfs, fat, NTFS, NFS, ext3 ......
Some file systems are suitable for a variety of media and are more common, while others are specially designed for a certain media. File System
It can be viewed as a data organization structure. Different data structures are used on different media for different reliability and efficiency.
There is an optimal match.
The core idea of a file system is simple. However, considering speed, efficiency, capacity, simplicity, security, and reliability
Performance, scalability, concurrent access, and various storage media. The implementation of "access by name" is challenging.
ECos implements POSIX standard file and directory functions, including file system installation and uninstallation, directory operations
And file operations. The file system will have the root file name (
The file name starting with "/") points to the correct file system. When a file name starts with "/", the system
Compare the names of all valid table items in the mounted table. The table item names are compared before the occurrence of the character "/" or before the end of the string
If the file name has the longest matching table item, it is the installation table item of the file system to which the file belongs. The remaining part of the file name.
The pointer used to install table items and the root value used as the directory pointer are transmitted as parameters to the phase
Function.
For example, assume that an installation table has the following table items:
{"/", "Msdos", "/dev/hd0 ",...}
{"/FD", "msdos", "/dev/fd0 ",...}
{"/ROM", "romfs ","",...}
{"/ROM/tmp", "ramfs ","",...}
{"/Tmp", "ramfs ","",...}
{"/Dev", "devfs ","",...}
The order is: {directory name (mounting point), file system, physical device ...}
When you try to open "/ROM/YY", the file is directed to the ROM file system (romfs), while "/ROM/tmp"
Is directed to the RAM File System (ramfs ). Opening "/BAR/Bundy" will be directed to the hard disk msdos File System
(Msdos). The "/dev/tty0" operation will be directed to the device table of the device management File System (devfs ).
Lookup function. A file name without a root (a file name not starting with "/") will be directed to a file containing the current directory.
System. The current directory is expressed by a combination of an installation table and a directory pointer.

The following uses the romfs file system as an example to describe the usage of the file system. The romfs file system is the simplest.
Readers interested in file system design can read the source code. If you want to transplant the fat operating system, you only need to use romfs as the blueprint.
To modify the implementation body of the corresponding interface function.
Our example program is a general static page publishing server that can publish static pages in any directory (
Supports long file names), PDF files, images (JPG/BMP/GIF, etc.) ...... zlg the original example program can only publish one simple
The release directory cannot be changed for a single page. The program provided below can change the release directory as needed and can be used as a static Web
Server. "Linux from scratch" will be released for the sample program. The LFS Installation Guide will explain how to compile
Translate the source code package downloaded from the Internet to build a Linux system. Of course, you can also publish your own static personal homepage
Or GNU documents, or English learning documents. You can connect the Web server to the local area network.
Find Common Information on servers, which is more efficient than PC servers. If 2 M Flash is too small, consider
The release directory is attached to a 2g sd card/CF card or a G hard disk. You only need to change the mount Mount mount point.
No need to change. If you want to publish it on the Internet, you can achieve pppoe and dynamic domain names. In the release object
A.m. And B .jpg files are added to the directory to test the release of PDF and jpg files.
Let's go!
The working principle of Web server is: Whenever you click a link, ie will send a GET request "Get File Name (from
Root) HTTP/1.0 ", we only need to send back the requested file data to complete the service, ie will automatically
Each request corresponds to a TCP connection, which is not retained after transmission. GET request in progress
The file name is an absolute path. Even if the relative path is used on the page, ie will translate it into an absolute path and send it to the service.
The directory is maintained by the browser, and the server is only responsible for responding to requests without memorizing the connection information. You can add
Print the statement output request information, which is a good way to deeply understand the HTTP protocol. Therefore, the value-added ECOs package is used
It is very convenient and effective to learn various TCP/IP protocols! The program also took advantage of a reasonable debugging technique, completely missing
HTTP header. In most (not all) cases, the browser still works correctly. Although we do not advocate such violations of the Agreement
But it is indeed useful in the initial development and debugging. The relationship between file extensions and HTTP data types is more complex.
When a file is loaded from a disk, the browser must determine the document type based on the file extension and operating system.
System File Association, or both are used as the basis. When a file is downloaded from the internet, it usually has a file extension and data type.
Priority. However, the web server is not forced to provide the HTTP data type, although it is easy to cause confusion, so some
The browser derives the type based on the file extension. For other types, the default value is HTML ).
The following analysis program is mostly the same as the zlg program, which only analyzes different parts.
After reading the browser request, extract the file name with the root. If it is a "/folder, use the" folder "index.htm to replace
(Ult.htm ). Open this file. If-1 is returned, it indicates that the file does not exist.
Close the TCP connection to prepare for the next response. Otherwise, read and send the file, and repeat the process until the read is complete.
After sending. In this way, no matter how large the file is, it can also be sent with a limited buffer. It can be seen that the changes are not significant,
The file size and type are not limited. Using a file system does not complicate applications
But it becomes easier.

Romfs file systems can be implemented in two ways: 1. Using Program header files; 2. Using ROM images.
The directory I want to publish is LFS. Its structure is as follows:

Aboutdebug.htm
.
.
Index.htm
.
.
Zlib.htm
Zlib-1.htm
A.pdf
B .jpg

Use $ mk_romfs-V./LFS romfs. IMG in cygwin to create the LFS directory as a romfs File System Image
Romfs. IMG,
Use lo-B 0x81010000-r-H 192.168.0.1 romfs. IMG in Redboot to download the image to ram,
In Redboot, use the FS create-B 0x81010000-l 0x40000 romfs to burn the image in Ram
Flash,
You can view the Redboot file in the FS list to automatically allocate the image to the 0x80080000 address.
If cygnum_fs_rom_base_address is defined as 0x80080000 in the program, you can use romfs.
If you want to release another directory, you only need to create a new image and replace the romfs file in this location.
.

If you want to use the header file, you can use file2c. TCL to convert it to the C header file, as shown below:
Sh file2c. TCL romfs. IMG romfs. h
Include this header file in the C application and mount romfs to this array. However
Re-compile the program to change the release directory/file.

Redboot can guide the program to run automatically. Use fconfig to configure the Startup File, and automatically execute the application without pressing the buttons in five seconds.
Program (Web Server). If you press the key to enter Redboot, you can write it to the romfs file system. In this way,
This Static Page Server is more practical. The page will be automatically released 5 seconds after power-on. You can change the release directory and Mount
Install different storage devices for portable power saving.

Use 192.168.0.6/a.pdf to view PDF files, and use 192.168.0.6/B .jpg to view images. Actually supported
Dynamic Release with ASP, JavaScript, databases, and public networks is also possible. Let's talk about it later.

 

// This program works with IE browser

# Include <network. h>

# Include <pkgconf/system. h>
# Include <pkgconf/net. h>

# Include <Cyg/fileio. h>

# Include <Cyg/Infra/testcase. h>

# Define cygnum_fs_rom_base_address 0x80080000

Mtab_entry (romfs_mte1,
"/",
"Romfs ",
"",
(Cyg_addrword) cygnum_fs_rom_base_address );

# Ifdef cygbld_devs_eth_device_h // get the device config if it exists
# Include cygbld_devs_eth_device_h // may provide
Cygtst_devs_eth_test_net_realtime
# Endif

# Ifdef cygpkg_net_tests_use_rt_test_harness // do we use the RT test?
# Ifdef cygtst_devs_eth_test_net_realtime // get the test ancilla if it exists
# Include cygtst_devs_eth_test_net_realtime
# Endif
# Endif

# Define stack_size (cygnum_hal_stack_size_typical + 0x1000)
Static char stack [stack_size], stack1 [stack_size];
Static cyg_thread thread_data, thread_data1;
Static cyg_handle_t thread_handle, thread_handle1;

# Define buf_len 10000.
Void
Pexit (char * s)
{
Cyg_test_fail_finish (s );
}

Void
Webserver_test (struct BOOTP * bp)
{
// Struct protoent * P;
// Struct timeval TV;
Struct sockaddr_in host, client;
Int S, SA, e_source, Len;
Unsigned char Buf [buf_len];
Unsigned char * P;

Int err, FD;

Err = Mount ("", "/", "romfs ");

S = socket (af_inet, sock_stream, 0 );
If (S <0 ){
Pexit ("socket ");
Return;
}

// Set up host address
Host. sin_family = af_inet;
Host. sin_len = sizeof (host );
Host. sin_addr.s_addr = inaddr_any;
Host. sin_port = ntohs (80 );

If (BIND (S, (struct sockaddr *) & host, sizeof (host) <0 ){
Pexit ("bind/source/error ");
}

Listen (S, somaxconn );

While (true ){
// Memset (BUF, 0, sizeof (BUF ));

If (SA = accept (S, (struct sockaddr *) & client, & Len) <0 ){
Printf ("Accept error! /N ");
Continue;
}

Printf ("server: http request arrived from % s: % d/N ",
Inet_ntoa (client. sin_addr), ntohs (client. sin_port ));

Len = read (SA, Buf, sizeof (BUF ));

P = & Buf [4];
While (* P ++! = '');
* (P-1) = '/0 ';
P = & Buf [4];

Printf ("/n % s/n", P );

If (strcmp (P, "/") = 0)
{
Strcpy (P, "/index.htm ");
}

FD = open (p, o_rdonly );

If (FD =-1)
{
Close (FD );
Close (SA );
Printf ("file open error! /N ");
Continue;
}

Len = read (FD, Buf, buf_len );
While (Len! = 0 ){
Len = write (SA, Buf, Len );
Len = read (FD, Buf, buf_len );
}

Close (FD );

Close (SA );
}
}

Void
Net_test (cyg_addrword_t P)
{
Diag_printf ("start networking test.../N ");

Init_all_network_interfaces ();

# Ifdef cyghwr_net_driver_eth0
If (eth0_up ){
Cyg_thread_create (10, // priority-just a number
Webserver_test, // entry
(Cyg_addrword_t) & eth0_bootp_data, // entry Parameter
"Network TCP test", // name
& Stack1 [0], // Stack
Stack_size, // size
& Thread_handle1, // handle
& Thread_data1 // thread Data Structure
);
Cyg_thread_resume (thread_handle1); // start it
}
# Endif

}

Void
Cyg_start (void)
{
// Create a main thread, so we can run the schedpass and have time 'pass'
Cyg_thread_create (10, // priority-just a number
Net_test, // entry
0, // entry Parameter
"Network Test", // name
& Stack [0], // Stack
Stack_size, // size
& Thread_handle, // handle
& Thread_data // thread Data Structure
);

Cyg_thread_resume (thread_handle); // start it
Cyg_scheduler_start ();
}

--

※Source: · Shui Mu community http://newsmth.net · [from: 61.149.56. *]
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.