1、进程是处于活动状态的程序,某个用户通过操作系统运行程序所产生的进程代表着该用户的行为。如果用户不具备访问某个目录和文件的权限,那么该用户的进程也不能访问。

2、Linux系统中文件安全机制是通过给系统中的文件赋予两个属性来实现的,这两个属性分别是所有者属性和访问权限属性。Linux系统下的每一个文件必须严格地属于一个用户和一个组,针对不同的用户和组又具有不同的访问权限。

3、系统调用是Linux内核提供的功能十分强大的一系列的函数。这些函数是在内核中实现的,他们是应用程序和内核交互的接口,如图所示。

4、打开文件操作使用系统调用函数open(),该函数的作用是建立一个文件描述符,其他的函数可以通过文件描述符对指定文件进行读取与写入的操作。

新建目录操作可使用函数mkdir()实现

获得当前子目录的操作可使用函数getwd(),

重新指定调用进程的当前工作目录 chdir(const char* pathname)

删除目录操作可使用函数rmdir()完成,该函数的一般形式是:rmdir(路径),该函数必须是在该目录下没有子目录或文件的情况下才能运行。删除文件操作可使用函数unlink,该函数的一般形式是:unlink(路径);

获取文件状态和属性操作可使用fstat()、lstat()和stat()这三个函数来操作。获取的stat结构中有以下属性可以使用:

5、代码:

(1)  FileHandler.c: file type and permission getter

/*
 * FileOpenClose.c
 *
 *  Created on: Jul 9, 2013
 *      Author: root
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main(){
    int f;
    const char* f_path="test";
    mode_t f_attrib;
    f_attrib = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
    struct stat *buf = malloc(sizeof(stat));
    f= open(f_path, O_RDONLY);
    if(f == -1){
        f = open(f_path, O_RDWR | O_CREAT, f_attrib);
        if(f != -1){
            puts("create a new file successfully");
        }else{
            puts("unble to create a file, program exit");
            return 1;
        }

}else{
        puts("open test file successfully");
    }
    close(f);
    stat(f_path, buf);
    //file mode: user permission
    if(buf->st_mode & S_IRUSR){
        puts("user have read permission!");
    }
    if(buf->st_mode & S_IWOTH){
        puts("other users have write permission.!");
    }else{
        puts("other users don't have write permission.!");
    }
    //file mode: file type.
    switch(buf->st_mode & S_IFMT){
        case S_IFREG: puts("regular\n");break;
        case S_IFDIR: puts("directory\n");break;
        case S_IFCHR: puts("character special");break;
        case S_IFBLK: puts("block special");break;
        case S_IFSOCK:puts("socket");break;
        default:puts("unknown mode");
    }
    return 0;
}

(2) DirectoryHandler.c: open a directory and read its sub files.

#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc, char * argv[]){
    char path[1000];
    DIR * dp;
    struct dirent * pdirent;
    if(argc != 2){
        printf("Usage ex3-9 <pathname>\n");
        return 1;
    }
    if((dp=opendir(argv[1])) == NULL){
        printf("OPen dir %s failed.\n", argv[1]);
        return 2;
    }
    while((pdirent=readdir(dp)) != 0){
        printf("%s\n", pdirent->d_name);
    }
    closedir(dp);
    return 0;
}

 (3) DirectoryHandler2.c:  scan  files in a directory

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

void scan_dir(char * pathname, int depth){
    DIR * dp;
    dp = opendir(pathname);
    if(dp == NULL){
        puts("directory open failed");
        return;
    }
    chdir(pathname);
    struct dirent * direntPointer;
    //struct stat * pstat = malloc(sizeof(stat));
    struct stat stat;
    while((direntPointer = readdir(dp)) != NULL){
        lstat(direntPointer->d_name, &stat);
        if((strcmp(".", direntPointer->d_name) == 0) ||
                (strcmp("..", direntPointer->d_name) == 0)){
            continue;
        }
        if((strcmp("Debug", direntPointer->d_name) == 0)){
            puts("debug*********");
        }
        if((stat.st_mode & S_IFMT) == S_IFDIR){
            printf("%*s%s\n", depth, "", direntPointer->d_name);
            scan_dir(direntPointer->d_name, depth+4);
        }else{
            printf("%*s%s\n", depth, "", direntPointer->d_name);
            //puts(direntPointer->d_name);
        }
    }
    chdir("..");

closedir(dp);
}

int main(){
    puts("/home directory is \n");
    scan_dir("/home", 0);
    return 0;
}

(4) DirectoryHandler3.c: create directory and file, and delete file and directory. if the directory does'nt have any file, we can delete this directory.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
    mode_t mode;
    mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP;
    if(mkdir("testdir", mode) != -1){
        puts("create testdir directory successfully.");
    }
    chdir("./testdir");
    if(creat("testfile", mode) != -1){
        puts("create testfile file successfully.");
    }

if(unlink("testfile") != -1){
        puts("delete testfile file successfully.");
    }
    chdir("..");
    if(rmdir("testdir") != -1){
        puts("delete testdir directory successfully.");
    }

}

LInux文件基础知识和文件目录操作(系统调用函数方式)的更多相关文章

  1. LInux文件基础知识和文件目录操作(二)文件I/O操作

    1.文件I/O操作分为两部分来讲解: 第一部分是非缓冲文件操作,这种操作适合于比较小规模文件的读写和对实时性要求很高的设备的数据通信,这类操作是系统调用提供的: 第二部分是缓冲文件操作,所面向的则是大 ...

  2. Linux文件管理 | Linux 文件基础知识

    目录 Linux 常用文件类别 Linux 目录结构概述 LInux 系统目录及说明 一.Linux 常用文件类别 1.文件   在Linux上系统上,有一切皆文件的说法,就是说任何软件和I/O设备都 ...

  3. Kali Linux渗透基础知识整理(四):维持访问

    Kali Linux渗透基础知识整理系列文章回顾 维持访问 在获得了目标系统的访问权之后,攻击者需要进一步维持这一访问权限.使用木马程序.后门程序和rootkit来达到这一目的.维持访问是一种艺术形式 ...

  4. Kali Linux渗透基础知识整理(二)漏洞扫描

    Kali Linux渗透基础知识整理系列文章回顾 漏洞扫描 网络流量 Nmap Hping3 Nessus whatweb DirBuster joomscan WPScan 网络流量 网络流量就是网 ...

  5. Linux shell基础知识(上)

    Linux shell基础知识(上) 目录 一.shell介绍 二.命令历史 三.命令补全和别名 四.通配符 五.输入输出重定向 六.管道符和作业控制 七.shell变量 八.环境变量配置文件 九.b ...

  6. Linux入门基础知识

    注:内容系兄弟连Linux教程(百度传课:史上最牛的Linux视频教程)的学习笔记. Linux入门基础知识 1. Unix和Linux发展历史 二者就像父子关系,当然Unix是老爹.1965年,MI ...

  7. Greenplum入门——基础知识、安装、常用函数

    Greenplum入门——基础知识.安装.常用函数 2017年10月08日 22:03:09 在咖啡里溺水的鱼 阅读数:8709    版权声明:本文为博主原创,允许非商业性质转载但请注明原作者和出处 ...

  8. [SQL] SQL 基础知识梳理(六)- 函数、谓词、CASE 表达式

    SQL 基础知识梳理(六)-  函数.谓词.CASE 表达式 目录 函数 谓词 CASE 表达式 一.函数 1.函数:输入某一值得到相应输出结果的功能,输入值称为“参数”,输出值称为“返回值”. 2. ...

  9. Linux Shell 基础知识(一)

    1. 本文知识结构 2. shell 基础知识 2.1 shell 简单介绍 ​ GNU bash shell 能提供对 Linux 系统的交互式访问,一般来说,使用快捷键 Ctrl + Alt + ...

随机推荐

  1. 容器————priority_queue

    #include <queue> 与queue不同的是可以自定义其中数据的优先级,让优先级高的先出队列. 优先队列具有队列的所有特性,包括基本操作,只是在这基础上添加了内部的一个排序,它本 ...

  2. RealsenseD415/D435深度相机常用资料汇总

    1.Realsense SDK 2.0 Ubuntu 16.04 安装指导网址https://github.com/IntelRealSense/librealsense/blob/master/do ...

  3. AcWing 241. 楼兰图腾 (树状数组)打卡

    题目:https://www.acwing.com/problem/content/description/243/ 题意:给你n个点,问你 V 和  ^的图腾有多少个 思路:比如V 其实就是找当前点 ...

  4. Nginx加载多个CPU核心,worker_cpu_affinity

    配置Nginx多核CPU,worker_cpu_affinity使用方法和范例1. 2核CPU,开启2个进程 worker_processes 2;worker_cpu_affinity 01 10; ...

  5. [CSP-S模拟测试]:玄学题/c(数学)

    题目传送门(内部题40) 输入格式 第一行:两个正整数$n$.$m$. 输出格式 第一行:一个整数,代表式子的值. 样例 样例输入1: 4 5 样例输出1: 0样例输入2: 799 8278 样例输出 ...

  6. 分布式系统理论基础4:Paxos

    本文转自:https://www.cnblogs.com/bangerlee/p/5655754.html 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到 ...

  7. vue之slot用法

    slot元素作为组件模板之中的内容分发插槽.这个元素自身将被替换. 有 name 特性的 slot 称为具名 slot. 有 slot 特性的内容将分发到名字相匹配的具名 slot. 一.单个组件 如 ...

  8. python基础二(基本数据类型)

    python的基本数据类型:数字.字符串.列表.元祖.字典.集合 一.基本数据类型 1.1 数字int 数字主要是用来计算用的,使用方法并不多. # bit_length() 当十进制用二进制表示的时 ...

  9. HTML5: HTML5 拖放

    ylbtech-HTML5: HTML5 拖放 1.返回顶部 1. HTML5 拖放(Drag 和 Drop) 拖放(Drag 和 drop)是 HTML5 标准的组成部分.   将 RUNOOB.C ...

  10. MySQL高级学习笔记(六):MySql锁机制

    文章目录 概述 定义 生活购物 锁的分类 从对数据操作的类型(读\写)分 从对数据操作的粒度分 三锁 表锁(偏读) 特点 案例分析 建表SQL 加读锁 加写锁 结论 如何分析表锁定 行锁(偏写) 特点 ...