嵌入式学习 Day 22

一. 时间获取:


    1.time 


      time_t time(time_t *tloc);
      功能:
        返回1970-1-1到现在的秒数(格林威治时间)
      参数:
        tloc:存放秒数空间首地址
      返回值:
        成功返回秒数
        失败返回-1 

注意:两种方式都可以获得秒数

通过返回值获得秒数

通过存放秒数的空间首地址获得秒数

    2.localtime


      struct tm *localtime(const time_t *timep);
      功能:
        将秒数转换为本地时间
      参数:
        timep:存放秒数空间首地址
      返回值:
        成功返回结构体时间
        失败返回NULL

        struct tm {
            int tm_sec;    /* Seconds (0-60) */
            int tm_min;    /* Minutes (0-59) */
            int tm_hour;   /* Hours (0-23) */
            int tm_mday;   /* Day of the month (1-31) */
            int tm_mon;    /* Month (0-11) */
            int tm_year;   /* Year - 1900 */
            int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
            int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
            int tm_isdst;  /* Daylight saving time */
        };

    3.mktime


      time_t mktime(struct tm *tm);
      功能:
        将本地时间转换为秒数

二. 文件属性和权限的获取:


    1.stat


      int stat(const char *pathname, struct stat *statbuf);    // 操作链接文件指向的文件

      int lstat(const char *pathname, struct stat *statbuf);   // 操作链接文件本身
      功能:
        将pathname对应的文件信息放入statbuf所指向的空间中
      参数:
        pathname:文件路径字符串的首地址
        statbuf:存放文件信息空间的首地址
      返回值:
        成功返回0 
        失败返回-1 

    struct stat {
        dev_t     st_dev;         /* ID of device containing file */
        ino_t     st_ino;         /* Inode number */
        mode_t    st_mode;        /* File type and mode */
        nlink_t   st_nlink;       /* Number of hard links */
        uid_t     st_uid;         /* User ID of owner */
        gid_t     st_gid;         /* Group ID of owner */
        dev_t     st_rdev;        /* Device ID (if special file) */
        off_t     st_size;        /* Total size, in bytes */
        blksize_t st_blksize;     /* Block size for filesystem I/O */
        blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */

        /* Since Linux 2.6, the kernel supports nanosecond
            precision for the following timestamp fields.
            For the details before Linux 2.6, see NOTES. */

        struct timespec st_atim;  /* Time of last access */
        struct timespec st_mtim;  /* Time of last modification */
        struct timespec st_ctim;  /* Time of last status change */

    #define st_atime st_atim.tv_sec      /* Backward compatibility */
    #define st_mtime st_mtim.tv_sec
    #define st_ctime st_ctim.tv_sec
    };

    /etc/passwd   口令文件
    /etc/group    组信息文件

注意:

这样写是不对的,虽然pbuf为指针类型,符合stat的参数类型,但为野指针,信息会随机存储到野指针所指向的空间。 

接函数返回值时可以定义指针来接,但函数参数要考虑能否使用指针 

判断文件类型:

文件类型信息在buf里的st_mode中,将其以16进制的形式打印出来为:0x41fd

通过位运算:与0得0;或1置1

将0x41fd写为二进制形式,再与1111 0000 0000 0000 将只保留前四位文件类型,再将其与上表比较得出文件类型。

判断文件权限:

将0100 0001 1111 1101 与上 0000 0001 0000 0000,只保留1所在位数值不变,其余位置零,从而判断出是否具有该位权限。 

    2.getpwuid 


      struct passwd *getpwuid(uid_t uid);
      功能:
        通过UID获得对应的用户信息
      参数:
        uid:用户的ID号
      返回值:
        成功返回包含用户信息的结构体
        失败返回NULL

    struct passwd {
        char   *pw_name;       /* username */
        char   *pw_passwd;     /* user password */
        uid_t   pw_uid;        /* user ID */
        gid_t   pw_gid;        /* group ID */
        char   *pw_gecos;      /* user information */
        char   *pw_dir;        /* home directory */
        char   *pw_shell;      /* shell program */
    };

    3.getgrgid


      struct group *getgrgid(gid_t gid);
      功能:
        通过组ID获得组信息
      参数:
        gid:组的ID号
      返回值:
        成功返回包含组信息的结构体
        失败返回NULL
    

    struct group {
        char   *gr_name;        /* group name */
        char   *gr_passwd;      /* group password */
        gid_t   gr_gid;         /* group ID */
        char  **gr_mem;         /* NULL-terminated array of pointers
                                    to names of group members */
    };

    4.readlink


      ssize_t readlink(const char *pathname, char *buf, size_t bufsiz);
      功能:
        读取链接文件本身的内容
      参数:
        pathname:链接文件的路径
        buf:存放数据空间首地址
        bufsiz:最大存放数据字节数
      返回值:
        成功返回读到字节个数
        失败返回-1  


三. 软连接和硬链接:


    1.软链接(符号链接)


        通过文件名链接,所有能够看到的链接文件均为软链接文件

        ln -s file.txt a.txt       //创建软链接 a.txt  到 file.txt

        操作 a.txt 就相当于操作 file.txt

    2.硬链接

 
        通过文件对应的inode节点链接     

        ln file.txt b.txt         //创建硬链接 b.txt 到 file.txt

        操作 b.txt 就相当于操作 file.txt

注意:

            删除 file.txt后再次创建 file.txt 软链接再次出现(软链接是通过文件名建立的链接,只要文件名恢复,链接恢复)

            删除 file.txt后再次创建 file.txt 硬链接不会再次出现(硬链接是通过inode节点建立的链接,恢复文件名inode节点不会恢复,链接不会恢复)

对于软连接:a.txt -> inode -> 数据块2 -> file.txt -> inode -> 数据块1

恢复 file.txt 文件名,意味着可再次通过inode节点找到file.txt对应的数据块(此时file.txt对应的数据块不一定在1000这个位置,因为无法被访问到的文件空间将被系统回收,此时新建立的file.txt[大概率不在在1000这个位置,若在数据恰巧恢复],之前的数据丢失[打开file.txt显示空白])

对于硬连接:a.txt -> inode -> 数据块1 

删除file.txt后,仍可以通过b.txt来操作该文件,数据不会丢失。这是因为该文件相当于有两个文件名,删除掉其中一个,仍可以通过另一个文件名来访问到该文件。只是硬链接个数减1

编程实现 ls -l 功能 :

相关推荐

  1. 嵌入学习day22 Linux

    2024-02-21 09:34:02       29 阅读
  2. 嵌入学习笔记day28

    2024-02-21 09:34:02       15 阅读
  3. 嵌入学习 Day17

    2024-02-21 09:34:02       29 阅读
  4. 嵌入学习day33

    2024-02-21 09:34:02       23 阅读
  5. 嵌入学习 Day 31

    2024-02-21 09:34:02       20 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-21 09:34:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-21 09:34:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-21 09:34:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-21 09:34:02       20 阅读

热门阅读

  1. Go 空切片与nil切片

    2024-02-21 09:34:02       33 阅读
  2. 区块链笔记(二)

    2024-02-21 09:34:02       31 阅读
  3. 闲鱼搜索API接口

    2024-02-21 09:34:02       63 阅读
  4. Android无法获取已安装应用包名的问题

    2024-02-21 09:34:02       28 阅读
  5. svg.js

    2024-02-21 09:34:02       28 阅读
  6. linux系统消息队列的模式和介绍

    2024-02-21 09:34:02       30 阅读
  7. 浅谈数仓发展

    2024-02-21 09:34:02       28 阅读
  8. pytorch-textsummary,中文文本摘要实践

    2024-02-21 09:34:02       35 阅读
  9. 隐私保护 AI 的演变:从协议到实际实现

    2024-02-21 09:34:02       36 阅读
  10. 【Jenkins+Gitlab自动化部署配置】

    2024-02-21 09:34:02       26 阅读
  11. Node.js

    Node.js

    2024-02-21 09:34:02      26 阅读