| 1 | package openssh |
| 2 | |
| 3 | import ( |
| 4 | sshfx "github.com/pkg/sftp/internal/encoding/ssh/filexfer" |
| 5 | ) |
| 6 | |
| 7 | const extensionStatVFS = "[email protected]" |
| 8 | |
| 9 | |
| 10 | func RegisterExtensionStatVFS() { |
| 11 | sshfx.RegisterExtendedPacketType(extensionStatVFS, func() sshfx.ExtendedData { |
| 12 | return new(StatVFSExtendedPacket) |
| 13 | }) |
| 14 | } |
| 15 | |
| 16 | |
| 17 | func ExtensionStatVFS() *sshfx.ExtensionPair { |
| 18 | return &sshfx.ExtensionPair{ |
| 19 | Name: extensionStatVFS, |
| 20 | Data: "2", |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | |
| 25 | type StatVFSExtendedPacket struct { |
| 26 | Path string |
| 27 | } |
| 28 | |
| 29 | |
| 30 | func (ep *StatVFSExtendedPacket) Type() sshfx.PacketType { |
| 31 | return sshfx.PacketTypeExtended |
| 32 | } |
| 33 | |
| 34 | |
| 35 | func (ep *StatVFSExtendedPacket) MarshalPacket(reqid uint32, b []byte) (header, payload []byte, err error) { |
| 36 | p := &sshfx.ExtendedPacket{ |
| 37 | ExtendedRequest: extensionStatVFS, |
| 38 | |
| 39 | Data: ep, |
| 40 | } |
| 41 | return p.MarshalPacket(reqid, b) |
| 42 | } |
| 43 | |
| 44 | |
| 45 | func (ep *StatVFSExtendedPacket) MarshalInto(buf *sshfx.Buffer) { |
| 46 | buf.AppendString(ep.Path) |
| 47 | } |
| 48 | |
| 49 | |
| 50 | |
| 51 | NOTE: |
| 52 | func (ep *StatVFSExtendedPacket) MarshalBinary() ([]byte, error) { |
| 53 | size := 4 + len(ep.Path) |
| 54 | |
| 55 | buf := sshfx.NewBuffer(make([]byte, 0, size)) |
| 56 | |
| 57 | ep.MarshalInto(buf) |
| 58 | |
| 59 | return buf.Bytes(), nil |
| 60 | } |
| 61 | |
| 62 | |
| 63 | func (ep *StatVFSExtendedPacket) UnmarshalFrom(buf *sshfx.Buffer) (err error) { |
| 64 | *ep = StatVFSExtendedPacket{ |
| 65 | Path: buf.ConsumeString(), |
| 66 | } |
| 67 | |
| 68 | return buf.Err |
| 69 | } |
| 70 | |
| 71 | |
| 72 | func (ep *StatVFSExtendedPacket) UnmarshalBinary(data []byte) (err error) { |
| 73 | return ep.UnmarshalFrom(sshfx.NewBuffer(data)) |
| 74 | } |
| 75 | |
| 76 | const extensionFStatVFS = "[email protected]" |
| 77 | |
| 78 | |
| 79 | func RegisterExtensionFStatVFS() { |
| 80 | sshfx.RegisterExtendedPacketType(extensionFStatVFS, func() sshfx.ExtendedData { |
| 81 | return new(FStatVFSExtendedPacket) |
| 82 | }) |
| 83 | } |
| 84 | |
| 85 | |
| 86 | func ExtensionFStatVFS() *sshfx.ExtensionPair { |
| 87 | return &sshfx.ExtensionPair{ |
| 88 | Name: extensionFStatVFS, |
| 89 | Data: "2", |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | |
| 94 | type FStatVFSExtendedPacket struct { |
| 95 | Path string |
| 96 | } |
| 97 | |
| 98 | |
| 99 | func (ep *FStatVFSExtendedPacket) Type() sshfx.PacketType { |
| 100 | return sshfx.PacketTypeExtended |
| 101 | } |
| 102 | |
| 103 | |
| 104 | func (ep *FStatVFSExtendedPacket) MarshalPacket(reqid uint32, b []byte) (header, payload []byte, err error) { |
| 105 | p := &sshfx.ExtendedPacket{ |
| 106 | ExtendedRequest: extensionFStatVFS, |
| 107 | |
| 108 | Data: ep, |
| 109 | } |
| 110 | return p.MarshalPacket(reqid, b) |
| 111 | } |
| 112 | |
| 113 | |
| 114 | func (ep *FStatVFSExtendedPacket) MarshalInto(buf *sshfx.Buffer) { |
| 115 | buf.AppendString(ep.Path) |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |
| 120 | NOTE: |
| 121 | func (ep *FStatVFSExtendedPacket) MarshalBinary() ([]byte, error) { |
| 122 | size := 4 + len(ep.Path) |
| 123 | |
| 124 | buf := sshfx.NewBuffer(make([]byte, 0, size)) |
| 125 | |
| 126 | ep.MarshalInto(buf) |
| 127 | |
| 128 | return buf.Bytes(), nil |
| 129 | } |
| 130 | |
| 131 | |
| 132 | func (ep *FStatVFSExtendedPacket) UnmarshalFrom(buf *sshfx.Buffer) (err error) { |
| 133 | *ep = FStatVFSExtendedPacket{ |
| 134 | Path: buf.ConsumeString(), |
| 135 | } |
| 136 | |
| 137 | return buf.Err |
| 138 | } |
| 139 | |
| 140 | |
| 141 | func (ep *FStatVFSExtendedPacket) UnmarshalBinary(data []byte) (err error) { |
| 142 | return ep.UnmarshalFrom(sshfx.NewBuffer(data)) |
| 143 | } |
| 144 | |
| 145 | |
| 146 | |
| 147 | const ( |
| 148 | MountFlagsReadOnly = 0x1 |
| 149 | MountFlagsNoSUID = 0x2 |
| 150 | ) |
| 151 | |
| 152 | |
| 153 | type StatVFSExtendedReplyPacket struct { |
| 154 | BlockSize uint64 |
| 155 | FragmentSize uint64 |
| 156 | Blocks uint64 |
| 157 | BlocksFree uint64 |
| 158 | BlocksAvail uint64 |
| 159 | Files uint64 |
| 160 | FilesFree uint64 |
| 161 | FilesAvail uint64 |
| 162 | FilesystemID uint64 |
| 163 | MountFlags uint64 |
| 164 | MaxNameLength uint64 |
| 165 | } |
| 166 | |
| 167 | |
| 168 | func (ep *StatVFSExtendedReplyPacket) Type() sshfx.PacketType { |
| 169 | return sshfx.PacketTypeExtendedReply |
| 170 | } |
| 171 | |
| 172 | |
| 173 | func (ep *StatVFSExtendedReplyPacket) MarshalPacket(reqid uint32, b []byte) (header, payload []byte, err error) { |
| 174 | p := &sshfx.ExtendedReplyPacket{ |
| 175 | Data: ep, |
| 176 | } |
| 177 | return p.MarshalPacket(reqid, b) |
| 178 | } |
| 179 | |
| 180 | |
| 181 | func (ep *StatVFSExtendedReplyPacket) UnmarshalPacketBody(buf *sshfx.Buffer) (err error) { |
| 182 | p := &sshfx.ExtendedReplyPacket{ |
| 183 | Data: ep, |
| 184 | } |
| 185 | return p.UnmarshalPacketBody(buf) |
| 186 | } |
| 187 | |
| 188 | |
| 189 | func (ep *StatVFSExtendedReplyPacket) MarshalInto(buf *sshfx.Buffer) { |
| 190 | buf.AppendUint64(ep.BlockSize) |
| 191 | buf.AppendUint64(ep.FragmentSize) |
| 192 | buf.AppendUint64(ep.Blocks) |
| 193 | buf.AppendUint64(ep.BlocksFree) |
| 194 | buf.AppendUint64(ep.BlocksAvail) |
| 195 | buf.AppendUint64(ep.Files) |
| 196 | buf.AppendUint64(ep.FilesFree) |
| 197 | buf.AppendUint64(ep.FilesAvail) |
| 198 | buf.AppendUint64(ep.FilesystemID) |
| 199 | buf.AppendUint64(ep.MountFlags) |
| 200 | buf.AppendUint64(ep.MaxNameLength) |
| 201 | } |
| 202 | |
| 203 | |
| 204 | |
| 205 | NOTE: |
| 206 | func (ep *StatVFSExtendedReplyPacket) MarshalBinary() ([]byte, error) { |
| 207 | size := 11 * 8 |
| 208 | |
| 209 | b := sshfx.NewBuffer(make([]byte, 0, size)) |
| 210 | ep.MarshalInto(b) |
| 211 | return b.Bytes(), nil |
| 212 | } |
| 213 | |
| 214 | |
| 215 | func (ep *StatVFSExtendedReplyPacket) UnmarshalFrom(buf *sshfx.Buffer) (err error) { |
| 216 | *ep = StatVFSExtendedReplyPacket{ |
| 217 | BlockSize: buf.ConsumeUint64(), |
| 218 | FragmentSize: buf.ConsumeUint64(), |
| 219 | Blocks: buf.ConsumeUint64(), |
| 220 | BlocksFree: buf.ConsumeUint64(), |
| 221 | BlocksAvail: buf.ConsumeUint64(), |
| 222 | Files: buf.ConsumeUint64(), |
| 223 | FilesFree: buf.ConsumeUint64(), |
| 224 | FilesAvail: buf.ConsumeUint64(), |
| 225 | FilesystemID: buf.ConsumeUint64(), |
| 226 | MountFlags: buf.ConsumeUint64(), |
| 227 | MaxNameLength: buf.ConsumeUint64(), |
| 228 | } |
| 229 | |
| 230 | return buf.Err |
| 231 | } |
| 232 | |
| 233 | |
| 234 | func (ep *StatVFSExtendedReplyPacket) UnmarshalBinary(data []byte) (err error) { |
| 235 | return ep.UnmarshalFrom(sshfx.NewBuffer(data)) |
| 236 | } |