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).

1  
!759
2  
3  
concepts.
4  
5  
concept MayReadFile {
6  
  S path;
7  
  long length;
8  
}
9  
10  
// = may read this directory and all files directly in it
11  
concept MayReadDir {
12  
  S path;
13  
}
14  
15  
// First bytes of a file
16  
concept FirstBytes {
17  
  new Ref<MayReadFile> file;
18  
  byte[] data;
19  
}
20  
21  
p {
22  
  loadAndAutoSaveConcepts();
23  
  makeBot();
24  
}
25  
26  
static MayReadFile mayReadFile(File f) {
27  
  ret uniq(MayReadFile, "path", f.getPath(), "length", f.length());
28  
}
29  
30  
synchronized answer {
31  
  if "you may read *" {
32  
    S path = m.unq(0);
33  
    File f = new File(path);
34  
    if (!f.isAbsolute()) ret "Need absolute path";
35  
    if (!f.exists()) ret "Not found: " + path;
36  
    if (f.isDirectory()) {
37  
      uniq(MayReadDir, +path);
38  
      ret "Directory " + quote(path) + " marked as readable";
39  
    } else {
40  
      mayReadFile(f);
41  
      ret "File " + quote(path) + " marked as readable";
42  
    }
43  
  }
44  
  
45  
  if "read dirs" {
46  
    for (MayReadDir dir) {
47  
      for (File f : listFilesOnly(dir.path))
48  
        mayReadFile(f);
49  
    }
50  
    ret "OK, now " + n(countConcepts(MayReadFile), "readable files");
51  
  }
52  
  
53  
  if "read * first bytes" {
54  
    int n = parseInt(m.unq(0));
55  
    for (MayReadFile file) pcall {
56  
      cset(uniq(FirstBytes, +file), "data", loadFirstBytes(file.path, n));
57  
    }
58  
    ret "OK, now have data from " + n(countConcepts(FirstBytes), "files");
59  
  }
60  
}

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: 452 / 515
Referenced in: [show references]