Skip to content
This repository was archived by the owner on Jun 8, 2021. It is now read-only.

Commit 286d10c

Browse files
committed
File system --documentation
1 parent 537d2e7 commit 286d10c

File tree

3 files changed

+259
-221
lines changed

3 files changed

+259
-221
lines changed

File_System/js/single.js

Lines changed: 67 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
var clipboard = [] // To store files to clipboard to copy or move them
1+
var clipboard = [] // To store files to clipboard to copy or move them
22
var pre = "user@root" // Beginning of each terminal line
3-
var current_path = "/" // Current location of user
3+
var current_path = "/" // Current location of user
44
var pointer = -1 // pointer points to the index of the current directory
55
//pointer = -1 corresponds to root directory
66
var file_dir = [] // List of files and directories
7-
var commands = ["create","vi","rm","truncate","ls","rename","properties","command_list"]
7+
var commands = ["create","vi","rm","truncate","ls","rename","properties","command_list"] //List of predefined commands
88

99
/*
10+
------------COMMANDS AVAILABLE-------------
1011
create : For creating a file
1112
mkdir : Creating a directory
1213
rmdir : Deleting a directory
@@ -26,8 +27,9 @@ var lslabel
2627
var index
2728

2829

29-
start()
30+
start() //Intializing the terminal UI
3031

32+
//Function to run terminal UI
3133
function start()
3234
{
3335
var div = document.getElementById('divx')
@@ -36,42 +38,42 @@ function start()
3638
textbox = document.createElement('input')
3739
textbox.setAttribute("class","textbox")
3840
textbox.setAttribute("type","text")
39-
textbox.setAttribute("onkeydown","nextline(event,textbox.value)")
41+
textbox.setAttribute("onkeydown","nextline(event,textbox.value)") //Detect every key pressed from terminal and send to nextline() function
4042
div.appendChild(label)
4143
div.appendChild(textbox)
4244
div.appendChild(br)
4345
textbox.focus()
4446
}
4547

46-
48+
//Function to run the input command from terminal
4749
function nextline(event,text)
4850
{
4951
var x = event.keyCode;
50-
if(x == 13)
52+
if(x == 13) // On ENTER key press (Keycode of ENTER is 13)
5153
{
52-
divide = text.split(" ")
54+
divide = text.split(" ") //Tokenize command
5355
user_com = divide[0]
5456
for(i=0;i<commands.length;i++)
5557
{
56-
if(user_com == commands[i])
58+
if(user_com == commands[i]) //Detecting the command (Which command it is)
5759
{
5860
break
5961
}
6062
}
61-
if(i < commands.length)
63+
if(i < commands.length) //Checking if command is predefined ie., is among the predefined list
6264
{
63-
switch(i)
65+
switch(i) //Running the command
6466
{
6567
case 0:create(divide[1])
66-
break
68+
break
6769
case 1:vi(divide[1])
68-
break
70+
break
6971
case 2:rm(divide[1])
7072
break
7173
case 3:truncate(divide[1])
72-
break
74+
break
7375
case 4:ls()
74-
break
76+
break
7577
case 5:rename(divide[1],divide[2])
7678
break
7779
case 6:properties(divide[1])
@@ -92,48 +94,50 @@ Each entry is a list
9294
4. Size : Length of content of file - String format
9395
5. Protection : User, Group, Other
9496
6. Location : Location of file
95-
7. Content : String
97+
7. Content : String
9698
*/
9799

98-
function create(file) // Working
100+
//Function to create a text file. Syntax: create <filename>
101+
function create(file)
99102
{
100-
for(i=0;i<file_dir.length;i++)
103+
for(i=0;i<file_dir.length;i++) //Traversing through all files to check if filename already exists
101104
{
102-
if(file_dir[i][0] == file && file_dir[i][2] == pointer) // Second condition verifies whether elements of current directory are checked
103-
{ // or not
105+
if(file_dir[i][0] == file && file_dir[i][2] == pointer) // Second condition verifies whether elements of current directory are checked
106+
{ // or not
104107
alert("File already exists")
105108
start()
106109
return
107110
}
108111
}
109-
file_dir.push([file,0,pointer,0,666,current_path,""])
112+
file_dir.push([file,0,pointer,0,666,current_path,""]) //Creating empty file
110113
start()
111114
}
112115

113-
114-
function ls() // Working
116+
//Function to list all content in the directory ie., system
117+
function ls()
115118
{
116119
var div = document.getElementById('divx')
117-
for(i=0;i<file_dir.length;i++)
120+
for(i=0;i<file_dir.length;i++) //Getting names of all files present
118121
{
119122
if(file_dir[i][2] == pointer)
120123
{
121-
var label = document.createTextNode(String(file_dir[i][0]) + " ")
122-
div.appendChild(label)
124+
var label = document.createTextNode(String(file_dir[i][0]) + " ") //Getting filename
125+
div.appendChild(label)
123126
}
124-
}
127+
}
125128
var br = document.createElement("br")
126-
div.appendChild(br)
129+
div.appendChild(br)
127130
start()
128131
}
129132

130-
function rm(file) // Working
133+
//Function to remove a file. Syntax: rm <filename>
134+
function rm(file)
131135
{
132136
for(i=0;i<file_dir.length;i++)
133137
{
134-
if(file_dir[i][0] == file && file_dir[i][2] == pointer && file_dir[i][1] == 0)
135-
{
136-
file_dir.splice(i,1)
138+
if(file_dir[i][0] == file && file_dir[i][2] == pointer && file_dir[i][1] == 0) //3rd Check is to confirm this is a file, not a folder
139+
{
140+
file_dir.splice(i,1) //Removing file
137141
start()
138142
return
139143
}
@@ -146,28 +150,29 @@ function rm(file) // Working
146150
start()
147151
}
148152

149-
function vi(file) // Working
153+
//Function to edit a text file. Syntax: vi <filename>
154+
function vi(file)
150155
{
151156
div = document.getElementById('divx')
152157
var flag = 0
153158
for(i=0;i<file_dir.length;i++)
154159
{
155-
if(file_dir[i][0] == file && file_dir[i][2] == pointer && file_dir[i][1] == 0)
156-
{
160+
if(file_dir[i][0] == file && file_dir[i][2] == pointer && file_dir[i][1] == 0)
161+
{
157162
flag = 1 // File is found
158163
var br = document.createElement("br")
159164
var br1 = document.createElement("br")
160-
var prompt = document.createTextNode("Press Ctrl to Save and Exit")
161-
contents = document.createElement('TextArea')
165+
var prompt = document.createTextNode("Press Ctrl to Save and Exit")
166+
contents = document.createElement('TextArea') // Create/display textbox to edit/add content to the file
162167
contents.setAttribute("rows","15")
163168
contents.setAttribute("cols","120")
164169
index = i
165-
contents.setAttribute("onkeydown","savefile(event,contents.value,index,contents)")
170+
contents.setAttribute("onkeydown","savefile(event,contents.value,index,contents)") //Add attribute to DOM to send data on each key press
166171
contents.value = file_dir[i][6] // Contents of file are copied to the textbox
167172
div.appendChild(prompt)
168173
div.appendChild(br)
169174
div.appendChild(contents)
170-
div.appendChild(br1)
175+
div.appendChild(br1)
171176
contents.focus()
172177
}
173178
}
@@ -181,27 +186,31 @@ function vi(file) // Working
181186
}
182187
}
183188

184-
function savefile(event,text,index,contents) // Save contents of file // Working
189+
190+
//Function to save contents of a file
191+
function savefile(event,text,index,contents)
185192
{
186193
var x = event.keyCode
187-
if(x == 17)
194+
if(x == 17) //Check if CTRL key is pressed
188195
{
189196
file_dir[index][6] = text
190197
file_dir[index][3] = String(text.length)
191198
alert("File saved")
192-
contents.setAttribute("disabled",true)
199+
contents.setAttribute("disabled",true) //Disable textbox
193200
start()
194201
}
195-
}
202+
}
203+
196204

205+
//Function to truncate a file. Syntax: truncate <filename>
197206
function truncate(file)
198207
{
199208
for(i=0;i<file_dir.length;i++)
200209
{
201-
if(file_dir[i][0] == file && file_dir[i][2] == pointer && file_dir[i][1] == 0)
202-
{
203-
file_dir[i][6] = ""
204-
file_dir[i][3] = "0"
210+
if(file_dir[i][0] == file && file_dir[i][2] == pointer && file_dir[i][1] == 0)
211+
{
212+
file_dir[i][6] = "" //Set content empty
213+
file_dir[i][3] = "0" //Set file size to 0
205214
start()
206215
return
207216
}
@@ -214,13 +223,15 @@ function truncate(file)
214223
start()
215224
}
216225

226+
227+
//Function to rename file. Syntax: rename <old filename> <new filename>
217228
function rename(file,name)
218229
{
219230
for(i=0;i<file_dir.length;i++)
220231
{
221-
if(file_dir[i][0] == file && file_dir[i][2] == pointer && file_dir[i][1] == 0)
222-
{
223-
file_dir[i][0] = name
232+
if(file_dir[i][0] == file && file_dir[i][2] == pointer && file_dir[i][1] == 0)
233+
{
234+
file_dir[i][0] = name //Set filename
224235
start()
225236
return
226237
}
@@ -233,15 +244,16 @@ function rename(file,name)
233244
start()
234245
}
235246

236-
function properties(file) // Working
247+
//Function to display file properties. Syntax: properties <filename>
248+
function properties(file)
237249
{
238250
for(i=0;i<file_dir.length;i++)
239251
{
240-
if(file_dir[i][0] == file && file_dir[i][2] == pointer && file_dir[i][1] == 0) // For files
241-
{
252+
if(file_dir[i][0] == file && file_dir[i][2] == pointer && file_dir[i][1] == 0) // For files
253+
{
242254
var div = document.getElementById('divx')
243255
var l1 = document.createTextNode("Name : " + file_dir[i][0])
244-
var br1 = document.createElement("br")
256+
var br1 = document.createElement("br")
245257
var l2 = document.createTextNode("Type : text")
246258
var br2 = document.createElement("br")
247259
var l3 = document.createTextNode("Size : " + file_dir[i][3])
@@ -268,6 +280,7 @@ function properties(file) // Working
268280
start()
269281
}
270282

283+
//Function to display available commands list
271284
function command_list()
272285
{
273286
var div = document.getElementById("divx")

0 commit comments

Comments
 (0)