前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >minix文件系统源码分析之symlink.c(基于linux1.2.13)

minix文件系统源码分析之symlink.c(基于linux1.2.13)

作者头像
theanarkh
发布2019-07-30 18:49:18
1K0
发布2019-07-30 18:49:18
举报
文章被收录于专栏:原创分享原创分享

该文件是实现软链接相关的功能。我们可以了解到软链接的实现原理。

代码语言:javascript
复制
/*
 *  linux/fs/minix/symlink.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *
 *  minix symlink handling code
 */

#ifdef MODULE
#include <linux/module.h>
#endif

#include <asm/segment.h>

#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/minix_fs.h>
#include <linux/stat.h>

static int minix_readlink(struct inode *, char *, int);
static int minix_follow_link(struct inode *, struct inode *, int, int, struct inode **);

/*
 * symlinks can't do much...
 */
// 操作软链接文件的函数集,在新建软链接文件的时候赋值给inode结构体
struct inode_operations minix_symlink_inode_operations = {
  NULL,      /* no file-operations */
  NULL,      /* create */
  NULL,      /* lookup */
  NULL,      /* link */
  NULL,      /* unlink */
  NULL,      /* symlink */
  NULL,      /* mkdir */
  NULL,      /* rmdir */
  NULL,      /* mknod */
  NULL,      /* rename */
  minix_readlink,    /* readlink */
  minix_follow_link,  /* follow_link */
  NULL,      /* bmap */
  NULL,      /* truncate */
  NULL      /* permission */
};
// 打开软链对应的文件
static int minix_follow_link(struct inode * dir, struct inode * inode,
  int flag, int mode, struct inode ** res_inode)
{
  int error;
  struct buffer_head * bh;

  *res_inode = NULL;
  if (!dir) {
    dir = current->fs->root;
    dir->i_count++;
  }
  if (!inode) {
    iput(dir);
    return -ENOENT;
  }
  if (!S_ISLNK(inode->i_mode)) {
    iput(dir);
    *res_inode = inode;
    return 0;
  }
  if (current->link_count > 5) {
    iput(inode);
    iput(dir);
    return -ELOOP;
  }
  // 读取文件第一块内容
  if (!(bh = minix_bread(inode, 0, 0))) {
    iput(inode);
    iput(dir);
    return -EIO;
  }
  iput(inode);
  current->link_count++;
  // 打开b_data里的保存的文件名对应的文件
  error = open_namei(bh->b_data,flag,mode,res_inode,dir);
  current->link_count--;
  brelse(bh);
  return error;
}
// 读取软链文件的内容,即文件路径
static int minix_readlink(struct inode * inode, char * buffer, int buflen)
{
  struct buffer_head * bh;
  int i;
  char c;

  if (!S_ISLNK(inode->i_mode)) {
    iput(inode);
    return -EINVAL;
  }
  if (buflen > 1023)
    buflen = 1023;
  bh = minix_bread(inode, 0, 0);
  iput(inode);
  if (!bh)
    return 0;
  i = 0;
  while (i<buflen && (c = bh->b_data[i])) {
    i++;
    put_fs_byte(c,buffer++);
  }
  brelse(bh);
  return i;
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-07-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 编程杂技 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档