symbian os:文件,目录,分区 .目录操作经常用到的API

来源:互联网 发布:万方期刊论文数据库 编辑:程序博客网 时间:2024/05/16 13:57

在Symbian OS中,Flash(闪存)通常被定义为C盘。另外,在Symbian智能手机中还有一个ROM存储器,通常被映射为Z盘,用户的许多文件存放在Z盘中,其他可移动存储器(如存储卡等)则映射为d,e等盘符。
一个完整的Symbian OS文件名包括以下四部分:
1. 驱动器名,即盘符。Symbian以\symbian\8.1a\S60_2nd_FP3\作为根目录的起点。
2. 路径。
3. 文件名。
4. 文件扩展名。
一个完整的文件名不能超过255个字符。Symbian OS中提供了TFileName类来存储文件名。
const TInt KMaxFileName=0x100;
typedef TBuf<KMaxFileName> TFileName;

应用程序要对文件进行读写,必须使用文件服务器提供的各种读写函数。在使用这些函数前,必须建立应用程序与文件服务器的连接,这种连接称为会话(session)。Symbian OS提供了RFs类,该类的Connect()/Close()函数用来建立和关闭连接。应用程序和文件服务器建立连接的具体过程如下:
1. 定义一个RFs对象iFs。
2. 调用Connect()建立连接 User::LeaveIfError(iFs.Connect());
3. 当文件读写完成后,调用iFs.Close()关闭连接,结束会话并释放相关资源。

常用的驱动器操作:
首先,熟悉一下一些结构/类的定义
const TInt KMaxDrives=26;
typedef TBuf8<KMaxDrives> TDriveList;
const TInt KMaxDriveName=0x02;
typedef TBuf<KMaxDriveName> TDriveName;

enum TDriveNumber
{
          EDriveA,       EDriveB,       EDriveC,       EDriveD,       EDriveE,
          EDriveF,       EDriveG,       EDriveH,       EDriveI,       EDriveJ,
          EDriveK,       EDriveL,       EDriveM,       EDriveN,       EDriveO,
          EDriveP,       EDriveQ,       EDriveR,       EDriveS,       EDriveT,
          EDriveU,       EDriveV,       EDriveW,       EDriveX,       EDriveY,
          EDriveZ
};

enum TEntryKey
{
          ESortNone=0,ESortByName,ESortByExt,ESortBySize,ESortByDate,ESortByUid,
          EDirsAnyOrder=0,EDirsFirst=0x100,EDirsLast=0x200,
          EAscending=0,EDescending=0x400,EDirDescending=0x800
};

enum TFileMode
{
          EFileShareExclusive,EFileShareReadersOnly,EFileShareAny,
          EFileStream=0,EFileStreamText=0x100,
          EFileRead=0,EFileWrite=0x200
};

class TDriveInfo
{
public:
          TMediaType iType; // Whether the drive supports a battery and if so, its state
          TBatteryState iBattery; // The drive attributes. For more information seeKDriveAttLocal and other drive attributes.

          /* The attributes of the media mounted on the drive. Replaced with: For more information seeKMediaAttVariableSize and other media attributes.*/
          TUint iDriveAtt;
          TUint iMediaAtt; // The type of media mounted on the drive.
};

class TVolumeInfo
{
public:
           IMPORT_C TVolumeInfo();
          TDriveInfo iDrive;
          TUint iUniqueID;
          TInt64 iSize;
          TInt64 iFree;
          TBufC<KMaxFileName> iName;
};

RFs中与驱动器操作相关的方法:
TInt DriveList(TDriveList& aList) const;
Gets a list of the available drives. The drive list consists of an array of 26 bytes. Array index zero corresponds to drive A, one equals B etc. Each byte with a non zero value signifies that the corresponding drive contains recognised media.
TInt Drive(TDriveInfo& anInfo,TInt aDrive=KDefaultDrive) const;
Gets information about a drive and the medium mounted on it.
TInt Volume(TVolumeInfo& aVol,TInt aDrive=KDefaultDrive) const;
Gets volume information for a formatted device. This function provides additional information to that given by Drive(), including the volume label, if set, and the amount of free space on the disk
TInt GetDriveName(TInt aDrive,TDes& aDriveName) const;
Gets the name of a drive. Drive naming is optional. If the drive specified has not been assigned a name, this function returns a descriptor whose length is zero.
TInt SetDriveName(TInt aDrive,const TDesC& aDriveName);
Sets the name of a drive. Drive naming is optional. Any drive may be assigned a name and more than one drive may share the same name.
TInt SetVolumeLabel(const TDesC& aName,TInt aDrive=KDefaultDrive);
Sets the label for a volume.
  static TBool IsValidDrive(TInt aDrive);
Tests whether a drive number is valid. A valid drive number is any number between 0 and 25 inclusive.
static TInt CharToDrive(TChar aChar,TInt& aDrive);
Maps a drive character to the corresponding number. The drive character must be in the range A to Z. For example, drive A corresponds to zero, drive B corresponds to 1. For the drive number enumeration, see TDriveNumber.
static TInt DriveToChar(TInt aDrive,TChar& aChar);
Maps a drive number to the corresponding character. The drive number must be in the range 0 to 25. For example, drive number zero (EDriveA) corresponds to drive A, one (EDriveB) corresponds to drive B. For the drive number enumeration, see TDriveNumber.
TInt LockDrive(TInt aDrv, const TMediaPassword& aOld, const TMediaPassword& aNew, TBool aStr);
Locks the card in the specified drive.The function locks an unlocked card and sets the password. An existing password can be changed.The new password must be added to the controller's password store so that the controller can subsequently issue the password without the user having to be prompted for it again.
TInt UnlockDrive(TInt aDrv, const TMediaPassword& Pswd, TBool aStr);
Unlocks the card in the specified drive.The password must be added to the controller's password store so that the controller can subsequently issue the password without the user having to be prompted for it again.

目录操作主要包括建立一个目录或目录结构、访问目录的属性等。在进行目录操作之前仍然先与文件服务器进行连接。

RFs中常用的文件/目录操作方法如下:

TInt MkDir(const TDesC& aPath);

Makes a directory. It should be a sub-directory of an existing directory and its name should be unique within its parent directory, otherwise the function returns an error.

TInt MkDirAll(const TDesC& aPath);

Makes one or more directories. Any valid path component specified in the specified path which does not already exist is created as a directory.

TInt RmDir(const TDesC& aPath);

Note that if a filename is specified in the argument, it is ignored. Therefore, there should be a trailing backslash after the final directory name in the argument to indicate that it is a directory, not a filename.

TInt Delete(const TDesC& aName);

Deletes a single file. Wildcards are not allowed in either the file name or the extension, otherwise an error is returned.

TInt Rename(const TDesC& anOldName,const TDesC& aNewName);

Renames a single file or directory. It can also be used to move a file or directory by specifying different destination and source directories. If so, the destination and source directories must be on the same drive. If a directory is moved, then the directory structure beneath it is also moved.

If a directory specified by aNewName is different from one specified byanOldName, then the file or directory is moved to the new directory, see example below. The file or directory may not be moved to another device by this means, either explicitly (by another drive specified in the name) or implicitly (because the directory has been mapped to another device withSetSubst()).

TInt Replace(const TDesC& anOldName,const TDesC& aNewName);

Replaces a single file with another. This function does not support the use of wildcards. UnlikeRename(), it only applies to files.

TInt Att(const TDesC& aName,TUint& aAttValue) const;

Returns a file's attributes.

TInt SetAtt(const TDesC& aName,TUint aSetAttMask,TUint aClearAttMask);

Sets or clears the attributes of a single file.The function uses two bitmasks. The first bitmask specifies the attributes to set. The second specifies the attributes to clear.

TInt Modified(const TDesC& aName,TTime& aTime) const;

Gets the last modification date and time of a file or directory. If there has been no modification, the function gets the date and time of the file or directory's creation.

TInt Entry(const TDesC& aName,TEntry& anEntry) const;

Gets the entry details, including UID information, for a file or directory.

TInt SetModified(const TDesC& aName,const TTime& aTime);

Sets the date and time that the contents of a file or directory were modified.

TInt SetEntry(const TDesC& aName,const TTime& aTime,TUint aSetAttMask,TUint aClearAttMask);

Sets both the attributes and the last modified date and time for a file or directory.The function uses two bitmasks. The first bitmask determines which attributes should be set. The second bitmask determines which are cleared.

TInt RealName(const TDesC& aName,TDes& aResult) const;

Gets the real name of a file.This is used in circumstances where a file system needs to mangle Symbian OS natural names so that it can store them on that file system.

TInt GetDir(const TDesC& aName,TUint anEntryAttMask,TUint anEntrySortKey, CDir*& anEntryList) const;

Gets a filtered list of a directory's contents. The bitmask determines which file and directory entry types should be listed. The sort key determines the order in which they are listed.

TInt GetDir(const TDesC& aName,TUint anEntryAttMask,TUint anEntrySortKey, CDir*& anEntryList,CDir*& aDirList) const;

Gets a filtered list of the directory and file entries contained in a directory and a list of the directory entries only. The bitmask determines which file and directory entry types should be listed inanEntryList. The contents of the second list,aDirList are not affected by the bitmask; it returns all directory entries contained in directoryaName. The sort key determines the order in which both lists are sorted.

TInt GetDir(const TDesC& aName,const TUidType& anEntryUid,TUint anEntrySortKey, CDir*& aFileList) const;

Gets a filtered list of a directory's contents by UID.The TUidType parameter determines which file entry types should be listed. The sort key determines the order in which they are listed.

TInt DefaultPath(TDes& aPath) const;

Gets the system default path. This path is assigned as the session path to new clients when they connect to the file server. There is a single system default path rather than one for each drive.

TInt SetDefaultPath(const TDesC& aPath);

Sets the system default path. This path is assigned as the session path to new clients when they connect to the file server. The session paths of existing clients remain unchanged. There is a single system default path rather than one for each drive.

TInt SessionPath(TDes& aPath) const;

Gets the session path. When a client connects to the file server, its session path is initialised to the system default path. The session path of an existing client can only be changed bySetSessionPath().

TInt SetSessionPath(const TDesC& aPath);

Sets the session path for the current file server client. When the client first connects to the file server, its session path is initialised to the system default path.

******************************************************************************************

RFs

概述

提供了对文件系统的应用程序接口。

在Symbian中文件的存取是通过Symbian OS文件服务器提供的。服务器提供了本地的文件系统(ROM、RAM和移动存储)。驱动器、目录和文件的层次关系是VFAT(虚拟文件分配表),因此这种文件系统很自然的与PC相兼容。流存储定义了存储的API,包含了文件的接口,通常直接使用存储API代替文件API。

文件服务器相关的API包含了7个关键的概念:文件服务器会话、文件、目录、单元、路径和文件名分析器、文件管理、文件查找。

文件服务器会话:从客户端到服务器的所有访问都是通过文件服务器会话。它提供了文件系统处理函数,包括添加、删除、移动和重命名文件;检查和改变文件属性;请求通知。RFs提供了文件服务器会话。

文件:文件能够打开,创建或替代。在文件读或写之前必须先打开。在读或写文件时,数据是经过描述符进行传递。文件也能够通过文件存储API提供的文件流类进行读或写。RFile提供了文件接口。

目录项:目录项可以是目录、文件或卷标。目录项可以读取或排序。从一个目录读取一条目录项,使用RDir。同时读取多条目录项,使用RFs函数和相关的CDir。目录项类型是TEntry。

单元:单元是一个可能有驱动器的驱动器设备。对于移动介质,单元是一直存在的,但驱动器设备不一定存在。RFs提供了查找单元上驱动器是否存在的方法,驱动器的信息被包装在TDriveUnit、TDriveInfo和TVolumeInfo中。

路径和文件名分析器:一个文件名包括逻辑驱动器、路径、名称和扩展名。TParase提供了文件名的操作接口。

文件管理:CFileMan提供了大量文件拷贝、移动和其他文件管理的高效操作。

文件查找:TFindFile提供了高级别的文件查找操作。
概述

RFs类包装了与文件服务器的会话的句柄,通过RFs类型可以访问文件服务器。每一个连接到文件服务器的RFs都要求消耗一定服务器端资源和系统资源。因此,应该尽可能减少与文件服务器会话的数量。RFs类不允许用户继承。

RFs类提供了全部的文件系统服务,包括:

1、添加、删除、移动和重命名文件以及目录

2、检查和改变文件属性以及目录项信息。这包括文件和目录最后修改的时间日期、大小,以及文件只读、隐藏、存档或系统的属性标记。

3、可获得目录列表。

4、维护了一个单一系统的缺省路径,而不像其他一些系统,每一个驱动器都用一个缺省路径。

5、使用TParse对象可分析文件名。

6、可获得驱动器和卷标的信息。

7、格式化和标注卷标。

8、可获得有效的驱动器列表、

9、仿效DOS的subst命令,将路径与驱动器号关联,允许任意一个目录当作一个磁盘驱动器来看。

10、当有重要的改变发生时,发出请求。用于当程序维护了一个文件列表,当系统中的文件发生改变时,必须发出请求更新这些列表。

11、查找文件服务器的版本号。

12、确保了当会话结束时,所以的资源都将关闭。

RFs API

1、Connect()、Close()

IMPORT_C TInt Connect(TInt aMessageSlots=KFileServerDefaultMessageSlots);

连接一个与文件服务器的会话。在使用文件服务器之前,必须连接服务器会话。对于UI应用程序,控制环境(CONE)是组成程序框架的一部分,它提供了文件服务器会话的永久句柄,可以通过iCoeEnv上调用FsSession()来获得句柄的引用,而不用再连接新的会话。

IMPORT_C void Close();

关闭文件服务器会话句柄。如果句柄被多个对象所引用,在关闭前要确保其他对象不在使用。如果句柄没有通过Close()关闭,那么文件服务器会话将在它所在的线程结束时自动关闭。

2、SessionPath()、SetSessionPath()

IMPORT_C TInt SessionPath(TDes &aPath) const;

获得当前的会话路径。当客户端连接到文件服务器上后,会话路径就初始化为系统的缺省路径。

IMPORT_C TInt SetSessionPath(const TDesC &aPath);

重新设置会话路径,路径中不能包含文件名。

3、GetDir()

IMPORT_C TInt GetDir(const TDesC &aName, TUint anEntryAttMask,

TUint anEntrySortKey, CDir *&anEntryList) const;

获得经筛选后的文件和目录项列表。anEntryAttMask决定了目录项筛选的方法,anEntrySortKey决定了目录项的排序规则,anEntryList引用了文件和目录项列表的指针。

IMPORT_C TInt GetDir(const TDesC &aName, TUint anEntryAttMask,

TUint anEntrySortKey, CDir *&anEntryList,

CDir *&aDirList) const;

获得一个经筛选后的文件和目录项列表和一个不筛选的只含目录项列表。anEntryAttMask决定了第一个列表筛选的方法,anEntrySortKey决定了两个列表的排序规则,anEntryList引用了文件和目录项列表的指针,aDirList引用了目录项列表的指针。

4、MkDir()、MkDirAll()、RmDir()、Delete()

IMPORT_C TInt MkDir(const TDesC &aPath);

IMPORT_C TInt MkDirAll(const TDesC &aPath);

创建新的目录项,MkDir()的所有上级目录必须存在,MkDirAll()将创建所有不存在的上级目录。

IMPORT_C TInt RmDir(const TDesC &aPath);

删除指定目录,指定的目录必须为空。

IMPORT_C TInt Delete(const TDesC &aName);

删除指定的文件。不能使用通配符删除文件。

5、Att()、SetAtt()

IMPORT_C TInt Att(const TDesC &aName, TUint &aAttValue) const;

获得指定文件的属性。aAttValue的二进制数的每一位表示一种属性。

IMPORT_C TInt SetAtt(const TDesC &aName, TUint aSetAttMask,

TUint aClearAttMask);

设置或清除指定文件的属性。aSetAttMask为设置的属性值,aClearAttMask为清除的属性值。

6、Modified()、SetModified()

IMPORT_C TInt Modified(const TDesC &aName, TTime &aTime) const;

获得指定文件或目录的最后修改时间日期,以UTC(协调世界时)表示。

IMPORT_C TInt SetModified(const TDesC &aName, const TTime &aTime);

设置指定文件或目录的最后修改时间日期,以UTC(协调世界时)表示。

7、Entry()、SetEntry()

IMPORT_C TInt Entry(const TDesC &aName, TEntry &anEntry) const;

获得指定文件或目录项的详细信息。anEntry为详细信息的引用。

IMPORT_C TInt SetEntry(const TDesC &aName, const TTime &aTime,

TUint aSetAttMask, TUint aClearAttMask);

同时设置文件或目录项的属性和最后修改时间。aTime为新的修改时间,aSetAttMask为设置的属性值,aClearAttMask为清除的属性值。

8、Replace()和Rename()

IMPORT_C TInt Replace(const TDesC &anOldName, const TDesC &aNewName);

将anOldName路径下的文件移到aNewName路径下。若aNewName路径下有同名文件,那么将覆盖这个文件。文件名中不能带通配符。

IMPORT_C TInt Rename(const TDesC &anOldName, const TDesC &aNewName);

当源路径和目的路径相同时,为重新命名文件或目录。当源路径和目的路径不同时,为移动文件或目录,不支持对已有文件和目录的覆盖。移动目录时,目录下所以内容将一起移动。路径中不能带有通配符。

9、DriveList()、DriveToChar()、CharToDrive()、Drive()、Volume()

IMPORT_C TInt DriveList(TDriveList &aList) const;

获得一个可用的驱动器列表。列表包含26项,第0项对应驱动器A、第1项对应驱动器B,以此类推。某一项的值为非零时,表示对应的驱动器可用。

static IMPORT_C TInt DriveToChar(TInt aDrive, TChar &aChar);

将驱动器数字表示映射到传统的驱动器字母表示。

static IMPORT_C TInt CharToDrive(TChar aChar, TInt &aDrive);

将传统的驱动器字母表示映射到驱动器数字表示。

IMPORT_C TInt Drive(TDriveInfo &anInfo, TInt aDrive=KDefaultDrive) const;

获得指定驱动器的信息。

IMPORT_C TInt Volume(TVolumeInfo &aVol, TInt aDrive=KDefaultDrive) const;

获得指定卷标的信息。

10、Parse()

IMPORT_C TInt Parse(const TDesC &aName, TParse &aParse) const;

IMPORT_C TInt Parse(const TDesC &aName, const TDesC &aRelated,

TParse &aParse) const;

分析指定的文件名称信息。

11、NotifyChange()、NotifyChangeCancel()、SetNotifyChange()

IMPORT_C void NotifyChange(TNotifyType aType, TRequestStatus &aStat);

IMPORT_C void NotifyChange(TNotifyType aType, TRequestStatus &aStat,

const TDesC &aPathName);

IMPORT_C void NotifyChangeCancel();

IMPORT_C void NotifyChangeCancel(TRequestStatus &aStat);

IMPORT_C TInt SetNotifyChange(TBool aNotifyChange);

12、NotifyDiskSpace()、NotifyDiskSpaceCancel()

IMPORT_C void NotifyDiskSpace(TInt64 aThreshold, TInt aDrive,

TRequestStatus &aStat);

IMPORT_C void NotifyDiskSpaceCancel(TRequestStatus &aStat);

IMPORT_C void NotifyDiskSpaceCancel();


原创粉丝点击