mount 函数挂载的几点说明

当我们在Linux系统的板子上挂载U盘时,会出现,原本在windows上显示正常的文件名,在Linux下全部显示为问号?.

如果需要显示出中文,需要使用uft8模式挂载U盘。

 

 解决中文字符显示问号的方法:
  mount /dev/sdb4 /mnt/ho1 -o iocharset=utf8

然而,在程序中,使用mount函数来这样挂载,却出现了问题。

首先,我们看看mount函数的定义:

int  mount(const  char  *source,  const char *target, const char *filesystemtype, unsigned long mountflags, const void *data);
source :设备 /dev/sda等;
target :挂载点/mnt/usb等;
*filesystemtype:"minix","ext2", "msdos", "proc", "nfs", "iso9660" ,“vfat”etc.;
mountflags:MS_MGC_VAL/还有别的参数;
*data:例如:”codepage=936,iocharset=cp936“。

 

其中:

mountflags 经查阅资料有如下选项:

// MS_RELATIME  //(default for Linux >= 2.6.30) 
// MS_STRICTATIME //(default for Linux < 2.6.30) 

// http://harmattan-dev.nokia.com/docs/library/html/manpages/headers/sys/mount.html
public enum MountFlags : ulong
{
    MS_RDONLY = 1,         // Mount read-only.
    MS_NOSUID = 2,         // Ignore suid and sgid bits.
    MS_NODEV = 4,         // Disallow access to device special files.
    MS_NOEXEC = 8,         // Disallow program execution.
    MS_SYNCHRONOUS = 16,    // Writes are synced at once.
    MS_REMOUNT = 32,    // Alter flags of a mounted FS.
    MS_MANDLOCK = 64,    // Allow mandatory locks on an FS.
    S_WRITE = 128,   // Write on file/directory/symlink.
    S_APPEND = 256,   // Append-only file.
    S_IMMUTABLE = 512,   // Immutable file.
    MS_NOATIME = 1024,  // Do not update access times.
    MS_NODIRATIME = 2048,  // Do not update directory access times.
    MS_BIND = 4096,  // Bind directory at different place.
}; // End Enum MountFlags : ulong

 

iocharset=name
Character set to use for converting from Unicode to ASCII. The default is to
do no conversion. Use iocharset=utf8 for UTF8 translations. This requires
CONFIG_NLS_UTF8 to be set in the kernel .config file.


 

函数返回值:

RETURN VALUES
If successful, mount() returns a value of zero. On failure, it returns -1 and sets errno to one of the following values:

EFAULT 
spec or dir is an invalid pointer.

EINVAL 
spec or dir contains an invalid character.

ENAMETOOLONG 
The length of the spec or dir parameter exceeds PATH_MAX or a path name component is longer than NAME_MAX.

ENOTDIR 
spec or dir contains an invalid character.


我在程序中使用的方法如下:

  if( mount(pTempDevName,pMyDisk[tTempEmptyOther].mountName,"vfat",0,"-o iocharset=utf8") )
    return; 

 

目前为止,使用命令行是能够成功显示中文名的,但是在程序中使用这个函数暂时未成功,仍在探索中...

 

修改成:

   if( mount(pTempDevName,pMyDisk[tTempEmptyOther].mountName,"vfat",0,"iocharset=utf8") )
    return; 

成功以utf8模式挂载,读取中文字符成功,哦耶...

 

 

 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
mount_single函数是Linux内核中与文件系统挂载相关的一个函数,其定义在fs/super.c文件中。该函数的作用是将一个新的文件系统挂载到指定的挂载点上,并返回挂载的超级块对象。 以下是mount_single函数的源代码: ```c struct super_block *mount_single(struct file_system_type *fs_type, int flags, const char *dev_name, void *data, int (*fill_super)(struct super_block *, void *, int)) { struct super_block *s; struct dentry *root; int err; s = sget(fs_type, NULL, set_anon_super, flags, NULL); if (IS_ERR(s)) return s; if (!s->s_op->read_inode) goto out; if (!(flags & MS_SILENT)) pr_debug("Mounting %s filesystem on device %s\n", s->s_type->name, dev_name ? dev_name : "(root)"); err = -ENOMEM; root = d_make_root(s); if (!root) goto out; s->s_root = root; err = fill_super(s, data, flags & MS_SILENT ? 1 : 0); if (err) goto out_put; if (!(flags & MS_SILENT)) pr_debug("Mounted %s filesystem on device %s\n", s->s_type->name, dev_name ? dev_name : "(root)"); return s; out_put: dput(root); out: deactivate_locked_super(s); return ERR_PTR(err); } ``` 下面对该函数的源代码进行详细介绍: 1. 首先,在函数的第一行中,我们可以看到函数的定义,其接受5个参数,分别是:文件系统类型、挂载标志、设备名、挂载数据、填充超级块的函数。 2. 在函数中,我们首先调用了sget函数,该函数用于获取一个已经存在的超级块对象,如果不存在,则新建一个超级块对象。如果sget函数返回的是一个错误码,则直接返回该错误码。 3. 接下来,我们判断超级块对象的操作函数中是否有read_inode函数,如果没有,则直接跳转到out标签处,释放资源并返回错误码。 4. 如果有read_inode函数,则进行文件系统的挂载操作。在挂载操作中,我们首先创建一个根目录对象,然后调用fill_super函数来填充超级块对象。如果fill_super函数返回错误码,则跳转到out_put标签处,释放资源并返回错误码。 5. 如果fill_super函数执行成功,则将根目录对象指定为超级块对象的根目录,并输出挂载成功的信息,最后返回超级块对象。 6. 如果在任何一个步骤中发生错误,则释放资源并返回错误码。 总体来说,mount_single函数的作用是将指定的文件系统挂载到指定的挂载点上,并返回挂载的超级块对象。其内部实现包括创建超级块对象、调用fill_super函数填充超级块对象、创建根目录对象等几个步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值