zoukankan      html  css  js  c++  java
  • 文件和目录的链接

    链接以及基础知识

    硬链接和软链接基本概念
    硬链接类似于拷贝,但与源文件同步更新,权限、大小、时间值均与源
    文件一样,inode也与源文件相同
    inode和block
    inode存文件属性
    block存文件数据
    使用ln命令可以创建链接

    硬链接link

    硬链接命令为ln,函数为link
    man 2 link
    硬链接函数
    int link(const char *oldpath, const char *newpath);
    参数*oldpath:已有的文件路径。
    参数*newpath:新建的硬链接文件路径。
    返回值:成功返回0,错误返回-1。
    编写编译运行测试

    #include <unistd.h>
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
            int ret;
    
            if(argc < 3) {
                    printf("Please input file path
    ");
                    return 1;
            }
    
            ret = link(argv[1], argv[2]);
            if(ret < 0) {
                    printf("%s link %s failed.
    ", argv[1], argv[2]);
                    return 1;
            }
            printf("%s link %s success
    ", argv[1], argv[2]);
    
            return 0;
    }

    符号链接symlink

    符号链接也叫软链接,symlink
    man 2 symlink
    软链接函数
    int symlink(const char *oldpath, const char *newpath);
    参数*oldpath:已有的文件路径
    参数*newpath:新建的符号链接文件路径
    返回值:成功返回0,错误返回-1
    编写编译运行测试
    和link对比测试

    #include <unistd.h>
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
            int ret;
    
            if(argc < 3) {
                    printf("Please input two path
    ");
                    return 1;
            }
    
            ret = symlink(argv[1], argv[2]);
            if(ret < 0) {
                    printf("symlink failed.
    ");
                    return 1;
            }
    
            printf("%s symlink %s success.
    ", argv[1], argv[2]);
    
            return 0;
    }

    解除链接unlink

    man 2 unlink
    解除链接函数
    int unlink(const char *pathname);
    参数*pathname:链接文件的路径
    返回值:成功返回0,错误返回-1
    unlink指向软链接,删除软链接;指向最后一个硬链接,相当于删除文件
    编写编译运行测试

    #include <unistd.h>
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
            int ret;
            if(argc < 2) {
                    printf("Please input path
    ");
                    return 1;
            }
    
            ret = unlink(argv[1]);
            if(ret < 0) {
                    printf("unlink failed.
    ");
                    return 1;
            }
            printf("unlink %s success.
    ", argv[1]);
    
            return 0;
    }

    拷贝文件

    Linux 下并没有专门的拷贝函数和接口,需要通过open,read,wite 等文件操作函数实现。
    拷贝流程图

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <string.h>
    #include <unistd.h>
    
    #define LENGTH 1024
    
    int main(int argc, char *argv[])
    {
            int ret, fileNew, fileOld;
            char buf[LENGTH];
            if(argc < 3) {
                    printf("Please input two path.
    ");
                    return 1;
            }
    
            fileOld = open(argv[1], O_RDWR|O_NOCTTY|O_NDELAY);
            if(fileOld < 0) {
                    printf("open %s failed.
    ", argv[1]);
                    return 1;
            }
    
            fileNew = open(argv[2], O_WRONLY|O_CREAT, 0644);
            if(fileNew < 0) {
                    printf("open %s failed
    ", argv[2]);
                    return 1;
            }
    
            while(read(fileOld, buf, LENGTH)) {
                    write(fileNew, buf, strlen(buf));
            }
    
            close(fileOld);
            close(fileNew);
    
            printf("cp to finished!
    ");
            printf("cp %s to %s success!
    ", argv[1], argv[2]);
    
            return 0;
    }

    移动文件命令为mv,函数为rename

    man 2 rename
    int rename(const char *oldpath, const char *newpath)
    参数*oldpath:旧的文件路径
    参数*newpath:新的文件路径
    返回值:成功返回0,错误返回-1
    编写编译运行测试

    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
            int ret;
            if(argc < 3) {
                    printf("Please input two path
    ");
                    return 1;
            }
    
            ret = rename(argv[1], argv[2]);
            if(ret < 0) {
                    printf("rename failed.
    ");
                    return 1;
            }
    
            printf("rename %s to %s success
    ", argv[1], argv[2]);
            return 0;
    }
    无欲速,无见小利。欲速,则不达;见小利,则大事不成。
  • 相关阅读:
    Kafka 生产者 自定义分区策略
    同步互斥
    poj 1562 Oil Deposits(dfs)
    poj 2386 Lake Counting(dfs)
    poj 1915 KnightMoves(bfs)
    poj 1664 放苹果(dfs)
    poj 1543 Perfect Cubes (暴搜)
    poj 1166 The Clocks (暴搜)
    poj 3126 Prime Path(bfs)
    处理机调度
  • 原文地址:https://www.cnblogs.com/ch122633/p/9406861.html
Copyright © 2011-2022 走看看