1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-12-29 08:39:04 +00:00

Add fs.getCapacity and fs.attributes (#365)

- fs.getCapacity just returns the capacity of the current drive, if
   available. This will be nil on rom mounts.
 - fs.attributes returns an lfs like table of various file attributes.
   Currently, this contains:
    - access, modification, created: When this file was last accessed,
      modified and created. Time is measured in milliseconds since the
      epoch, same as os.epoch("utc") and what is accepted by os.date.
    - size: Same as fs.getSize
    - isDir: same as fs.isDir

Closes #262
This commit is contained in:
Jonathan Coates
2020-02-08 21:04:58 +00:00
committed by GitHub
parent 239bd769df
commit 0ffd5fcf85
12 changed files with 664 additions and 349 deletions

View File

@@ -22,6 +22,10 @@ import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.Map;
import java.util.OptionalLong;
import java.util.function.Function;
import static dan200.computercraft.api.lua.ArgumentHelper.getString;
@@ -76,6 +80,8 @@ public class FSAPI implements ILuaAPI
"getFreeSpace",
"find",
"getDir",
"getCapacity",
"attributes",
};
}
@@ -315,9 +321,8 @@ public class FSAPI implements ILuaAPI
throw new LuaException( e.getMessage() );
}
}
case 14:
case 14: // find
{
// find
String path = getString( args, 0 );
try
{
@@ -329,12 +334,42 @@ public class FSAPI implements ILuaAPI
throw new LuaException( e.getMessage() );
}
}
case 15:
case 15: // getDir
{
// getDir
String path = getString( args, 0 );
return new Object[] { FileSystem.getDirectory( path ) };
}
case 16: // getCapacity
{
String path = getString( args, 0 );
try
{
OptionalLong capacity = m_fileSystem.getCapacity( path );
return new Object[] { capacity.isPresent() ? capacity.getAsLong() : null };
}
catch( FileSystemException e )
{
throw new LuaException( e.getMessage() );
}
}
case 17: // attributes
{
String path = getString( args, 0 );
try
{
BasicFileAttributes attributes = m_fileSystem.getAttributes( path );
Map<String, Object> result = new HashMap<>();
result.put( "modification", attributes.lastModifiedTime().toMillis() );
result.put( "created", attributes.creationTime().toMillis() );
result.put( "size", attributes.isDirectory() ? 0 : attributes.size() );
result.put( "isDir", attributes.isDirectory() );
return new Object[] { result };
}
catch( FileSystemException e )
{
throw new LuaException( e.getMessage() );
}
}
default:
assert false;
return null;