add: touch
This commit is contained in:
parent
5a2dc67707
commit
2645c3368c
2
Makefile
2
Makefile
@ -1,7 +1,7 @@
|
|||||||
NAME := seyshell
|
NAME := seyshell
|
||||||
DRIVE := disk
|
DRIVE := disk
|
||||||
CC := gcc
|
CC := gcc
|
||||||
CFLAGS :=
|
CFLAGS := -Wall -Wextra -Werror -g
|
||||||
OBJ_DIR := obj
|
OBJ_DIR := obj
|
||||||
SRC_DIR := src
|
SRC_DIR := src
|
||||||
|
|
||||||
|
|||||||
88
src/disk.c
88
src/disk.c
@ -104,39 +104,73 @@ int create_directory(disk *disk, inode *parent, char *dirname) {
|
|||||||
return new_idx;
|
return new_idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
int do_mkdir(disk *d, char *dirpath) {
|
int do_touch(disk *d, char *filepath) {
|
||||||
char *parent_path;
|
char *parent_path;
|
||||||
char *dirname;
|
char *dirname;
|
||||||
|
|
||||||
if (get_dirname_and_parent_path_by_absolute_path(dirpath, &parent_path, &dirname) == -1) {
|
if (get_name_and_parent_path_by_absolute_path(filepath, &parent_path,
|
||||||
dprintf(STDERR_FILENO, "mkdir: erreur jsp");
|
&dirname) == -1) {
|
||||||
|
dprintf(STDERR_FILENO, "mkdir: erreur jsp");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *token = strtok(parent_path, "/");
|
||||||
|
int dir_index = 0;
|
||||||
|
|
||||||
|
while (token != NULL) {
|
||||||
|
|
||||||
|
dir_index = find_dir_inode_by_name(token, dir_index, d);
|
||||||
|
|
||||||
|
if (dir_index == -1) {
|
||||||
|
dprintf(STDERR_FILENO, "touch: %s file does not exist\n", token);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *token = strtok(parent_path, "/");
|
if (d->inodes[dir_index].filetype != TYPE_DIRECTORY) {
|
||||||
int dir_index = 0;
|
dprintf(STDERR_FILENO, "touch: %s is not a directory\n", token);
|
||||||
|
return -1;
|
||||||
while (token != NULL) {
|
|
||||||
|
|
||||||
dir_index = find_dir_inode_by_name(token, dir_index, d);
|
|
||||||
|
|
||||||
if (dir_index == -1) {
|
|
||||||
dprintf(STDERR_FILENO, "mkdir: %s directory does not exist\n", token);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (d->inodes[dir_index].filetype != TYPE_DIRECTORY) {
|
|
||||||
dprintf(STDERR_FILENO, "mkdir: %s is not a directory\n", token);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
token = strtok(NULL, "/");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
token = strtok(NULL, "/");
|
||||||
create_directory(d, &d->inodes[dir_index], dirname);
|
}
|
||||||
|
|
||||||
return 0;
|
create_file(d, &d->inodes[dir_index], dirname);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int do_mkdir(disk *d, char *dirpath) {
|
||||||
|
char *parent_path;
|
||||||
|
char *dirname;
|
||||||
|
|
||||||
|
if (get_name_and_parent_path_by_absolute_path(dirpath, &parent_path,
|
||||||
|
&dirname) == -1) {
|
||||||
|
dprintf(STDERR_FILENO, "mkdir: erreur jsp");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *token = strtok(parent_path, "/");
|
||||||
|
int dir_index = 0;
|
||||||
|
|
||||||
|
while (token != NULL) {
|
||||||
|
|
||||||
|
dir_index = find_dir_inode_by_name(token, dir_index, d);
|
||||||
|
|
||||||
|
if (dir_index == -1) {
|
||||||
|
dprintf(STDERR_FILENO, "mkdir: %s directory does not exist\n", token);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (d->inodes[dir_index].filetype != TYPE_DIRECTORY) {
|
||||||
|
dprintf(STDERR_FILENO, "mkdir: %s is not a directory\n", token);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
token = strtok(NULL, "/");
|
||||||
|
}
|
||||||
|
|
||||||
|
create_directory(d, &d->inodes[dir_index], dirname);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void write_in_file(disk *d, int file_index, char *data /*, char mode*/) {
|
void write_in_file(disk *d, int file_index, char *data /*, char mode*/) {
|
||||||
@ -266,8 +300,6 @@ char *read_in_file(disk *d, int file_index) {
|
|||||||
void init_test_files(disk *d) {
|
void init_test_files(disk *d) {
|
||||||
int inode_idx = create_file(d, &d->inodes[0], "test");
|
int inode_idx = create_file(d, &d->inodes[0], "test");
|
||||||
write_in_file(d, inode_idx, "Contenu du fichier :)\n");
|
write_in_file(d, inode_idx, "Contenu du fichier :)\n");
|
||||||
int dir_idx = create_directory(d, &d->inodes[0], "dir");
|
|
||||||
create_file(d, &d->inodes[dir_idx], "test1");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -15,5 +15,6 @@
|
|||||||
disk create_disk();
|
disk create_disk();
|
||||||
disk open_disk(char* filename);
|
disk open_disk(char* filename);
|
||||||
int do_ls(disk *d, char* path);
|
int do_ls(disk *d, char* path);
|
||||||
|
int do_touch(disk *d, char *filepath);
|
||||||
int do_mkdir(disk* d, char *dirpath);
|
int do_mkdir(disk* d, char *dirpath);
|
||||||
int find_dir_inode_by_name(char *name, int dir_index, disk *d);
|
int find_dir_inode_by_name(char *name, int dir_index, disk *d);
|
||||||
@ -1,5 +1,7 @@
|
|||||||
/** @file */
|
/** @file */
|
||||||
#include "exec.h"
|
#include "exec.h"
|
||||||
|
#include "disk.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
int do_cd(disk *d, char *path) {
|
int do_cd(disk *d, char *path) {
|
||||||
char full_path[1024] = {0};
|
char full_path[1024] = {0};
|
||||||
@ -34,6 +36,8 @@ int child_process_job(disk *d, char **args) {
|
|||||||
do_ls(d, args[1]);
|
do_ls(d, args[1]);
|
||||||
} else if (strcmp("mkdir", args[0]) == 0) {
|
} else if (strcmp("mkdir", args[0]) == 0) {
|
||||||
do_mkdir(d, args[1]);
|
do_mkdir(d, args[1]);
|
||||||
|
} else if (strcmp("touch", args[0]) == 0) {
|
||||||
|
do_touch(d, args[1]);
|
||||||
} else {
|
} else {
|
||||||
dprintf(STDERR_FILENO, "%s: no such command\n", args[0]);
|
dprintf(STDERR_FILENO, "%s: no such command\n", args[0]);
|
||||||
}
|
}
|
||||||
|
|||||||
20
src/utils.c
20
src/utils.c
@ -12,29 +12,29 @@ void format_path(char *dest, char *src, int dest_len) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_dirname_and_parent_path_by_absolute_path(char* dirpath, char** parent_path, char** dirname) {
|
int get_name_and_parent_path_by_absolute_path(char* path, char** parent_path, char** name) {
|
||||||
if (dirpath == NULL || strlen(dirpath) == 0) {
|
if (path == NULL || strlen(path) == 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t len = strlen(dirpath);
|
size_t len = strlen(path);
|
||||||
if (len > 1 && dirpath[len - 1] == '/') {
|
if (len > 1 && path[len - 1] == '/') {
|
||||||
dirpath[len - 1] = '\0';
|
path[len - 1] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
char *last_occ = strrchr(dirpath, '/');
|
char *last_occ = strrchr(path, '/');
|
||||||
|
|
||||||
if (last_occ == NULL) {
|
if (last_occ == NULL) {
|
||||||
*parent_path = strdup(get_env_value("PWD"));
|
*parent_path = strdup(get_env_value("PWD"));
|
||||||
*dirname = strdup(dirpath);
|
*name = strdup(path);
|
||||||
} else {
|
} else {
|
||||||
*dirname = strdup(last_occ + 1);
|
*name = strdup(last_occ + 1);
|
||||||
|
|
||||||
if (last_occ == dirpath) {
|
if (last_occ == path) {
|
||||||
*parent_path = strdup("/");
|
*parent_path = strdup("/");
|
||||||
} else {
|
} else {
|
||||||
*last_occ = '\0';
|
*last_occ = '\0';
|
||||||
*parent_path = strdup(dirpath);
|
*parent_path = strdup(path);
|
||||||
*last_occ = '/';
|
*last_occ = '/';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,4 +8,4 @@
|
|||||||
|
|
||||||
void canonicalize_path(char *path);
|
void canonicalize_path(char *path);
|
||||||
void format_path(char *dest, char *src, int dest_len);
|
void format_path(char *dest, char *src, int dest_len);
|
||||||
int get_dirname_and_parent_path_by_absolute_path(char* dirpath, char** parent_path, char** dirname);
|
int get_name_and_parent_path_by_absolute_path(char* dirpath, char** parent_path, char** dirname);
|
||||||
Loading…
Reference in New Issue
Block a user