Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

60
LINES

< > BotCompany Repo | #1005310 // Investigating Files [works, reads a few bytes of each file]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (4855L/32K/109K).

!759

concepts.

concept MayReadFile {
  S path;
  long length;
}

// = may read this directory and all files directly in it
concept MayReadDir {
  S path;
}

// First bytes of a file
concept FirstBytes {
  new Ref<MayReadFile> file;
  byte[] data;
}

p {
  loadAndAutoSaveConcepts();
  makeBot();
}

static MayReadFile mayReadFile(File f) {
  ret uniq(MayReadFile, "path", f.getPath(), "length", f.length());
}

synchronized answer {
  if "you may read *" {
    S path = m.unq(0);
    File f = new File(path);
    if (!f.isAbsolute()) ret "Need absolute path";
    if (!f.exists()) ret "Not found: " + path;
    if (f.isDirectory()) {
      uniq(MayReadDir, +path);
      ret "Directory " + quote(path) + " marked as readable";
    } else {
      mayReadFile(f);
      ret "File " + quote(path) + " marked as readable";
    }
  }
  
  if "read dirs" {
    for (MayReadDir dir) {
      for (File f : listFilesOnly(dir.path))
        mayReadFile(f);
    }
    ret "OK, now " + n(countConcepts(MayReadFile), "readable files");
  }
  
  if "read * first bytes" {
    int n = parseInt(m.unq(0));
    for (MayReadFile file) pcall {
      cset(uniq(FirstBytes, +file), "data", loadFirstBytes(file.path, n));
    }
    ret "OK, now have data from " + n(countConcepts(FirstBytes), "files");
  }
}

Author comment

Began life as a copy of #1005307

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

-
Snippet ID: #1005310
Snippet name: Investigating Files [works, reads a few bytes of each file]
Eternal ID of this version: #1005310/1
Text MD5: 80f845cff275acb81aec18a414430a63
Transpilation MD5: 6641a254dcca85a423f85d4936643a64
Author: stefan
Category: javax / a.i.
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-11-01 17:11:47
Source code size: 1347 bytes / 60 lines
Pitched / IR pitched: No / No
Views / Downloads: 515 / 590
Referenced in: