-
Notifications
You must be signed in to change notification settings - Fork 26
/
sftp_attr.js
166 lines (153 loc) · 4.15 KB
/
sftp_attr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
paramikojs.SFTPAttributes = function () {
/*
Create a new (empty) SFTPAttributes object. All fields will be empty.
*/
this._flags = 0;
this.st_size = null;
this.st_uid = null;
this.st_gid = null;
this.st_mode = null;
this.st_atime = null;
this.st_mtime = null;
this.attr = {};
}
paramikojs.SFTPAttributes.prototype = {
/*
Representation of the attributes of a file (or proxied file) for SFTP in
client or server mode. It attemps to mirror the object returned by
C{os.stat} as closely as possible, so it may have the following fields,
with the same meanings as those returned by an C{os.stat} object:
- st_size
- st_uid
- st_gid
- st_mode
- st_atime
- st_mtime
Because SFTP allows flags to have other arbitrary named attributes, these
are stored in a dict named C{attr}. Occasionally, the filename is also
stored, in C{filename}.
*/
FLAG_SIZE : 1,
FLAG_UIDGID : 2,
FLAG_PERMISSIONS : 4,
FLAG_AMTIME : 8,
FLAG_EXTENDED : 0x80000000,
/*
Create an SFTPAttributes object from an existing C{stat} object (an
object returned by C{os.stat}).
@param obj: an object returned by C{os.stat} (or equivalent).
@type obj: object
@param filename: the filename associated with this file.
@type filename: str
@return: new L{SFTPAttributes} object with the same attribute fields.
@rtype: L{SFTPAttributes}
*/
from_stat : function(obj, filename) {
var attr = this;
attr.st_size = obj.st_size;
attr.st_uid = obj.st_uid;
attr.st_gid = obj.st_gid;
attr.st_mode = obj.st_mode;
attr.st_atime = obj.st_atime;
attr.st_mtime = obj.st_mtime;
if (filename) {
attr.filename = filename;
}
return attr;
},
// internals...
_from_msg : function(msg, filename, longname) {
var attr = this;
attr._unpack(msg);
if (filename) {
attr.filename = filename;
}
if (longname) {
attr.longname = longname;
}
return attr;
},
_unpack : function(msg) {
this._flags = msg.get_int();
if (this._flags & this.FLAG_SIZE) {
this.st_size = msg.get_int64();
}
if (this._flags & this.FLAG_UIDGID) {
this.st_uid = msg.get_int();
this.st_gid = msg.get_int();
}
if (this._flags & this.FLAG_PERMISSIONS) {
this.st_mode = msg.get_int();
}
if (this._flags & this.FLAG_AMTIME) {
this.st_atime = msg.get_int();
this.st_mtime = msg.get_int();
}
if (this._flags & this.FLAG_EXTENDED) {
var count = msg.get_int();
for (var x = 0; x < count.length; ++x) {
this.attr[msg.get_string()] = msg.get_string();
}
}
},
_pack : function(msg) {
this._flags = 0;
if (this.st_size) {
this._flags |= this.FLAG_SIZE;
}
if (this.st_uid && this.st_gid) {
this._flags |= this.FLAG_UIDGID;
}
if (this.st_mode) {
this._flags |= this.FLAG_PERMISSIONS;
}
if (this.st_atime && this.st_mtime) {
this._flags |= this.FLAG_AMTIME;
}
var i;
for (i in this.attr) { // lamesauce :-/
break;
}
if (i) {
this._flags |= this.FLAG_EXTENDED;
}
msg.add_int(this._flags);
if (this._flags & this.FLAG_SIZE) {
msg.add_int64(this.st_size);
}
if (this._flags & this.FLAG_UIDGID) {
msg.add_int(this.st_uid);
msg.add_int(this.st_gid);
}
if (this._flags & this.FLAG_PERMISSIONS) {
msg.add_int(this.st_mode);
}
if (this._flags & this.FLAG_AMTIME) {
// throw away any fractional seconds
msg.add_int(this.st_atime);
msg.add_int(this.st_mtime);
}
if (this._flags & this.FLAG_EXTENDED) {
msg.add_int(this.attr.length);
for (var key in this.attr) {
msg.add_string(key);
msg.add_string(this.attr[key]);
}
}
},
_rwx : function(n, suid, sticky) {
if (suid) {
suid = 2;
}
var out = '-r'.charCodeAt(n >> 2) + '-w'.charCodeAt((n >> 1) & 1);
if (sticky) {
out += '-xTt'.charCodeAt(suid + (n & 1));
} else {
out += '-xSs'.charCodeAt(suid + (n & 1));
}
return out;
},
toString : function() {
// todo, implement if necessary
}
};