Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
implements stat
  • Loading branch information
thecabinet committed Jan 21, 2013
1 parent 98bc4b7 commit 65301a3
Show file tree
Hide file tree
Showing 5 changed files with 586 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -21,7 +21,7 @@ helper.pbc: t/helper.nqp QASTJASTCompiler.pbc

bin: $(JAVAS)
perl -MExtUtils::Command -e mkpath bin
javac -cp 3rdparty/bcel/bcel-5.2.jar -d bin $(JAVAS)
javac -source 1.7 -cp 3rdparty/bcel/bcel-5.2.jar -d bin $(JAVAS)

test: all
prove --exec=nqp t/*.t
Expand Down
3 changes: 0 additions & 3 deletions docs/LHF.md
Expand Up @@ -7,9 +7,6 @@ to Ops. existspos is implementable in terms of elems and <. Note that if the val
passed in is negative, it should add the element count to it. Next, add an op
for deletepos, which could be done in terms of splice.

## nqp::stat
Look into how the nqp::stat operation works in NQP on Parrot. Implement it.

## Port xor
The code-gen for QAST::Op type xor needs porting. Potentially a bit fiddly, but
should be mostly transliteration.
3 changes: 3 additions & 0 deletions lib/QAST/JASTCompiler.nqp
Expand Up @@ -1155,6 +1155,9 @@ QAST::OperationsJAST.map_classlib_core_op('positional_bind', $TYPE_OPS, 'bindpos
QAST::OperationsJAST.map_classlib_core_op('associative_get', $TYPE_OPS, 'atkey', [$RT_OBJ, $RT_STR], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('associative_bind', $TYPE_OPS, 'bindkey', [$RT_OBJ, $RT_STR, $RT_OBJ], $RT_OBJ, :tc);

# I/O opcodes
QAST::OperationsJAST.map_classlib_core_op('stat', $TYPE_OPS, 'stat', [$RT_STR, $RT_INT], $RT_INT);

# terms
QAST::OperationsJAST.map_classlib_core_op('time_i', $TYPE_OPS, 'time_i', [], $RT_INT);
QAST::OperationsJAST.map_classlib_core_op('time_n', $TYPE_OPS, 'time_n', [], $RT_NUM);
Expand Down
177 changes: 177 additions & 0 deletions src/org/perl6/nqp/runtime/Ops.java
@@ -1,9 +1,15 @@
package org.perl6.nqp.runtime;

import java.math.BigInteger;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;
import java.util.HashMap;

import org.perl6.nqp.sixmodel.*;
Expand Down Expand Up @@ -31,6 +37,177 @@ public static String say(String v) {
System.out.println(v);
return v;
}

/* I/O opcodes */
public static final int STAT_EXISTS = 0;
public static final int STAT_FILESIZE = 1;
public static final int STAT_ISDIR = 2;
public static final int STAT_ISREG = 3;
public static final int STAT_ISDEV = 4;
public static final int STAT_CREATETIME = 5;
public static final int STAT_ACCESSTIME = 6;
public static final int STAT_MODIFYTIME = 7;
public static final int STAT_CHANGETIME = 8;
public static final int STAT_BACKUPTIME = 9;
public static final int STAT_UID = 10;
public static final int STAT_GID = 11;
public static final int STAT_ISLNK = 12;
public static final int STAT_PLATFORM_DEV = -1;
public static final int STAT_PLATFORM_INODE = -2;
public static final int STAT_PLATFORM_MODE = -3;
public static final int STAT_PLATFORM_NLINKS = -4;
public static final int STAT_PLATFORM_DEVTYPE = -5;
public static final int STAT_PLATFORM_BLOCKSIZE = -6;
public static final int STAT_PLATFORM_BLOCKS = -7;

public static long stat(String filename, long status) {
long rval = -1;

switch ((int) status) {
case STAT_EXISTS:
rval = new File(filename).exists() ? 1 : 0;
break;

case STAT_FILESIZE:
rval = new File(filename).length();
break;

case STAT_ISDIR:
try {
rval = (Boolean) Files.getAttribute(Paths.get(filename), "basic:isDirectory") ? 1 : 0;
} catch (Exception e) {
rval = -1;
}
break;

case STAT_ISREG:
try {
rval = (Boolean) Files.getAttribute(Paths.get(filename), "basic:isRegularFile") ? 1 : 0;
} catch (Exception e) {
rval = -1;
}
break;

case STAT_ISDEV:
try {
rval = (Boolean) Files.getAttribute(Paths.get(filename), "basic:isOther") ? 1 : 0;
} catch (Exception e) {
rval = -1;
}
break;

case STAT_CREATETIME:
try {
rval = ((Number) Files.getAttribute(Paths.get(filename), "basic:creationTime")).longValue();
} catch (Exception e) {
rval = -1;
}
break;

case STAT_ACCESSTIME:
try {
rval = ((FileTime) Files.getAttribute(Paths.get(filename), "basic:lastAccessTime")).to(TimeUnit.SECONDS);
} catch (Exception e) {
rval = -1;
}
break;

case STAT_MODIFYTIME:
try {
rval = ((FileTime) Files.getAttribute(Paths.get(filename), "basic:lastModifiedTime")).to(TimeUnit.SECONDS);
} catch (Exception e) {
rval = -1;
}
break;

case STAT_CHANGETIME:
try {
rval = ((FileTime) Files.getAttribute(Paths.get(filename), "unix:ctime")).to(TimeUnit.SECONDS);
} catch (Exception e) {
rval = -1;
}
break;

case STAT_BACKUPTIME:
rval = -1;
break;

case STAT_UID:
try {
rval = ((Number) Files.getAttribute(Paths.get(filename), "unix:uid")).longValue();
} catch (Exception e) {
rval = -1;
}
break;

case STAT_GID:
try {
rval = ((Number) Files.getAttribute(Paths.get(filename), "unix:gid")).longValue();
} catch (Exception e) {
rval = -1;
}
break;

case STAT_ISLNK:
try {
rval = (Boolean) Files.getAttribute(Paths.get(filename), "basic:isSymbolicLink", LinkOption.NOFOLLOW_LINKS) ? 1 : 0;
} catch (Exception e) {
rval = -1;
}
break;

case STAT_PLATFORM_DEV:
try {
rval = ((Number) Files.getAttribute(Paths.get(filename), "unix:dev")).longValue();
} catch (Exception e) {
rval = -1;
}
break;

case STAT_PLATFORM_INODE:
try {
rval = ((Number) Files.getAttribute(Paths.get(filename), "unix:ino")).longValue();
} catch (Exception e) {
rval = -1;
}
break;

case STAT_PLATFORM_MODE:
try {
rval = ((Number) Files.getAttribute(Paths.get(filename), "unix:mode")).longValue();
} catch (Exception e) {
rval = -1;
}
break;

case STAT_PLATFORM_NLINKS:
try {
rval = ((Number) Files.getAttribute(Paths.get(filename), "unix:nlink")).longValue();
} catch (Exception e) {
rval = -1;
}
break;

case STAT_PLATFORM_DEVTYPE:
try {
rval = ((Number) Files.getAttribute(Paths.get(filename), "unix:rdev")).longValue();
} catch (Exception e) {
rval = -1;
}
break;

case STAT_PLATFORM_BLOCKSIZE:
throw new UnsupportedOperationException("STAT_PLATFORM_BLOCKSIZE not supported");

case STAT_PLATFORM_BLOCKS:
throw new UnsupportedOperationException("STAT_PLATFORM_BLOCKS not supported");

default:
break;
}

return rval;
}

/* Lexical lookup in current scope. */
public static long getlex_i(CallFrame cf, int i) { return cf.iLex[i]; }
Expand Down

0 comments on commit 65301a3

Please sign in to comment.