Libuv库(探讨)---第三节:文件系统

系列目录:

libuv概设:https://blog.csdn.net/knowledgebao/article/details/82251307

libuv异步调度:https://blog.csdn.net/knowledgebao/article/details/82251513

libuv文件系统:https://blog.csdn.net/knowledgebao/article/details/82252619

libuv网络相关:https://blog.csdn.net/knowledgebao/article/details/82252653

libuv线程相关:https://blog.csdn.net/knowledgebao/article/details/82252664

libuv进程调度:https://blog.csdn.net/knowledgebao/article/details/82252683

libuv高级调度:https://blog.csdn.net/knowledgebao/article/details/82252698

libuv其它:https://blog.csdn.net/knowledgebao/article/details/82252716

官网介绍:http://docs.libuv.org/en/v1.x/guide/filesystem.html

libuv provides a wide variety of cross-platform sync and async file system operations. All functions defined in this document take a callback, which is allowed to be NULL. If the callback is NULL the request is completed synchronously, otherwise it will be performed asynchronously.

All file operations are run on the threadpool. See Thread pool work scheduling for information on the threadpool size.

文件相关的函数以uv_fs_开头,结构统一使用uv_fs_t,文件操作和网络操作不同,网络操作是底层异步API支持非阻塞,而文件系统内部是阻塞调用,通过线程池实现异步通知。文件操作有两种模式:同步模式(回调为NULL)和异步模式。

所有操作回调可以设置为空,如果回调为空执行同步返回,一般对文件的频繁读写,需要使用同步模式。因为异步不保证内容写入的先后顺序,如果对一个文件多次写入有先后顺序要求的话,只能通过同步模式进行。文件的异步回调方式,每个函数的操作都会从线程池里关联一个线程进行操作。

API官网地址:http://docs.libuv.org/en/v1.x/fs.html

文件句柄操作相关

void uv_fs_req_cleanup(uv_fs_t* req);
uv_buf_t uv_buf_init(char* base, unsigned int len);

int uv_fs_open(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags, int mode, uv_fs_cb cb);
int uv_fs_read(uv_loop_t* loop,uv_fs_t* req,uv_file file,const uv_buf_t bufs[], unsigned int nbufs, int64_t offset,uv_fs_cb cb);
int uv_fs_write(uv_loop_t* loop,uv_fs_t* req,uv_file file,uv_buf_t bufs[],unsigned int nbufs,int64_t offset,uv_fs_cb cb);
int uv_fs_close(uv_loop_t* loop,uv_fs_t* req,uv_file file,uv_fs_cb cb);

int uv_fs_fsync(uv_loop_t* loop,uv_fs_t* req,uv_file file, uv_fs_cb cb);
int uv_fs_fdatasync(uv_loop_t* loop,uv_fs_t* req,uv_file file, uv_fs_cb cb);
int uv_fs_ftruncate(uv_loop_t* loop,uv_fs_t* req,uv_file file, int64_t offset, uv_fs_cb cb);

uv_fs_type uv_fs_get_type(const uv_fs_t*);
ssize_t uv_fs_get_result(const uv_fs_t*);
void* uv_fs_get_ptr(const uv_fs_t*);
const char* uv_fs_get_path(const uv_fs_t*);
uv_stat_t* uv_fs_get_statbuf(uv_fs_t*);

uv_fs_req_cleanup:

Cleanup request. Must be called after a request is finished to deallocate any memory libuv might have allocated.

清空请求,每次发起请求后,最后必须调用的函数,用于释放libuv可能申请的内存。

uv_buf_init:

功能:构造一个uv_buf_t结构,uv_buf_t结构含有数据和长度,一般用于uv_fs_write.

uv_fs_open:

功能:打开文件,linux下封装open,window下封装CreateFileW (window仅支持二进制模式打开)。

参数:

req:输入输出参数,req->result成功的话,返回文件句柄(>0),否则返回错误码(<0),下同

Flags,mode:unix flags,libuv在window环境之中,会自动对应到window的参数。

uv_fs_read:

功能:读多组数据,等价于preadv.

参数:

file:打开的文件句柄,bufs:数据数组(含数据和长度),nbufs:bufs个数,offset:偏移量

uv_fs_write:

功能:写多组数据,等同于pwritev.

参数:参考uv_fs_read

uv_fs_close:

功能:关闭文件,linux下封装close。

参数:参考uv_fs_open

uv_fs_fsync:刷新文件,类似flushes

uv_fs_fdatasync:同上

uv_fs_ftruncate:截取文件或者补充文件到指定大小。

uv_fs_get_type/uv_fs_get_result/uv_fs_get_ptr/uv_fs_get_path/uv_fs_get_statbuf

返回结构特uv_fs_t的各种参数,具体详见uv_fs_t

文件直接操作

int uv_fs_rename(uv_loop_t* loop, uv_fs_t* req, const char* path, const char* new_path, uv_fs_cb cb);
int uv_fs_unlink(uv_loop_t* loop,uv_fs_t* req, const char* path, uv_fs_cb cb);
int uv_fs_copyfile(uv_loop_t* loop, uv_fs_t* req, const char* path, const char* new_path, int flags, uv_fs_cb cb);
int uv_fs_sendfile(uv_loop_t* loop, uv_fs_t* req, uv_file out_fd, uv_file in_fd, int64_t in_offset, size_t length, uv_fs_cb cb);

uv_fs_rename:改名字

uv_fs_unlink:

功能:删除文件,封装的unlink(2).

参数:参考uv_fs_open

uv_fs_copyfile:拷贝文件,

参数:

flags:

UV_FS_COPYFILE_EXCL: 如果目标文件已经存在,返回UV_EEXIST,不执行。

UV_FS_COPYFILE_FICLONE: 如果目标文件存在,尝试创建copy-on-write reflink,如果平台不支持copy-on-write reflink,那就先删除,后拷贝

UV_FS_COPYFILE_FICLONE_FORCE:同上,但是,如果平台不支持copy-on-write reflink,那就返回失败

uv_fs_sendfile:等同于copyfile

目录操作相关

int uv_fs_mkdir(uv_loop_t* loop,uv_fs_t* req,const char* path,int mode,uv_fs_cb cb);
int uv_fs_mkdtemp(uv_loop_t* loop,uv_fs_t* req,const char* tpl,uv_fs_cb cb);
int uv_fs_rmdir(uv_loop_t* loop,uv_fs_t* req,const char* path,uv_fs_cb cb);
int uv_fs_scandir(uv_loop_t* loop,uv_fs_t* req,const char* path,int flags,uv_fs_cb cb);
int uv_fs_scandir_next(uv_fs_t* req, uv_dirent_t* ent);

uv_fs_mkdir:创建目录

uv_fs_mkdtemp:The result can be found as a null terminated string at req->path.

uv_fs_rmdir:删除目录

uv_fs_scandir:浏览目录文件

uv_fs_scandir_next:目录下一个文件

文件属性相关

int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb);
int uv_fs_fstat(uv_loop_t* loop,uv_fs_t* req,uv_file file,uv_fs_cb cb);
int uv_fs_lstat(uv_loop_t* loop,uv_fs_t* req,const char* path,uv_fs_cb cb);
int uv_fs_access(uv_loop_t* loop, uv_fs_t* req, const char* path, int mode, uv_fs_cb cb);
int uv_fs_utime(uv_loop_t* loop,uv_fs_t* req,const char* path,double atime,double mtime,uv_fs_cb cb);
int uv_fs_futime(uv_loop_t* loop, uv_fs_t* req, uv_file file, double atime, double mtime, uv_fs_cb cb);
int uv_fs_chmod(uv_loop_t* loop,uv_fs_t* req,const char* path,int mode,uv_fs_cb cb);
int uv_fs_fchmod(uv_loop_t* loop, uv_fs_t* req, uv_file file, int mode, uv_fs_cb cb);
int uv_fs_chown(uv_loop_t* loop,uv_fs_t* req,const char* path,uv_uid_t uid,uv_gid_t gid,uv_fs_cb cb);
int uv_fs_fchown(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_uid_t uid, uv_gid_t gid, uv_fs_cb cb);
int uv_fs_lchown(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_uid_t uid, uv_gid_t gid, uv_fs_cb cb);

uv_fs_stat:文件状态,同linux的stat

uv_fs_fstat:文件状态,同linux的fstat

uv_fs_lstat:文件状态,同linux的lstat

uv_fs_access:检测是否可用。Equivalent to access(2) on Unix. Windows uses GetFileAttributesW().

uv_fs_utime:修改文件时间

uv_fs_futime:同上

uv_fs_chmod:检测执行权限,等同于chmod

uv_fs_fchmod:检测执行权限,等同于fchmod

uv_fs_chown:更改所有者及所属组,等同于chown() 

uv_fs_fchown:更改所有者及所属组,等同于fchown() 

uv_fs_lchown:更改所有者及所属组,等同于lchown() 

文件链接相关

int uv_fs_link(uv_loop_t* loop, uv_fs_t* req, const char* path, const char* new_path, uv_fs_cb cb);
int uv_fs_symlink(uv_loop_t* loop,uv_fs_t* req,const char* path,const char* new_path,int flags,uv_fs_cb cb);
int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb);
int uv_fs_realpath(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb);

uv_fs_link:创建硬链接,等同于link

uv_fs_symlink:创建符号链接,等同于symlink

uv_fs_readlink:读取链接,结果放在req->ptr.

uv_fs_realpath:相对路径转换为绝对路径。

Equivalent to realpath(3) on Unix. Windows uses GetFinalPathNameByHandle. The resulting string is stored in req->ptr.

文件监听相关

int uv_fs_event_init(uv_loop_t* loop, uv_fs_event_t* handle);
int uv_fs_event_start(uv_fs_event_t* handle,uv_fs_event_cb cb, const char* path, unsigned int flags);
int uv_fs_event_stop(uv_fs_event_t* handle);
int uv_fs_event_getpath(uv_fs_event_t* handle, char* buffer, size_t* size);
int uv_fs_poll_init(uv_loop_t* loop, uv_fs_poll_t* handle);
int uv_fs_poll_start(uv_fs_poll_t* handle, uv_fs_poll_cb poll_cb, const char* path, unsigned int interval);
int uv_fs_poll_stop(uv_fs_poll_t* handle);
int uv_fs_poll_getpath(uv_fs_poll_t* handle, char* buffer, size_t* size);

官网API:http://docs.libuv.org/en/v1.x/fs_event.html

官网API:http://docs.libuv.org/en/v1.x/fs_poll.html

FS Poll handles allow the user to monitor a given path for changes. Unlike uv_fs_event_t, fs poll handles use stat to detect when a file has changed so they can work on file systems where fs event handles can’t.

FS Event handles allow the user to monitor a given path for changes, for example, if the file was renamed or there was a generic change in it. This handle uses the best backend for the job on each platform.

FS Event在有些系统(AIXz/OS)使用有限制,而pool不受系统影响。

uv_fs_*_init:初始化监听句柄

uv_fs_*_start:开始监听

uv_fs_*_stop:停止监听

uv_fs_*_getpath:通过监听句柄,获取监听路径。buffersize是输出参数,buffer需要提前申请好。

 

猜你喜欢

转载自blog.csdn.net/knowledgebao/article/details/82252619