1 // Written in the D programming language.
2 
3 /**
4 This module contains the basic definition of the Dir structure, which is a representation of a folder or file. This structure is not found in the protocol description, but can be useful in the construction of some protocol messages, such as a read message.
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.dir;
12 
13 private {
14 	import std.conv : to;
15 	import std.string : format;
16 	
17 	import styx2000.lowlevel.endianness;
18 	import styx2000.lowlevel.vls;
19 	import styx2000.protobj.styxobject;
20 	
21 	import styx2000.protobj.stat;
22 }
23 
24 public {
25 	import styx2000.protobj.perm;
26 	import styx2000.protobj.qid;
27 }
28 
29 /// Directory entry representation in some 9P messages. The constructor parameters are the same as the constructor parameters of the Stat structure.
30 class Dir : Stat
31 {
32 	/// Contruct from raw values: The constructor parameters are the same as the constructor parameters of the Stat structure.
33 	this(
34 		ushort  type    = 0,
35 		uint    dev     = 0,
36 		Qid     qid     = new Qid,
37 		Perm    mode    = new Perm,
38 		uint    atime   = 0,
39 		uint    mtime   = 0,
40 		ulong   length  = 0,
41 		string  name    = "",
42 		string  uid     = "",
43 		string  gid     = "",
44 		string  muid    = ""
45 	)
46 	{
47 		super(type, dev, qid, mode, atime, mtime, length, name, uid, gid, muid);
48 	}
49 		
50 	/// Pack to byte array	
51 	override ubyte[] pack()
52 	{
53 		return _representation[2..$];
54 	}
55 	
56 	/// Unpack from byte array
57 	override void unpack(ubyte[] bytes...)
58 	{
59 		auto _contents = VariableLengthSequence.pack(bytes);
60 		return super.unpack(_contents);
61 	}
62 	
63 	/// String representation of Dir structure
64 	override string toString()
65 	{
66 		return format(
67 			`Dir(type=%s, dev=%s, qid=%s, mode=%s, atime=%d, mtime=%d, length=%s, name="%s", uid="%s", gid="%s", muid="%s")`,
68 			_type.to!string,
69 			_dev.to!string,
70 			_qid.to!string,
71 			_mode.to!string,
72 			_atime,
73 			_mtime,
74 			_length,
75 			(_name == "") ? `` : _name,
76 			(_uid  == "") ? `` : _uid,
77 			(_gid  == "") ? `` : _gid,
78 			(_muid == "") ? `` : _muid
79 		);
80 	}
81 	
82 	/// Pack to binary data
83 	alias pack this;
84 }
85 
86 /// Convert Stat object to Dir object
87 Dir stat2dir(Stat stat)
88 {
89 	auto _contents = stat.pack;
90 	Dir dir = new Dir;
91 	dir.unpack(_contents[2..$]);
92 	return dir;
93 }
94 
95 /// Convert Dir object to Stat object
96 Stat dir2stat(Dir dir)
97 {
98 	auto _contents = VariableLengthSequence.pack(dir.pack);
99 	Stat stat = new Stat;
100 	stat.unpack(_contents);
101 	return stat;
102 }