显示标签为“linux”的博文。显示所有博文
显示标签为“linux”的博文。显示所有博文

2009年11月29日星期日

一个c语言小技巧,摘自minix fs

我说的小技巧就是最后的那句 err |= minix_sync_inode(inode);

真是懒的可以,Linus真是惜行如jing啊, 前面的错误检测和后面的都一并检查了,不过这里也有一个前提,就是 sync_mapping_buffers错误了,调用minix_sync_inode也不会导致更严重的问题。

学习学习。。。




int minix_sync_file(struct file * file,
struct dentry *dentry, int datasync)
{
struct inode *inode = dentry->d_inode;
int err;

err = sync_mapping_buffers(inode->i_mapping);
if (!(inode->i_state & I_DIRTY))
return err;
if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
return err;

err |= minix_sync_inode(inode);
return err ? -EIO : 0;
}

2009年5月6日星期三

Linux hard link

You may confused what the hard link is, and why the delete file use the unlink(2) system call. this example will help you somehow.


assumption, there is a file in /tmp/a.txt

$ cat /tmp/a.txt
hello, i'm a.txt

then, we write this test.c
test.c:
#include
int main(void)
{
link("/tmp/a.txt", "/tmp/new_a.txt");
/* in this moment, the a.txt have same content with new_a.txt */
unlink("/tmp/a.txt");
/* in this moment, a.txt will be delete */
}

we compile the test.c
$gcc test.c

and run it
$./a.out

$ cat /tmp/new_a.txt
hello, i'm a.txt

Use hard link is the simplest and the fastest way move files within the same filesystem, since hard link cann't across filesystem.

More infomation you need check the link(2) or the unix filesystem design.