-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs_mkdir.c
More file actions
68 lines (56 loc) · 2.32 KB
/
Copy pathfs_mkdir.c
File metadata and controls
68 lines (56 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**************************************************************
* Class:: CSC-415-02 Spring 2024
* Name:: Tushin Kulshreshtha
* Student IDs:: 922180763
* GitHub-Name:: Dextron04
* Group-Name:: Something Simple
* Project:: Basic File System
*
* File:: fs_mkdir.c
*
* Description:: This file implements a function to create a new
* directory within the file system, handling the allocation of
* space, and linking it to the parent directory.
*
**************************************************************/
#include "mfs.h"
#include "fileSystem.h"
int fs_mkdir(const char * pathname, mode_t mode){
ppReturn ret;
// To calculate the number of entries = Size / sizeof(DirectoryEntry);
// duplicating the string as it is declared as a constant.
char * newPath = strdup(pathname);
int pRet = parsePath(newPath, &ret);
// If pRet is returning a negative number that means the path
// that was passed in is invalid and the function will fail
// by returning a -1;
if(pRet != 0){
return -1;
}
// If the deNum is not -1 that means the directory that the user is
// trying to create already exists and a new directory cannot be created
// while the old one with the same name exists.
if(ret.deNum != -1){
return -1;
}
// The createDirectory function is called which takes in the default entry number and
// the parent which creates a new directory and passes a pointer to its directory entry
// in the newDir.
DirectoryEntry * newDir = createDirectory(DEFAULT_ENTRY_NUM, ret.parent);
if(newDir == NULL){
printf("[ERROR] Could not create new directory\n");
return -1;
}
// Find empty space in the parent directory to store the newley created directory.
int freeLocation = findSpaceInDir(ret.parent);
// Link the newley created directory to the to the parent.
strcpy(ret.parent[freeLocation].dirName, ret.lastElement);
ret.parent[freeLocation].location = newDir[0].location;
ret.parent[freeLocation].size = newDir[0].size;
ret.parent[freeLocation].isDirectory = newDir[0].isDirectory;
ret.parent[freeLocation].dateCreated = newDir[0].dateCreated;
ret.parent[freeLocation].dateModified = newDir[0].dateModified;
// Finally the save parent containing the new directory.
saveDir(ret.parent);
return newDir[0].location;
}