Skip to content

Commit 156ad81

Browse files
committed
Code Update
* Changed the file paths to be converted to Nion file paths * Optimized Cryptography class; hashing files accept File types directly rather than accepting a String and then converting to File * Added feature to display the MD5 hash when listing files in a directory * makefile clean target removes contents inside the DeveloperDocumentaion and InternalDocumentation directories * Added initial documentation
1 parent 27a5d62 commit 156ad81

15 files changed

+175
-55
lines changed

README

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ Powered By Truncheon Core.
1313

1414
Website: https://dak404.github.io/Cataphract
1515

16-
Current Status: LVL_1-DEV
16+
Current Status: LVL_2-DEV -> TESTING
1717

1818
-----------------------------------------------------------------------------------------------
1919

2020
=== ! WARNING ! ===
2121

22-
This repository has code that is still under development.
23-
Currently, the kernel is under development.
22+
This repository has code that is still being tested.
2423

2524
DO NOT USE THE CODE FOR ANY PRODUCTION BUILDS!
2625

Source/Cataphract/API/Dragon/AccountCreate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ private final void addAccountToDatabase()
445445
try
446446
{
447447
// Store the path of the database
448-
String databasePath = "jdbc:sqlite:./System/Cataphract/Private/Mud.dbx";
448+
String databasePath = "jdbc:sqlite:" + IOStreams.convertFileSeparator(".|System|Cataphract|Private|Mud.dbx");
449449

450450
// Store the SQL command that needs to be executed by the SQLite database engine
451451
String sqlCommand = "INSERT INTO MUD(Username, Name, Password, SecurityKey, PIN, Privileges) VALUES(?,?,?,?,?,?)";
@@ -477,7 +477,7 @@ private final void addAccountToDatabase()
477477
dbConnection.close();
478478

479479
//Create directory for the new user account
480-
new File("./Users/Cataphract/"+_newAccountUsername).mkdirs();
480+
new File(IOStreams.convertFileSeparator(".|Users|Cataphract|" + _newAccountUsername)).mkdirs();
481481

482482
// Invoke garbage collector to free resources up
483483
System.gc();

Source/Cataphract/API/Dragon/AccountDelete.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private boolean accountDeletionLogic(String username) throws Exception
170170
if (console.readLine("Are you sure you wish to delete user account \"" + new Login(username).getNameLogic() + "\"? [ YES | NO ]\n> ").equalsIgnoreCase("yes"))
171171
{
172172
// Delete account from database and directories
173-
status = deleteFromDatabase() & deleteDirectories(new File("./Users/Cataphract/" + username));
173+
status = deleteFromDatabase() & deleteDirectories(new File(IOStreams.convertFileSeparator(".|Users|Cataphract|" + username)));
174174
// Print success message and exit
175175
IOStreams.printAttention("Account Successfully Deleted.");
176176
if(! _isCurrentUserAdmin)
@@ -231,7 +231,7 @@ private boolean deleteFromDatabase()
231231
try
232232
{
233233
// Define database path and SQL command
234-
String databasePath = "jdbc:sqlite:./System/Cataphract/Private/Mud.dbx";
234+
String databasePath = "jdbc:sqlite:" + IOStreams.convertFileSeparator(".|System|Cataphract|Private|Mud.dbx");
235235
String sqlCommand = "DELETE FROM MUD WHERE Username = ?";
236236

237237
// Load JDBC driver and establish connection

Source/Cataphract/API/Dragon/AccountModify.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,13 @@ private void viewUserInformation(String targetUser) throws Exception
302302
// Check if the specified user exists
303303
if(new Login(targetUser).checkUserExistence())
304304
{
305+
String userHomePath = IOStreams.convertFileSeparator(".|Users|Cataphract|" + targetUser);
306+
305307
// Display the target user information
306308
IOStreams.println("\n--- User Information ---");
307309
IOStreams.println("Account Name : " + new Login(targetUser).getNameLogic());
308310
IOStreams.println("Account Privileges : " + new Login(targetUser).checkPrivilegeLogic());
309-
IOStreams.println("User Home Directory : " + (new File("./Users/Cataphract/" + targetUser).exists()?"./Users/Cataphract/" + targetUser:"Home Directory Does Not Exist!") + "\n");
311+
IOStreams.println("User Home Directory : " + (new File(userHomePath).exists()?userHomePath:"Home Directory Does Not Exist!") + "\n");
310312
}
311313
// Notify that the specified user does not exist
312314
else
@@ -418,7 +420,7 @@ private void commitChangesToDatabase(String parameter, String value, String targ
418420
try
419421
{
420422
// Store the path of the database
421-
String databasePath = "jdbc:sqlite:./System/Cataphract/Private/Mud.dbx";
423+
String databasePath = "jdbc:sqlite:" + IOStreams.convertFileSeparator(".|System|Cataphract|Private|Mud.dbx");
422424

423425
// Store the SQL command that needs to be executed by the SQLite database engine
424426
String sqlCommand = "UPDATE MUD SET " + parameter + " = ? WHERE Username = ?";

Source/Cataphract/API/Dragon/Login.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
*/
3434
public class Login
3535
{
36+
/** Stores the Database path */
37+
private String databasePath = "jdbc:sqlite:" + IOStreams.convertFileSeparator(".|System|Cataphract|Private|Mud.dbx");
38+
3639
/** Stores the username */
3740
private String _username;
3841

@@ -114,6 +117,11 @@ public final boolean checkUserExistence() throws Exception
114117
return checkForExistingAccount();
115118
}
116119

120+
/**
121+
* Enumerates all the user accounts in the database
122+
*
123+
* @throws Exception Throws any exceptions encountered during runtime.
124+
*/
117125
protected void listAllUserAccounts()throws Exception
118126
{
119127
// Check if the current user is an Administrator
@@ -130,8 +138,8 @@ protected void listAllUserAccounts()throws Exception
130138
String disp = String.format(format, "Username", "Account Name", "Privileges");
131139
System.out.println(disp + c.repeat(disp.length()) + "\n");
132140

133-
// List the files in the ./Users/Cataphract directory
134-
File[] fileList = new File("./Users/Cataphract/").listFiles();
141+
// List the directories in the Users directory
142+
File[] fileList = new File(IOStreams.convertFileSeparator(".|Users|Cataphract|")).listFiles();
135143

136144
// Iterate through the list of files
137145
for(File users: fileList)
@@ -162,8 +170,6 @@ private final String retrieveDatabaseEntry(String sqlCommand, String parameter)
162170

163171
// JDBC driver registration
164172
Class.forName("org.sqlite.JDBC");
165-
// Database path
166-
String databasePath = "jdbc:sqlite:./System/Cataphract/Private/Mud.dbx";
167173

168174
// Establish database connection
169175
Connection dbConnection = DriverManager.getConnection(databasePath);
@@ -216,9 +222,7 @@ private boolean checkForExistingAccount() throws Exception
216222

217223
// JDBC driver registration
218224
Class.forName("org.sqlite.JDBC");
219-
// Database path
220-
String databasePath = "jdbc:sqlite:./System/Cataphract/Private/Mud.dbx";
221-
225+
222226
// Establish database connection
223227
Connection dbConnection = DriverManager.getConnection(databasePath);
224228

Source/Cataphract/API/Minotaur/Cryptography.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,37 +82,37 @@ public final static String stringToSHA3_256(String input) throws Exception
8282
/**
8383
* Hashing API which will return the MD5 hash value of a given file
8484
*
85-
* @param fileName The name of the file to be hashed
85+
* @param fileName The file to be hashed
8686
* @return String Returns the MD5 hash
8787
* @throws Exception Throws any exceptions encountered during runtime.
8888
*/
89-
public final static String fileToMD5(String fileName) throws Exception
89+
public final static String fileToMD5(File fileName) throws Exception
9090
{
91-
return hashFile(new File(fileName), "MD5");
91+
return hashFile(fileName, "MD5");
9292
}
9393

9494
/**
9595
* Hashing API which will return the SHA3-256 hash value of a given file
9696
*
97-
* @param fileName The name of the file to be hashed
97+
* @param fileName The file to be hashed
9898
* @return String Returns the SHA3-256 hash
9999
* @throws Exception Throws any exceptions encountered during runtime.
100100
*/
101-
public final static String fileToSHA_256(String fileName) throws Exception
101+
public final static String fileToSHA_256(File fileName) throws Exception
102102
{
103-
return hashFile(new File(fileName), "SHA-256");
103+
return hashFile(fileName, "SHA-256");
104104
}
105105

106106
/**
107107
* Hashing API which will return the SHA3-256 hash value of a given file
108108
*
109-
* @param fileName The name of the file to be hashed
109+
* @param fileName The file to be hashed
110110
* @return String Returns the SHA3-256 hash
111111
* @throws Exception Throws any exceptions encountered during runtime.
112112
*/
113-
public final static String fileToSHA3_256(String fileName) throws Exception
113+
public final static String fileToSHA3_256(File fileName) throws Exception
114114
{
115-
return hashFile(new File(fileName), "SHA3-256");
115+
return hashFile(fileName, "SHA3-256");
116116
}
117117

118118
// ------------------------------------------------------------------------------------ //

Source/Cataphract/API/Minotaur/PolicyCheck.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
//Import the required Java Util classes
2222
import java.util.Properties;
2323

24+
import Cataphract.API.IOStreams;
25+
2426
/**
2527
* A class that helps to check the policy for a given module.
2628
*
@@ -53,7 +55,7 @@ public final String retrievePolicyValue(String policyParameter)throws Exception
5355
{
5456
//Open the properties streams
5557
Properties prop = new Properties();
56-
String propsFileName="./System/Cataphract/Private/Policy.burn";
58+
String propsFileName = IOStreams.convertFileSeparator(".|System|Cataphract|Private|Policy.burn");
5759

5860
//Load the file stream containing the program properties
5961
FileInputStream configStream = new FileInputStream(propsFileName);

Source/Cataphract/API/Minotaur/PolicyManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class PolicyManager
4242
public final String [] resetValues = {"update", "download", "script", "filemgmt", "read", "edit", "policy", "account_create", "account_delete", "account_modify"};
4343

4444
/** Stores the path of the policy file.*/
45-
private final String policyFileName = "./System/Cataphract/Private/Policy.burn";
45+
private final String policyFileName = IOStreams.convertFileSeparator(".|System|Cataphract|Private|Policy.burn");
4646

4747
/** Provide a set of applicable commands to the users.*/
4848
private String suggestedInputs = "";

Source/Cataphract/API/Wraith/FileManagement.java

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private final void navPreviousDirectory()throws Exception
205205
_presentWorkingDirectory.substring(_presentWorkingDirectory.lastIndexOf('|'), _presentWorkingDirectory.length()),"|");
206206

207207
// Check if the present working directory is the restricted user home directory
208-
if (_presentWorkingDirectory.equals(IOStreams.convertFileSeparator(".|Users|Cataphract|")))
208+
if (_presentWorkingDirectory.equals(IOStreams.convertFileSeparator(".|Users|Cataphract|")))
209209
{
210210
// Print an error message if access is denied
211211
IOStreams.printError("Permission Denied.");
@@ -269,7 +269,7 @@ private final void copyMoveEntity(String fileName, String destination, boolean m
269269
// Convert paths from Nion paths to OS specific paths
270270
fileName = IOStreams.convertFileSeparator(fileName);
271271
destination = IOStreams.convertFileSeparator(destination);
272-
272+
273273
// Check if the specified file or directory is valid
274274
if(!checkEntityExistence(fileName) && !checkEntityExistence(destination))
275275
IOStreams.printError("Invalid file name or destination.");
@@ -289,7 +289,7 @@ private final void copyMoveEntity(String fileName, String destination, boolean m
289289
private final void copyMoveHelper(File source, File destination, boolean move)throws Exception
290290
{
291291
// Check if the source is a directory
292-
if (source.isDirectory())
292+
if (source.isDirectory())
293293
{
294294
// Create the destination directory
295295
destination.mkdirs();
@@ -319,34 +319,29 @@ private final void copyMoveHelper(File source, File destination, boolean move)th
319319
*
320320
* @throws Exception Throws any exceptions encountered during runtime.
321321
*/
322-
private final void listEntities()throws Exception
322+
private final void listEntities() throws Exception
323323
{
324324
// Define the format for displaying the directory/file information
325-
String format = "%1$-32s| %2$-24s| %3$-10s\n";
325+
String format = "%1$-32s| %2$-24s| %3$-10s| %4$-32s\n";
326326
String c = "-";
327-
328327
String ls = IOStreams.convertFileSeparator(_presentWorkingDirectory);
329-
330328
// Check if the present working directory exists
331329
if (checkEntityExistence(ls))
332330
{
333331
// Create a File object for the present working directory
334332
File dPath = new File(ls);
335-
336333
// Print a newline for better formatting
337334
System.out.println("\n");
338-
339335
// Format and print the header for the directory listing
340-
String disp = String.format(format, "Directory/File Name", "File Size [In KB]", "Type");
336+
String disp = String.format(format, "Directory/File Name", "File Size [In KB]", "Type", "MD5 Hash");
341337
System.out.println(disp + c.repeat(disp.length()) + "\n");
342338

343339
// Iterate through each file in the directory
344340
for (File file : dPath.listFiles())
345341
{
346342
// Format and print the file or directory information
347-
System.out.format(String.format(format, file.getName().replace(_username, _name), file.length() / 1024 + " KB", file.isDirectory() ? "Directory" : "File"));
343+
System.out.format(format, file.getName().replace(_username, _name), file.length() / 1024 + " KB", file.isDirectory() ? "Directory" : "File", Cryptography.fileToMD5(file));
348344
}
349-
350345
// Print a newline for better formatting
351346
System.out.println();
352347
}
@@ -357,6 +352,7 @@ private final void listEntities()throws Exception
357352
}
358353
}
359354

355+
360356
/**
361357
* Logic to change the present working directory to a given directory
362358
*
@@ -366,7 +362,7 @@ private final void listEntities()throws Exception
366362
private final void changeDirectory(String destination)throws Exception
367363
{
368364
// Check if the destination is the parent directory
369-
if (destination.equals(".."))
365+
if (destination.equals(".."))
370366
{
371367
// Navigate to the previous directory
372368
navPreviousDirectory();
@@ -401,23 +397,23 @@ public void fileManagementLogic()throws Exception
401397
{
402398
// Check if file management policy is enabled or if the user has the necessary privileges
403399
if (new PolicyCheck().retrievePolicyValue("filemgmt").equals("on") || new Login(_username).checkPrivilegeLogic())
404-
{
400+
{
405401
// Check if the user is logged in
406402
if (login())
407403
{
408404
System.out.println(IOStreams.convertToNionSeparator(IOStreams.convertFileSeparator(_presentWorkingDirectory)));
409405
String inputValue = "";
410406
// Loop to continuously read and execute commands until 'exit' is entered
411-
do
407+
do
412408
{
413409
// Read a line of input from the console
414410
inputValue = console.readLine(_name + "@" + IOStreams.convertFileSeparator(_presentWorkingDirectory).replace(_username, _name) + "> ");
415-
411+
416412
// Interpret and execute the command
417413
grinchInterpreter(inputValue);
418-
}
414+
}
419415
while (!inputValue.equalsIgnoreCase("exit"));
420-
}
416+
}
421417
else
422418
IOStreams.printError("Invalid Credentials.");
423419
}
@@ -437,12 +433,12 @@ public void fileManagementLogic(String scriptFileName)throws Exception
437433
if ((new PolicyCheck().retrievePolicyValue("filemgmt").equals("on") && new PolicyCheck().retrievePolicyValue("script").equals("on")) || new Login(_username).checkPrivilegeLogic())
438434
{
439435
// Validate the script file name
440-
if (scriptFileName == null || scriptFileName.equalsIgnoreCase("") || scriptFileName.startsWith(" ") || new File(scriptFileName).isDirectory() || !(new File("./Users/Truncheon/" + _username + "/" + scriptFileName + ".fmx").exists()))
436+
if (scriptFileName == null || scriptFileName.equalsIgnoreCase("") || scriptFileName.startsWith(" ") || new File(scriptFileName).isDirectory() || !(new File(IOStreams.convertFileSeparator(".|Users|Cataphract|" + _username + "|" + scriptFileName + ".fmx")).exists()))
441437
IOStreams.printError("Invalid Script File!");
442438
else
443439
{
444440
// Check if the user is logged in
445-
if (login())
441+
if (login())
446442
{
447443
// Initialize a stream to read the given file
448444
BufferedReader br = new BufferedReader(new FileReader(scriptFileName));
@@ -451,7 +447,7 @@ public void fileManagementLogic(String scriptFileName)throws Exception
451447
String scriptLine;
452448

453449
// Read the script file, line by line
454-
while (!(scriptLine = br.readLine()).equalsIgnoreCase("<EndGrinch>"))
450+
while (!(scriptLine = br.readLine()).equalsIgnoreCase("End Grinch"))
455451
{
456452
// Check if the line is a comment or is blank in the script file and skip the line
457453
if (scriptLine.startsWith("#") || scriptLine.equalsIgnoreCase(""))
@@ -486,9 +482,9 @@ private void grinchInterpreter(String command)throws Exception
486482
{
487483
// Split the command string into an array of command arguments
488484
String[] commandArray = Anvil.splitStringToArray(command);
489-
485+
490486
// Switch statement to handle different commands
491-
switch (commandArray[0].toLowerCase())
487+
switch (commandArray[0].toLowerCase())
492488
{
493489
case "cut":
494490
case "move":

Source/Cataphract/API/Wraith/FileRead.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public void readHelpFile(String helpFile) throws Exception
196196
// Enable help mode
197197
helpMode = true;
198198
// Set the file name
199-
fileName = new File("./Docs/Cataphract/Help/" + helpFile);
199+
fileName = new File(IOStreams.convertFileSeparator(".|Docs|Cataphract|Help|" + helpFile));
200200
// Perform file reading logic
201201
readFileLogic();
202202
}

0 commit comments

Comments
 (0)