Design the basic file system

  • Thread starter Yadagiri Rao KP
  • Start date
Y

Yadagiri Rao KP

Sir,
I am designing a file system as part of the assignment.The
requirements are small.
The file system should work similarly as basic unix commands .
create a directory ( mkdir)
change the directory ----cd
delete --rm
open -opens the file
close -closes the file
read - reads from file
write -writes in to file
copy ---- copy the contents (cp)
move ---- move the contents(mv)
format --- mounts
rename ---- renames the file
reading free blocks
listing the files in the current direcctory(equal to ls command)

I have been a basic skeleton and when run it goes in to the
dosprompt,I mean shell ---"\>" ---waiting for the user to give
input.
Now If I type any command after \> like ls ,then it should list all
the files and directories in the current directory.

I have to concentrate on the methods/commands mentioned above.

Pls guide me - on any of the commands.
Regards
Chintu
 
R

Roedy Green

I have to concentrate on the methods/commands mentioned above.

Split the problem in two. First get some methods that do what each of
your commands does.

Then write a simple parser that reads a line and figures out what
command and arguments are.

In the interim, you can use Javac as your parser. Just create scripts
with Java calls to your methods, similar to how Batik works below:


/* Batik file deleter */
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;

/**
* Batik is a class for writing command line scripts, primarily to
delete files.
* Unfortunately Java has no support for file attributes.
* Java seems to treat hidden files as if they were not hidden.
*
* @author copyright (c) 1999-2003 Roedy Green, Canadian Mind Products
* Shareware that may be copied and used freely for any purpose
* but military.
*
* Roedy Green
* Canadian Mind Products
* #327 - 964 Heywood Avenue
* Victoria, BC Canada V8V 2Y5
* tel: (250) 361-9093
* mailto:[email protected]
* http://mindprod.com CHECK HERE FOR LATEST VERSION!!
*
* Futures:
* - fast file copy and xcopy
* - copy to A: that checks sufficient space before doing all the
work.
* - design so that your script is an extension of the Batik class,
* rather than built-in to it.
* - check status of spawned commands.
* - check status of various file commands, invent an exception
*
* Version 1.0 1998 August 18
* Version 1.1 2001 March 1 : redo with FilenameFilter logic
* Version 1.2 2001 March 13: clean up JavaDoc
* add support for avoidDirs.
* Version 1.3 2002 March 11: add more dirs to clean, NT -> W2K
*/

public class Batik
{
/**
* Standard main, cleans all drives of junk files.
*
* @param args not used
*/
public static void main (String args[])
{
cleanAllRoedy();
System.exit(0);
} // end main


// You might want to configure these constants:

/**
* In debug mode, no files are actually deleted.
* Best to test your changes in debug mode first.
*/
private static final boolean debugging = false;

/**
* exec command to invoke Windows Command processor, customised for
my machine.
*/
private static String invokeCommand = "command /E:1900 /C ";
/**
* exec command to invoke NT command processor customised for my
machine.
*/
private static String invokeCmd = "cmd /E:1900 /C ";
/**
* exec command to invoke 4DOS customised for my machine.
*/
private static String invokeForDOS = "C:\\4DOS\\4DOS601.COM
/E:1900 /C ";
/**
* exec command to invoke 4NT customised for my machine.
*/
private static String invokeForNT = "C:\\4NT401\\4NT.EXE /E:1900
/C ";

/**
* The shell I am using right now, namely 4NT.
*/
private static String invokeShell = invokeForNT;


/**
* CUSTOM Script to clean all my drives of junk, as defined by me.
* You would modify this script section to suit your machine
* and recompile.
* I repeat, you need to extensively modify this for your own use!!
*/
public static void cleanAllRoedy ()
{

// define directories I want left untouched. There is no point
is combing
// jdk for trash. There won't be any.
avoidDirs( new String[] {
"c:\\j2sdk",
"c:\\j2sdk1.4.0",
"c:\\j2sdk1.4.1",
"c:\\j2sdk1.4.2",
"c:\\winnt\\system32"});

try
{
// Do special clearing first, wiping various temp directories
of
// all files and subdirs.
// None of these methods will remove read-only or system
files,
// but they will remove hidden files.

System.out.println();
System.out.println("Clearing temp and cache directories...");

// Some of these directories are hidden, but we can delete
contents anyway

deleteDirContents(new File("C:\\Documents and
Settings\\Administrator\\Application
Data\\EmTec\\SpamDetective\\trash"), true);
deleteDirContents(new File("C:\\Documents and
Settings\\Administrator\\Application
Data\\opera\\opera7\\mail\\cache"), true);
deleteDirContents(new File("C:\\Documents and
Settings\\Administrator\\Application
Data\\opera\\opera7\\profile\\cache4"), true);
deleteDirContents(new File("C:\\Documents and
Settings\\Administrator\\Local Settings\\temp"), true);
deleteDirContents(new File("C:\\Documents and
Settings\\Administrator\\Local Settings\\Temporary Internet Files"),
true);
deleteDirContents(new File("C:\\Documents and
Settings\\Administrator\\My Documents"), true);
deleteDirContents(new File("C:\\Documents and
Settings\\Administrator\\recent"), true);
deleteDirContents(new File("C:\\Documents and
Settings\\Administrator\\Application
Data\\EmTec\\SpamDetective\\trash"), true);


deleteDirContents(new File("c:\\program
files\\Eudora\\Plugins\\Esp\\temp"), true);
deleteDirContents(new File("c:\\program
files\\Opera7\\cache4"), true);
deleteDirContents(new File("c:\\program
files\\Opera7\\download"), true);

deleteDirContents(new File("c:\\temp"), true);
deleteDirContents(new File("c:\\tmp"), true);
deleteDirContents(new File("c:\\vslick\\backup"), true);
deleteDirContents(new File("c:\\winnt\\temp"), true);

deleteDirContents(new File("e:\\program
files\\agent\\data\\temp"), true);
deleteDirContents(new File("e:\\program
files\\netscape\\users\\roedy\\cache"), true);
deleteDirContents(new File("e:\\program
files\\staroffice\\user\\temp"), true);
deleteDirContents(new File("e:\\temp"), true);
deleteDirContents(new File("e:\\tmp"), true);
deleteDirContents(new File("e:\\vslick\\backup"), true);

}
catch ( Exception e )
{
java.awt.Toolkit.getDefaultToolkit().beep();
System.out.println("Problem clearing temp directories.");
System.out.println(e);
e.printStackTrace();
}
try
{
// clear junk extensions
System.out.println();
System.out.println("clearing c:\\ab1\\util of remnants");

junk( new String[] {"temp", "temp1", "temp2"}, new String[]
{"csm", "lst", "map", "obj"});

cleanDir(new File("c:\\ab1\\util"), true);

System.out.println("clearing chkdsk remnants");

// Wipe out files of form temp.* temp1.* and temp2.*,
// e.g. temp.txt temp1.dat temp2. but not template.txt.
// Wipe out files of form *.chk, *.s_dd
junk( new String[] {"temp", "temp1", "temp2"}, new String[]
{"chk", "s_dd"});

cleanDir(new File("D:\\."), false);

System.out.println("clearing all drives of temp?.*, *.bak,
*.tmp, *.temp");

junk ( new String[] {"temp", "temp1", "temp2"}, new String[]
{"bak", "tmp", "temp"});

cleanDrives(new char[] {'c','e'} );

System.out.println();
}
catch ( Exception e )
{
java.awt.Toolkit.getDefaultToolkit().beep();
System.out.println("Problem removing junk files from C: and
E:.");
System.out.println(e);
e.printStackTrace();
}
try
{
// rebuild 4NT quick directory index
System.out.println();
System.out.println("rebuilding 4NT directory...");
forNT("CDD /S CE ");
}
catch ( Exception e )
{
java.awt.Toolkit.getDefaultToolkit().beep();
System.out.println("Problem rebuilding 4NT directory.");
System.out.println(e);
e.printStackTrace();
}

try
{
// clear recycle bins. Do last to rid stuff we just deleted.
System.out.println();
System.out.println("clearing standard recycle bins, Norton
Protected Files must be cleared manually...");
deleteDirContents(new File("c:\\Recycler"), true);
deleteDirContents(new File("e:\\Recycler"), true);

}
catch ( Exception e )
{
java.awt.Toolkit.getDefaultToolkit().beep();
System.out.println("Problem clearing recycle bins.");
System.out.println(e);
e.printStackTrace();
}


System.out.println();
System.out.println("Done");
} // end cleanAllRoedy



/**
* CUSTOM Script to clean all my drives of junk, as defined by me.
* You would modify this script section to suit your machine
* and recompile.
* I repeat, you need to extensively modify this for your own use!!
*/
public static void cleanAllGeneva ()
{

// define directories I want left untouched. There is no point
is combing
// jdk for trash. There won't be any.
avoidDirs( new String[]
{"c:\\j2sdk1.4.0","c:\\windows\\system32"});

try
{
// Do special clearing first, wiping various temp directories
of
// all files and subdirs.
// None of these methods will remove read-only or system
files,
// but they will remove hidden files.

System.out.println();
System.out.println("Clearing temp and cache directories...");

// Some of these directories are hidden, but we can delete
contents anyway
deleteDirContents(new File("c:\\program
files\\cast\\bobby3.2\\tmp"), true);
deleteDirContents(new File("c:\\program files\\Common
Files\\Adobe\\Picture CD Catlogs\\temp"), true);
deleteDirContents(new File("c:\\program
files\\Gnotella\\temp"), true);
deleteDirContents(new File("c:\\program files\\juno\\tmp"),
true);
deleteDirContents(new File("c:\\program files\\Music
Match\\Music Match\\Jukebox 4\\TEMP"), true);
deleteDirContents(new File("c:\\program
files\\netscape\\users\\geni\\cache"), true);
deleteDirContents(new File("c:\\program
files\\Qualcomm\\Eudora\\plugins\\Esp\\Temp"), true);
deleteDirContents(new File("c:\\program
files\\Real\\RealJukebox\\temp"), true);
deleteDirContents(new File("c:\\temp"), true);
deleteDirContents(new File("c:\\tmp"), true);
deleteDirContents(new File("c:\\WINDOWS\\TEMP"), true);

}
catch ( Exception e )
{
java.awt.Toolkit.getDefaultToolkit().beep();
System.out.println("Problem clearing temp directories.");
System.out.println(e);
e.printStackTrace();
}
try
{
// clear junk extensions
System.out.println();
System.out.println("clearing chkdsk remnants");

// Wipe out files of form temp.* temp1.* and temp2.*,
// e.g. temp.txt temp1.dat temp2. but not template.txt.
// Wipe out files of form *.chk, *.s_dd
junk( new String[] {"temp", "temp1", "temp2"}, new String[]
{"chk", "s_dd"});

System.out.println("clearing C drive of temp?.*, *.bak,
*.tmp, *.temp");

junk ( new String[] {"temp", "temp1", "temp2"}, new String[]
{"bak", "tmp", "temp"});

cleanDrives(new char[] {'c'} );

System.out.println();
}
catch ( Exception e )
{
java.awt.Toolkit.getDefaultToolkit().beep();
System.out.println("Problem removing junk files from C:.");
System.out.println(e);
e.printStackTrace();
}
try
{
// rebuild 4NT quick directory index
System.out.println();
System.out.println("rebuilding 4DOS directory...");
forDOS("CDD /S");
}
catch ( Exception e )
{
java.awt.Toolkit.getDefaultToolkit().beep();
System.out.println("Problem rebuilding 4NT directory.");
System.out.println(e);
e.printStackTrace();
}

try
{
// clear recycle bins. Do last to rid stuff we just deleted.
System.out.println();
System.out.println("clearing standard recycle bin, Norton
Protected Files must be cleared manually...");
deleteDirContents(new File("c:\\Recycler"), true);
}
catch ( Exception e )
{
java.awt.Toolkit.getDefaultToolkit().beep();
System.out.println("Problem clearing recycle bins.");
System.out.println(e);
e.printStackTrace();
}


System.out.println();
System.out.println("Done");
} // end cleanAllGeneva



// code from here on, you probably won't need to change.

/**
* FilenameFilter that accepts only junk files to be deleted.
*/
private static JunkFilter mJunkFilter = new JunkFilter(new
String[]{},new String[]{});

/**
* FilenameFilter that accepts only directories to process.
*/
private static DirFilter mDirFilter = new DirFilter(new
String[]{});

/**
* Defines which directories should be left untouched.
* Stays in effect until you redefine it.
*
* @param pAvoidDirs
* Array of strings defining the fully qualified
directories to be left untouched.
* Directory Strings must be lower case.
*/
public static void avoidDirs(String[] pAvoidDirs)
{
mDirFilter = new DirFilter(pAvoidDirs);
}


/**
* Defines which filenames and extensions are considered junk, to
be deleted.
* Stays in effect until you redefine it.
*
* @param pJunkFilenames
* Array of strings defining the unqualified,
extensionless filenames considered junk.
* i.e. name.* files will be deleted.
* Strings should be lower case.
*
* @param pJunkExtensions
* Array of strings defining unqualified, dotless
filename extensions considered junk.
* i.e. *.ext files will be deleted.
* Strings should be lower case.
*/
public static void junk(String[] pJunkFilenames, String[]
pJunkExtensions)
{
mJunkFilter = new JunkFilter(pJunkFilenames, pJunkExtensions);
}


/**
* Display the fully qualified name of the given directory/file.
*
* @param pFile The file or directory to be displayed.
*/
public static void displayTrueName( File pFile )
{
try
{
System.out.println(pFile.getCanonicalPath());
}
catch ( IOException e )
{
System.out.println(e);
}
} // end displayTrueName

/**
* get rid of junk files on given list of drives.
* Uses current definition of junk and avoidDirs.
*
* @param pDrives array of chars giving drive letters to be
procesesed, not Strings, no colons.
* e.g. new char[]{'c','d'}
*/
public static void cleanDrives (char[] pDrives)
{
// in JDK 1.2 could get list of drives, but we want list to be
cleaned, which is
// presumably a subset.
for ( int i=0; i<pDrives.length; i++ )
{
System.out.println(" drive " + pDrives);
cleanDir(new File(pDrives + ":\\."), true );
} // end for
} // end cleanDrives


/**
* Delete junk files in given fully qualified directory, and all
subdirs.
* uses currerent definition of junk. Wipes out files matching
definition of junk, not dirs.
*
* @param pDir fully qualified directory. Would normally be
some existing directory.
*
* @param pRecursive true if should also clean subdirs of this dir.
*/
public static void cleanDir (File pDir, boolean pRecursive)
{
if ( pDir == null ) return;

// clean out child dirs
if ( pRecursive )
{
// all immediate child subdirs of this dir.
String[] allDirs = pDir.list(mDirFilter);
if ( allDirs != null )
{
for ( int i=0; i<allDirs.length; i++ )
{
cleanDir(new File(pDir, allDirs), true);
}
}
}
// just junk files in this dir, not dirs themselves.
String[] allFiles = pDir.list( mJunkFilter);
if ( allFiles != null )
{
for ( int i=0; i<allFiles.length; i++ )
{
deleteFile(new File(pDir, allFiles));
}
}
}

/**
* Get rid of ALL files and subdirectories
* in given directory, and all subdirs under it,
* not just the junk names and extensions.
* The directory itself is not deleted.
* Uses the current definition of junk and avoidDirs.
*
* @param pDir would normally be an existing directory.
*
* @param pRecursive true if want subdirs deleted as well
*/
public static void deleteDirContents (File pDir, boolean
pRecursive)
{
if ( pDir == null ) return;

// We must empty child subdirs contents before can get rid of
immediate child subdirs
if ( pRecursive )
{
String[] allDirs = pDir.list();
if ( allDirs != null )
{

for ( int i=0; i<allDirs.length; i++ )
{
deleteDirContents(new File(pDir, allDirs), true);
}

}
}
// delete all files and subdirs in this dir
String[] allFiles = pDir.list();
if ( allFiles!= null )
{
for ( int i=0; i<allFiles.length; i++ )
{
deleteFile(new File(pDir, allFiles));
}

}
} // end deleteDirContents


/**
* get rid of this fully qualified file.
* Display message on console about success.
*
* @param pFile fully qualified file that we want to delete.
*/
public static void deleteFile (File pFile)
{
String aFilename = pFile.getAbsolutePath();
boolean success;
if ( debugging )
{
success = true;
}
else
{
success = pFile.delete();
}
if ( success )
{
System.out.println(" => " + aFilename);
}
else
{
System.out.println(" ?? " + aFilename);
}
} // end deleteFile

/**
* wrapper for Runtime.exec.
* You will not see the console output of the spawned command.
*
* @param command fully qualified *.exe or *.com command
*
* @param wait true if you want to wait for this spawned task to
complete.
* false if you want it to run in parallel.
*
* @return
*
* @see exec, shell, command, cmd, forDOS, forNT
*/
public static Process exec (String pCommand, boolean pWait)
{
Process p;
try
{
p = Runtime.getRuntime().exec(pCommand);
}
catch ( IOException e )
{
return null;
}

if ( pWait )
{
try
{
p.waitFor();
}
catch ( InterruptedException e )
{

}
} // end if
return p;
} // end exec


/**
* Submit command to default shell command processor,
* and wait for it to complete.
* @param command
* @see exec, shell, command, cmd, forDOS, forNT
*/
public static Process shell(String command)
{
return exec(invokeShell + command, true);
} // end shell


/**
* Submit command to NT command processor\
* and wait for it to complete.
* @param command
* @see exec, shell, command, cmd, forDOS, forNT
*/
public static Process cmd(String command)
{
return exec(invokeCmd + command, true);
} // end cmd


/**
* Submit command to DOS command processor
* and wait for it to complete.
* @param command
* @see exec, shell, command, cmd, forDOS, forNT
*/
public static Process command (String command)
{
return exec(invokeCommand + command, true);
} // end command

/**
* Submit command to 4DOS command processor
* and wait for it to complete.
* @param command
* @see exec, shell, command, cmd, forDOS, forNT
*/
public static Process forDOS (String command)
{
return exec(invokeForDOS + command, true);
} // end forDOS


/**
* Submit command to 4NT command processor
* @param command
* @see exec, shell, command, cmd, forDOS, forNT
*/
public static Process forNT (String command)
{
return exec(invokeForNT + command, true);
} // end forNT



} // end class Batik



/**
* accept only junk files
*/
class JunkFilter implements FilenameFilter
{
/**
* filenames to be deleted, in lower case.
*/
private HashSet mJunkFilenames;
/**
* Extension to be deleted, stored in lower case.
*/
private HashSet mJunkExtensions;

/**
* Defines which filenames and extensions are considered junk, to
be deleted.
*
* @param pJunkFilenames
* Array of strings defining the unqualified,
extensionless filenames considered junk.
*
* @param pJunkExtensions
* Array of strings defining unqualified, dotless
filename extensions considered junk.
*/
JunkFilter( String[] pJunkFilenames, String[] pJunkExtensions )
{
/* build HashSets containing lists of junk filenames and
extensions */
mJunkFilenames = new HashSet(Arrays.asList(pJunkFilenames));
mJunkExtensions = new HashSet (Arrays.asList(pJunkExtensions));
}
/**
* Accept only junk files to be deleted.
*
* @param pDir the directory in which the file was found.
*
* @param name the name of the file
*
* @return true if and only if the name should be
* included in the file list; false otherwise.
*/
public boolean accept(File pDir, String pName)
{
if ( pDir == null || pName == null ) return false;
if ( new File(pDir, pName).isDirectory() ) return false;
String filename = pName;
String extension = "" ;
int whereDot = pName.lastIndexOf( '.' );

if ( 0 < whereDot && whereDot <= pName.length()-2 )
{
extension = pName.substring(whereDot+1).toLowerCase();
}
if ( 1 < whereDot )
{
filename = pName.substring(0,whereDot).toLowerCase();
}

// test extensions first as it is more likely to be true
return(mJunkExtensions.contains(extension)
|| mJunkFilenames.contains(filename) );
}
} // end class JunkFilter

/**
* Accept only Directories to be processed,
* reject files.
*/
class DirFilter implements FilenameFilter

{

/**
* Directories to be left untouched.
*/
private HashSet mAvoidDirs;
/**
* Defines which directories should be left untouched.
*
* @param pAvoidDirs
* Array of strings defining the fully qualified
directories to be left untouched.
*/

DirFilter( String[] pAvoidDirs )
{
// build HashSet containing list dirs to avoid.
// effectively just mAvoidDirs = new
HashSet(Arrays.asList(pAvoidDirs));
mAvoidDirs = new HashSet(pAvoidDirs.length);

for ( int i=0; i<pAvoidDirs.length; i++ )
{
String aDirname = pAvoidDirs;
try
{
File aFile = new File(aDirname);
aDirname = aFile.getCanonicalPath().toLowerCase();
if ( aFile.exists() )
{
System.out.println("Avoiding directory " + aDirname);
mAvoidDirs.add(aDirname);
}

}
catch ( IOException e )
{
throw new IllegalArgumentException("invalid directory name
" + pAvoidDirs);
}

}
}

/**
* Accept only directories to be processed.

* @param pDir the directory in which the dir/file was found.
*
* @param pName the name of the dir/file
*
* @return true if and only if the name should be
* included in the list; false otherwise.
*/
public boolean accept(File pDir, String pName)
{
try
{
File aFile = new File(pDir, pName);
if ( !aFile.isDirectory() ) return false;
String aDirname = aFile.getCanonicalPath().toLowerCase();
return ! mAvoidDirs.contains(aDirname);
}
catch ( IOException e )
{
return false;
}
}

} // end class DirFilter
 
S

Shripathi Kamath

Yadagiri Rao KP said:
Sir,
I am designing a file system as part of the assignment.The
requirements are small.
The file system should work similarly as basic unix commands .
create a directory ( mkdir)
change the directory ----cd
delete --rm
open -opens the file
close -closes the file
read - reads from file
write -writes in to file
copy ---- copy the contents (cp)
move ---- move the contents(mv)
format --- mounts
rename ---- renames the file
reading free blocks
listing the files in the current direcctory(equal to ls command)

I have been a basic skeleton and when run it goes in to the
dosprompt,I mean shell ---"\>" ---waiting for the user to give
input.
Now If I type any command after \> like ls ,then it should list all
the files and directories in the current directory.

I have to concentrate on the methods/commands mentioned above.

Pls guide me - on any of the commands.


You will probably have better success posting this in a OS group. Unless
you specify what this has to do with Java. The requirements are not
"small", your description of the problem is small.

Designing and implementing a file-system is a major undertaking. Listing
out commands to be typed at a command line that need to be implemented
offers no insight into a design or requirements, not does it describe your
problem domain.

Start with some of the following:

a. Is it to be run under a specific, existing, OS?
b. What hardware does it need to support?
c. What features do you consider "basic" in a file-system?
d. Why not use one of the file-systems already available, or at least port
it?
e. Why does this need to be done or addressed in Java.

From what little you have mentioned, you are best off porting one of the
"free" file systems. What about that approach does not work for you?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top