博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
慢慢欣赏linux 截断文件
阅读量:4070 次
发布时间:2019-05-25

本文共 2105 字,大约阅读时间需要 7 分钟。

echo > /root/test.txt和truncate -s 0 /test.txt都可以截断文件。

如果文件句柄没有被关闭,仍然被别的进程占据写的话可以将文件的前半部分变成空洞。

前者在内核态行为是通过sys_open调用下来的

ret_from_syscall	=>asmlinkage long sys_open(const char __user *filename, int flags, int mode);		struct file *f = do_filp_open(dfd, tmp, flags, mode, 0);			=>struct file *do_filp_open(int dfd, const char *pathname, int open_flag, int mode, int acc_mode)				filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);					=>static struct file *do_last(struct nameidata *nd, struct path *path, int open_flag, int acc_mode, int mode, const char *pathname)						filp = finish_open(nd, open_flag, acc_mode);							=>static struct file *finish_open(struct nameidata *nd, int open_flag, int acc_mode)								will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);								if (will_truncate)									error = handle_truncate(&nd->path);										=>static int handle_truncate(struct path *path)											do_truncate(path->dentry, 0, ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, NULL);

后者是如下调用                                            

ret_from_syscall	=>static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)		do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file);			=>int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs, struct file *filp)				struct iattr newattrs;				newattrs.ia_size = length;				ret = notify_change(dentry, &newattrs);					=>int notify_change(struct dentry * dentry, struct iattr * attr)						error = inode_setattr(inode, attr);							=>int inode_setattr(struct inode * inode, struct iattr * attr)								int error = vmtruncate(inode, attr->ia_size);									=>int vmtruncate(struct inode *inode, loff_t offset)										error = inode_newsize_ok(inode, offset);										oldsize = inode->i_size;										i_size_write(inode, offset);											=>static inline void i_size_write(struct inode *inode, loff_t i_size)												inode->i_size = i_size;										truncate_pagecache(inode, oldsize, offset);										inode->i_op->truncate(inode);								mark_inode_dirty(inode);

        
linux系统编程:用truncate调整文件大小
https://www.cnblogs.com/ghostwu/archive/2018/01/11/8269220.html

你可能感兴趣的文章
归档与解归档
查看>>
Window
查看>>
为什么button在设置标题时要用一个方法,而不像lable一样直接用一个属性
查看>>
字符串的截取
查看>>
2. Add Two Numbers
查看>>
17. Letter Combinations of a Phone Number (DFS, String)
查看>>
93. Restore IP Addresses (DFS, String)
查看>>
19. Remove Nth Node From End of List (双指针)
查看>>
49. Group Anagrams (String, Map)
查看>>
139. Word Break (DP)
查看>>
23. Merge k Sorted Lists (Divide and conquer, Linked List) 以及java匿名内部类
查看>>
Tensorflow入门资料
查看>>
剑指_用两个栈实现队列
查看>>
剑指_顺时针打印矩阵
查看>>
剑指_栈的压入弹出序列
查看>>
剑指_复杂链表的复制
查看>>
服务器普通用户(非管理员账户)在自己目录下安装TensorFlow
查看>>
星环后台研发实习面经
查看>>
大数相乘不能用自带大数类型
查看>>
字节跳动后端开发一面
查看>>