📄 dir.html
字号:
组 ID 为所在目录的组 ID, 否则为进程的有效组 ID.===============================================================================</PRE> <LI>用 access 函数可获得用户对文件的访问许可</LI><PRE>=============================================================================== #include <unistd.h> int access(const char *pathname, int mode);===============================================================================</PRE> <LI>mode 的值决定了 access 所测试的许可, 值可取 R_OK, W_OK, X_OK 和 F_OK 的 "或" 组合</LI> <LI>umask 设置进程创建文件的许可掩码</LI><PRE>=============================================================================== #include <sys/types.h> #include <sys/stat.h> mode_t umask(mode_t mask);===============================================================================</PRE> <LI>实际的 umask 值为 mask & 0777</LI> <LI>umask 返回先前的 umask 值, 没有错误返回</LI> <LI>chmod/fchmod, chmod 不对符号链接进行操作</LI><PRE>=============================================================================== #include <sys/types.h> #include <sys/stat.h> int chmod(const char *path, mode_t mode); int fchmod(int fildes, mode_t mode);===============================================================================</PRE> <LI>黏着位 (sticky bit)</LI><PRE>=============================================================================== * 保存交换后的可执行正文, 以便节省装入时间. 该标志已经没有多少实际意义. * 对普通文件没有任何效果. * 对目录, 作为限制删除标志, 用来限制目录项的删除和重命名.===============================================================================</PRE> <LI>chown/fchown/lchown</LI><PRE>=============================================================================== #include <sys/types.h> #include <unistd.h> int chown(const char *path, uid_t owner, gid_t group); int fchown(int fd, uid_t owner, gid_t group); int lchown(const char *path, uid_t owner, gid_t group);===============================================================================</PRE></UL><H3>5.4.4 目录及目录项操作</H3><UL> <LI>truncate/ftruncate: 截短文件</LI><PRE>=============================================================================== #include <unistd.h> int truncate(const char *path, off_t length); int ftruncate(int fd, off_t length);===============================================================================</PRE> <LI>打开文件时用 O_TRUNC 可清空文件数据</LI> <LI>link/unlink: 建立指向文件的新目录项/删除目录项或文件</LI><PRE>=============================================================================== #include <unistd.h> int link (const char *oldpath, const char *newpath); int unlink (const char *pathname);-------------------------------------------------------------------------------unlink 成功的先决条件: * 作用于非目录文件. * 对包含该目录项的目录, 必须具有写和执行许可. * 若目录具有黏着位, 则必须满足: 拥有文件, 拥有目录或具有超级用户特权===============================================================================</PRE> <LI>mkdir/rmdir</LI><PRE>=============================================================================== #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <unistd.h> int mkdir (const char *pathname, mode_t mode); int rmdir (const char *pathname);------------------------------------------------------------------------------- * rmdir 只能删除空目录.===============================================================================</PRE> <LI>remove/rename: 删除/重命名</LI><PRE>=============================================================================== #include <stdio.h> int remove(const char *pathname); int rename(const char *oldpath, const char *newpath);------------------------------------------------------------------------------- * remove 可删除文件和目录, 分别等同于 unlink 和 rmdir.===============================================================================</PRE> <LI>symlink/readlink: 建立/读取符号链接</LI><PRE>=============================================================================== #include <unistd.h> int symlink (const char *oldpath, const char *newpath); int readlink (const char *path, char *buf, size_t bufsiz);------------------------------------------------------------------------------- * 建立符号链接时, 不要求 oldpath 一定存在. * open 沿链接前行, readlink 可读取链接本身的信息.===============================================================================</PRE> <LI>chdir/fchdir/getcwd: 改变/获取当前工作目录</LI><PRE>=============================================================================== #include <unistd.h> int chdir(const char *path); int fchdir(int fd); char *getcwd(char *buf, size_t size); char *get_current_dir_name(void); char *getwd(char *buf);------------------------------------------------------------------------------- * 内核维护的当前目录信息是索引节点而不是完整路径名. * get_current_dir_name 是 GNU 的扩展. * getwd 是 BSD 扩展===============================================================================</PRE> <LI>utime: 修改文件的访问和更新时间</LI><PRE>=============================================================================== #include <sys/types.h> #include <utime.h> int utime (const char *filename, struct utimbuf *buf); #include <sys/time.h> int utimes (char *filename, struct timeval *tvp);------------------------------------------------------------------------------- struct utimbuf { time_t actime; /* access time */ time_t modtime; /* modification time */ }; struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ };------------------------------------------------------------------------------- * utime 返回的时间为日历时间, 即 1970 年 1 月 1 日 0:00:00 (UTC) 开始的秒数. * 对 utimes 必须传递一个两元素的 struct timeval 数组, 分别接受访问时间和 修改时间. 这个时间也是自 1970 年 1 月 1 日 0:00:00 (UTC) 算起的.===============================================================================</PRE></UL><H3>5.4.5 其他</H3><UL> <LI>特殊设备文件</LI><PRE>=============================================================================== * stat 结构的 st_dev 包含了文件所在文件系统的设备号. * stat 结构的 st_rdev 只对设备特殊文件有效. * 对 st_dev 和 st_rdev, 用 major 和 minor 两个宏, 可得到主设备号和次设备号.===============================================================================</PRE> <LI>sync 和 fsync: 同步</LI><PRE>=============================================================================== #include <unistd.h> int sync (void); int fsync (int fd); #ifdef _POSIX_SYNCHRONIZED_IO int fdatasync (int fd); #endif------------------------------------------------------------------------------- * sync 不等待实际的磁盘 I/O 结束, 只是将所有更新的块缓冲区进行排队. * fsync 和 fdatasync 等待实际的磁盘 I/O 结束. * fdatasync 只更新数据, 而 fsync 还要更新 inode 中的访问时间等信息.===============================================================================</PRE></UL><P><BR><BR></P><P ALIGN=CENTER><IMG SRC="images/striped.gif" NAME="Ruler" ALIGN=BOTTOM WIDTH=532 HEIGHT=13 BORDER=0></P><P><BR><BR></P><A HREF="stdio.html"><IMG SRC="prev.gif" ALT="Previous"></A><A HREF="advio.html"><IMG SRC="next.gif" ALT="Next"></A><A HREF="index.html"><IMG SRC="toc.gif" ALT="Contents"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -