1 // Written in the D programming language.
2 
3 /**
4 This module contains a set of various useful helpers for miscellaneous stuff.
5 
6 Copyright: LightHouse Software, 2021
7 License:   $(HTTP https://github.com/aquaratixc/ESL-License, Experimental Software License 1.0).
8 Authors:   Oleg Bakharev,
9 		   Ilya Pertsev   
10 */
11 module styx2000.extrautil.mischelpers;
12 
13 private 
14 {
15 	import std.path : baseName;
16 	import std.file : DirEntry, SpanMode;
17 	import std.stdio : File;
18 	
19 	import styx2000.extrautil.siphash : hash8;
20 	import styx2000.protobj;
21 }
22 
23 /// Create Qid object from DirEntry
24 auto createQid(DirEntry de)
25 {	
26 	return new Qid(
27 		(de.isDir) ? STYX_QID_TYPE.QTDIR : STYX_QID_TYPE.QTFILE,
28 		cast(uint) de.timeLastModified.toUnixTime,
29 		hash8(de.name)
30 	);
31 }
32 
33 /// Convenient helper for creating Qid from path string
34 auto createQid(string path)
35 {
36 	return createQid(
37 		DirEntry(path)
38 	);
39 }
40 
41 /// Create Stat object from DirEntry
42 auto createStat(DirEntry de, ushort type = 0, ushort dev = 0, string uid = "", string gid = "", string muid = "")
43 {
44 	auto qid = createQid(de);
45 	return new Stat(
46 		dev, 
47 		type, 
48 		qid,
49 		new Perm((qid.getType << 24) | (de.attributes & 0x1ff)),
50 		cast(uint) de.timeLastAccessed.toUnixTime,
51 		cast(uint) de.timeLastModified.toUnixTime, 
52 		(de.isDir) ? 0 : de.size, 
53 		baseName(de.name), 
54 		uid, 
55 		gid, 
56 		muid
57 	);
58 }
59 
60 /// Convenient helper for creating Stat object from path string
61 auto createStat(string path, ushort type = 0, ushort dev = 0, string uid = "", string gid = "", string muid = "")
62 {
63 	return createStat(DirEntry(path), type, dev, uid, gid, muid);
64 }