Skip to content

Commit

Permalink
Prevent opening device file
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlin96069 committed Mar 8, 2024
1 parent 89694d9 commit 41a2c02
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/file_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,26 @@ bool editorOpen(EditorFile* file, const char* path) {
editorInitFile(file);

FileType type = getFileType(path);
if (type != FT_INVALID) {
if (type == FT_DIR) {
if (gEditor.explorer.node)
switch (type) {
case FT_REG: {
FileInfo file_info = getFileInfo(path);
if (file_info.error) {
editorMsg("Can't load \"%s\"! Failed to get file info.", path);
return false;
}
file->file_info = file_info;
int open_index = isFileOpened(file_info);

if (open_index != -1) {
editorChangeToFile(open_index);
return false;
}
} break;

case FT_DIR:
if (gEditor.explorer.node) {
editorExplorerFreeNode(gEditor.explorer.node);
}
changeDir(path);
gEditor.explorer.node = editorExplorerCreate(".");
gEditor.explorer.node->is_open = true;
Expand All @@ -87,25 +103,13 @@ bool editorOpen(EditorFile* file, const char* path) {
gEditor.explorer.offset = 0;
gEditor.explorer.selected_index = 0;
return false;
}

if (type != FT_REG) {
editorMsg("Can't load \"%s\"! Not a regular file.", path);
case FT_DEV:
editorMsg("Can't load \"%s\"! It's a device file.", path);
return false;
}

FileInfo file_info = getFileInfo(path);
if (file_info.error) {
editorMsg("Can't load \"%s\"! Failed to get file info.", path);
return false;
}
file->file_info = file_info;
int open_index = isFileOpened(file_info);

if (open_index != -1) {
editorChangeToFile(open_index);
return false;
}
default:
break;
}

FILE* fp = openFile(path, "rb");
Expand Down
1 change: 1 addition & 0 deletions src/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ typedef enum FileType {
FT_INVALID = -1,
FT_REG,
FT_DIR,
FT_DEV,
} FileType;
FileType getFileType(const char* path);

Expand Down
2 changes: 2 additions & 0 deletions src/os_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ FileType getFileType(const char* path) {
struct stat info;
if (stat(path, &info) == -1)
return FT_INVALID;
if (S_ISCHR(info.st_mode))
return FT_DEV;
if (S_ISDIR(info.st_mode))
return FT_DIR;
if (S_ISREG(info.st_mode))
Expand Down

0 comments on commit 41a2c02

Please sign in to comment.