⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileio.sgml

📁 eCos操作系统源码
💻 SGML
📖 第 1 页 / 共 3 页
字号:
<!-- {{{ Banner                         --><!-- =============================================================== --><!--                                                                 --><!--     fileio.sgml                                                 --><!--                                                                 --><!--     eCos Generic File I/O package documentation                 --><!--                                                                 --><!-- =============================================================== --><!-- ####COPYRIGHTBEGIN####                                          --><!--                                                                 --><!-- =============================================================== --><!-- Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.  --><!-- This material may be distributed only subject to the terms      --><!-- and conditions set forth in the Open Publication License, v1.0  --><!-- or later (the latest version is presently available at          --><!-- http://www.opencontent.org/openpub/)                            --><!-- Distribution of the work or derivative of the work in any       --><!-- standard (paper) book form is prohibited unless prior           --><!-- permission obtained from the copyright holder                   --><!-- =============================================================== --><!--                                                                 -->      <!-- ####COPYRIGHTEND####                                            --><!-- =============================================================== --><!-- #####DESCRIPTIONBEGIN####                                       --><!--                                                                 --><!-- ####DESCRIPTIONEND####                                          --><!-- =============================================================== --><!-- }}} --><part id="fileio"><title>File System Support Infrastructure</title><!-- {{{ Introduction --><chapter id="fileio-intro"><title>Introduction</title><para>This document describes the filesystem infrastructure provided ineCos. This is implemented by the FILEIO package and provides POSIXcompliant file and IO operations together with the BSD socketAPI. These APIs are described in the relevant standards and originaldocumentation and will not be described here. See <xreflinkend="posix-standard-support"> for details of which parts of thePOSIX standard are supported.</para><para>This document is concerned with the interfaces presented to clientfilesystems and network protocol stacks.</para><para>The FILEIO infrastructure consist mainly of a set of tables containingpointers to the primary interface functions of a file system. Thisapproach avoids problems of namespace pollution (for example severalfilesystems can have a function called <function>read()</function>, so long as they arestatic). The system is also structured to eliminate the need fordynamic memory allocation.</para><para>New filesystems can be written directly to the interfaces describedhere. Existing filesystems can be ported very easily by theintroduction of a thin veneer porting layer that translates FILEIOcalls into native filesystem calls. </para><para>The term filesystem should be read fairly loosely in thisdocument. Object accessed through these interfaces could equally benetwork protocol sockets, device drivers, fifos, message queues or anyother object that can present a file-like interface.</para></chapter><!-- }}} --><!-- {{{ File System Table --><chapter id="fileio-fstab"><title>File System Table</title><para>The filesystem table is an array of entries that describe eachfilesystem implementation that is part of the system image. Eachresident filesystem should export an entry to this table using the<literal>FSTAB_ENTRY()</literal> macro.</para><note><title>Note</title><para>At present we do not support dynamic addition or removal of tableentries. However, an API similar to <function>mount()</function> wouldallow new entries to be added to the table.</para></note><para>The table entries are described by the following structure:</para><programlisting>struct cyg_fstab_entry{    const char          *name;          // filesystem name    CYG_ADDRWORD        data;           // private data value    cyg_uint32          syncmode;       // synchronization mode        int     (*mount)    ( cyg_fstab_entry *fste, cyg_mtab_entry *mte );    int     (*umount)   ( cyg_mtab_entry *mte );    int     (*open)     ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,                          int mode,  cyg_file *fte );    int     (*unlink)   ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );    int     (*mkdir)    ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );    int     (*rmdir)    ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );    int     (*rename)   ( cyg_mtab_entry *mte, cyg_dir dir1, const char *name1,                          cyg_dir dir2, const char *name2 );    int     (*link)     ( cyg_mtab_entry *mte, cyg_dir dir1, const char *name1,                          cyg_dir dir2, const char *name2, int type );    int     (*opendir)  ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,                          cyg_file *fte );    int     (*chdir)    ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,                          cyg_dir *dir_out );    int     (*stat)     ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,                          struct stat *buf);    int     (*getinfo)  ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,                          int key, char *buf, int len );    int     (*setinfo)  ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,                          int key, char *buf, int len );};</programlisting><para>The <structfield>name</structfield> field points to a string thatidentifies this filesystem implementation. Typical values might be&quot;romfs&quot;, &quot;msdos&quot;, &quot;ext2&quot; etc.</para><para>The <structfield>data</structfield> field contains any private datathat the filesystem needs, perhaps the root of its data structures.</para><para>The <structfield>syncmode</structfield> field contains a description ofthe locking protocol to be used when accessing this filesystem. Itwill be described in more detail in <xref linkend="fileio-synchronization">.</para><para>The remaining fields are pointers to functions that implementfilesystem operations that apply to files and directories as wholeobjects. The operation implemented by each function should be obviousfrom the names, with a few exceptions:</para><para>The <function>opendir()</function> function pointer opens a directoryfor reading. See <xref linkend="fileio-directories"> for details.</para><para>The <function>getinfo()</function> and<function>setinfo()</function> function pointers provide support forvarious minor control and information functions such as<function>pathconf()</function> and <function>access()</function>.</para><para>With the exception of the <function>mount()</function> and<function>umount()</function> functions, all of these functionstake three standard arguments, a pointer to a mount table entry (seelater) a directory pointer (also see later) and a file name relativeto the directory. These should be used by the filesystem to locate theobject of interest.</para></chapter><!-- }}} --><!-- {{{ Mount Table --><chapter id="fileio-mount-table"><title>Mount Table</title><para>The mount table records the filesystems that are actually active.These can be seen as being analogous to mount points in Unix systems.</para><para>There are two sources of mount table entries. Filesystems (or othercomponents) may export static entries to the table using the<literal>MTAB_ENTRY()</literal> macro. Alternatively, new entries maybe installed at run time using the <function>mount()</function>function. Both types of entry may be unmounted with the<function>umount()</function> function.</para><para>A mount table entry has the following structure:</para><programlisting>struct cyg_mtab_entry{    const char          *name;          // name of mount point    const char          *fsname;        // name of implementing filesystem    const char          *devname;       // name of hardware device    CYG_ADDRWORD        data;           // private data value    cyg_bool            valid;          // Valid entry?    cyg_fstab_entry     *fs;            // pointer to fstab entry    cyg_dir             root;           // root directory pointer};</programlisting><para>The <structfield>name</structfield> field identifies the mountpoint. This is used to direct rooted filenames (filenames thatbegin with &quot;/&quot;) to the correct filesystem. When a filename that begins with &quot;/&quot; is submitted, it is matchedagainst the <structfield>name</structfield> fields of all valid mounttable entries. The entry that yields the longest match terminatingbefore a &quot;/&quot;, or end of string, wins and the appropriatefunction from the filesystem table entry is then passed the remainderof the file name together with a pointer to the table entry and thevalue of the <structfield>root</structfield> field as the directorypointer.</para><para>For example, consider a mount table that contains the followingentries:</para><programlisting>	{ "/",    "msdos", "/dev/hd0", ... }	{ "/fd",  "msdos", "/dev/fd0", ... }	{ "/rom", "romfs", "", ... }	{ "/tmp", "ramfs", "", ... }	{ "/dev", "devfs", "", ... }</programlisting><para>An attempt to open &quot;/tmp/foo&quot; would be directed to the RAMfilesystem while an open of &quot;/bar/bundy&quot; would be directedto the hard disc MSDOS filesystem. Opening &quot;/dev/tty0&quot; wouldbe directed to the device management filesystem for lookup in thedevice table.</para><para>Unrooted file names (those that do not begin with a '/') are passedstraight to the filesystem that contains the current directory. Thecurrent directory is represented by a pair consisting of a mount tableentry and a directory pointer.</para><para>The <structfield>fsname</structfield> field points to a string thatshould match the <structfield>name</structfield> field of theimplementing filesystem. During initialization the mount table isscanned and the <structfield>fsname</structfield> entries looked up inthe filesystem table. For each match, the filesystem's _mount_function is called and if successful the mount table entry is markedas valid and the <structfield>fs</structfield> pointer installed.</para><para>The <structfield>devname</structfield> field contains the name of thedevice that this filesystem is to use. This may match an entry in thedevice table (see later) or may be a string that is specific to thefilesystem if it has its own internal device drivers.</para><para>The <structfield>data</structfield> field is a private data value. Thismay be installed either statically when the table entry is defined, ormay be installed during the <function>mount()</function> operation.</para><para>The <structfield>valid</structfield> field indicates whether this mountpoint has actually been mounted successfully. Entries with a false<structfield>valid</structfield> field are ignored when searching for aname match.</para><para>The <structfield>fs</structfield> field is installed after a successful<function>mount()</function> operation to point to the implementingfilesystem.</para><para>The <structfield>root</structfield> field contains a directory pointervalue that the filesystem can interpret as the root of its directorytree. This is passed as the <parameter>dir</parameter> argument offilesystem functions that operate on rooted filenames. This field mustbe initialized by the filesystem's <function>mount()</function>function.</para></chapter><!-- }}} --><!-- {{{ File Table --><chapter id="fileio-file-table"><title>File Table</title><para>Once a file has been opened it is represented by an open fileobject. These are allocated from an array of available fileobjects. User code accesses these open file objects via a second arrayof pointers which is indexed by small integer offsets. This gives theusual Unix file descriptor functionality, complete with the variousduplication mechanisms.</para><para>A file table entry has the following structure:</para><programlisting>struct CYG_FILE_TAG{    cyg_uint32	                f_flag;		/* file state                   */    cyg_uint16                  f_ucount;       /* use count                    */    cyg_uint16                  f_type;		/* descriptor type              */    cyg_uint32                  f_syncmode;     /* synchronization protocol     */    struct CYG_FILEOPS_TAG      *f_ops;         /* file operations              */    off_t       	        f_offset;       /* current offset               */    CYG_ADDRWORD	        f_data;		/* file or socket               */    CYG_ADDRWORD                f_xops;         /* extra type specific ops      */    cyg_mtab_entry              *f_mte;         /* mount table entry            */};</programlisting><para>The <structfield>f_flag</structfield> field contains some FILEIOcontrol bits and some bits propagated from the<parameter>flags</parameter> argument of the<function>open()</function> call (defined by<literal>CYG_FILE_MODE_MASK</literal>).</para><para>The <structfield>f_ucount</structfield> field contains a use count thatcontrols when a file will be closed. Each duplicate in the filedescriptor array counts for one reference here. It is alsoincremented around each I/O operation to ensure that the file cannotbe closed while it has current I/O operations.</para><para>The <structfield>f_type</structfield> field indicates the type of theunderlying file object. Some of the possible values here are<literal>CYG_FILE_TYPE_FILE</literal>,<literal>CYG_FILE_TYPE_SOCKET</literal> or <literal>CYG_FILE_TYPE_DEVICE</literal>.</para><para>The <structfield>f_syncmode</structfield> field is copied from the<structfield>syncmode</structfield> field of the implementingfilesystem. Its use is described in <xref linkend="fileio-synchronization">.</para><para>The <structfield>f_offset</structfield> field records the current fileposition. It is the responsibility of the file operation functions tokeep this field up to date.</para><para>The <structfield>f_data</structfield> field contains private dataplaced here by the underlying filesystem. Normally this will be apointer to, or handle on, the filesystem object that implements thisfile.</para><para>The <structfield>f_xops</structfield> field contains a pointer to anyextra type specific operation functions. For example, the socket I/Osystem installs a pointer to a table of functions that implement thestandard socket operations.</para><para>The <structfield>f_mte</structfield> field contains a pointer to theparent mount table entry for this file. It is used mainly to implementthe synchronization protocol. This may contain a pointer to some otherdata structure in file objects not derived from a filesystem.</para>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -