1 module styx2000.extrautil.dir;
2 
3 private {
4 	import std.conv : to;
5 	import std..string : format;
6 	
7 	import styx2000.lowlevel.endianness;
8 	import styx2000.lowlevel.vls;
9 	import styx2000.protobj.styxobject;
10 	
11 	import styx2000.protobj.stat;
12 }
13 
14 public {
15 	import styx2000.protobj.perm;
16 	import styx2000.protobj.qid;
17 }
18 
19 // directory entry representation in some 9P messages
20 class Dir : Stat
21 {
22 	this(
23 		ushort  type    = 0,
24 		uint    dev     = 0,
25 		Qid     qid     = new Qid,
26 		Perm    mode    = new Perm,
27 		uint    atime   = 0,
28 		uint    mtime   = 0,
29 		ulong   length  = 0,
30 		string  name    = "",
31 		string  uid     = "",
32 		string  gid     = "",
33 		string  muid    = ""
34 	)
35 	{
36 		super(type, dev, qid, mode, atime, mtime, length, name, uid, gid, muid);
37 	}
38 		
39 	override ubyte[] pack()
40 	{
41 		return _representation[2..$];
42 	}
43 	
44 	override void unpack(ubyte[] bytes...)
45 	{
46 		auto _contents = VariableLengthSequence.pack(bytes);
47 		return super.unpack(_contents);
48 	}
49 	
50 	override string toString()
51 	{
52 		return format(
53 			`Dir(type=%s, dev=%s, qid=%s, mode=%s, atime=%d, mtime=%d, length=%s, name="%s", uid="%s", gid="%s", muid="%s")`,
54 			_type.to!string,
55 			_dev.to!string,
56 			_qid.to!string,
57 			_mode.to!string,
58 			_atime,
59 			_mtime,
60 			_length,
61 			(_name == "") ? `` : _name,
62 			(_uid  == "") ? `` : _uid,
63 			(_gid  == "") ? `` : _gid,
64 			(_muid == "") ? `` : _muid
65 		);
66 	}
67 	
68 	alias pack this;
69 }
70 
71 /// Convert Stat object to Dir object
72 Dir stat2dir(Stat stat)
73 {
74 	auto _contents = stat.pack;
75 	Dir dir = new Dir;
76 	dir.unpack(_contents[2..$]);
77 	return dir;
78 }
79 
80 /// Convert Dir object to Stat object
81 Stat dir2stat(Dir dir)
82 {
83 	auto _contents = VariableLengthSequence.pack(dir.pack);
84 	Stat stat = new Stat;
85 	stat.unpack(_contents);
86 	return stat;
87 }