@@ -3,11 +3,7 @@ import fetch from 'node-fetch';
3
3
import { v4 as uuidv4 } from 'uuid' ;
4
4
import ssh2 from 'ssh2' ;
5
5
import { logger } from '../logger' ;
6
- import {
7
- generateFileEntry ,
8
- generateDefaultAttributes ,
9
- generateAttributesForFile ,
10
- } from '../utils' ;
6
+ import { generateFileEntry } from '../utils' ;
11
7
import { PermanentFileSystem } from './PermanentFileSystem' ;
12
8
import type {
13
9
Attributes ,
@@ -156,20 +152,11 @@ export class SftpSessionHandler {
156
152
*/
157
153
public fstatHandler = (
158
154
reqId : number ,
159
- handle : Buffer ,
155
+ itemPath : Buffer ,
160
156
) : void => {
161
157
logger . verbose ( 'SFTP read open file statistics request (SSH_FXP_FSTAT)' ) ;
162
- logger . debug ( 'Request:' , { reqId, handle } ) ;
163
- const file = this . openFiles . get ( handle . toString ( ) ) ;
164
- if ( ! file ) {
165
- logger . info ( 'There is no open file associated with this handle' , { reqId, handle } ) ;
166
- logger . debug ( 'Response: Status (FAILURE)' , { reqId } , SFTP_STATUS_CODE . FAILURE ) ;
167
- this . sftpConnection . status ( reqId , SFTP_STATUS_CODE . FAILURE ) ;
168
- return ;
169
- }
170
- const attrs = generateAttributesForFile ( file ) ;
171
- logger . debug ( 'Response: Attributes' , { reqId, attrs } ) ;
172
- this . sftpConnection . attrs ( reqId , attrs ) ;
158
+ logger . debug ( 'Request:' , { reqId, itemPath } ) ;
159
+ this . genericStatHandler ( reqId , itemPath ) ;
173
160
} ;
174
161
175
162
/**
@@ -253,22 +240,13 @@ export class SftpSessionHandler {
253
240
* Also: Retrieving File Attributes
254
241
* https://datatracker.ietf.org/doc/html/draft-ietf-secsh-filexfer-02#section-6.8
255
242
*/
256
- public lstatHandler = ( reqId : number , handle : Buffer ) : void => {
243
+ public lstatHandler = (
244
+ reqId : number ,
245
+ itemPath : Buffer ,
246
+ ) : void => {
257
247
logger . verbose ( 'SFTP read file statistics without following symbolic links request (SSH_FXP_LSTAT)' ) ;
258
- logger . debug ( 'Request:' , { reqId, handle } ) ;
259
- this . permanentFileSystem . getItemType ( handle . toString ( ) )
260
- . then ( ( fileType ) => {
261
- const attrs = generateDefaultAttributes ( fileType ) ;
262
- logger . debug ( 'Response:' , { reqId, attrs } ) ;
263
- this . sftpConnection . attrs (
264
- reqId ,
265
- attrs ,
266
- ) ;
267
- } )
268
- . catch ( ( ) => {
269
- logger . debug ( 'Response: Status (EOF)' , { reqId } , SFTP_STATUS_CODE . NO_SUCH_FILE ) ;
270
- this . sftpConnection . status ( reqId , SFTP_STATUS_CODE . NO_SUCH_FILE ) ;
271
- } ) ;
248
+ logger . debug ( 'Request:' , { reqId, itemPath } ) ;
249
+ this . genericStatHandler ( reqId , itemPath ) ;
272
250
} ;
273
251
274
252
/**
@@ -277,22 +255,13 @@ export class SftpSessionHandler {
277
255
* Also: Retrieving File Attributes
278
256
* https://datatracker.ietf.org/doc/html/draft-ietf-secsh-filexfer-02#section-6.8
279
257
*/
280
- public statHandler = ( reqId : number , handle : Buffer ) : void => {
258
+ public statHandler = (
259
+ reqId : number ,
260
+ itemPath : Buffer ,
261
+ ) : void => {
281
262
logger . verbose ( 'SFTP read file statistics following symbolic links request (SSH_FXP_STAT)' ) ;
282
- logger . debug ( 'Request:' , { reqId, handle } ) ;
283
- this . permanentFileSystem . getItemType ( handle . toString ( ) )
284
- . then ( ( fileType ) => {
285
- const attrs = generateDefaultAttributes ( fileType ) ;
286
- logger . debug ( 'Response:' , { reqId, attrs } ) ;
287
- this . sftpConnection . attrs (
288
- reqId ,
289
- attrs ,
290
- ) ;
291
- } )
292
- . catch ( ( ) => {
293
- logger . debug ( 'Response: Status (EOF)' , { reqId } , SFTP_STATUS_CODE . NO_SUCH_FILE ) ;
294
- this . sftpConnection . status ( reqId , SFTP_STATUS_CODE . NO_SUCH_FILE ) ;
295
- } ) ;
263
+ logger . debug ( 'Request:' , { reqId, itemPath } ) ;
264
+ this . genericStatHandler ( reqId , itemPath ) ;
296
265
} ;
297
266
298
267
/**
@@ -327,11 +296,11 @@ export class SftpSessionHandler {
327
296
logger . verbose ( 'SFTP canonicalize path request (SSH_FXP_REALPATH)' ) ;
328
297
logger . debug ( 'Request:' , { reqId, relativePath } ) ;
329
298
const resolvedPath = path . resolve ( '/' , relativePath ) ;
330
- this . permanentFileSystem . getItemType ( resolvedPath )
331
- . then ( ( fileType ) => {
299
+ this . permanentFileSystem . getItemAttributes ( resolvedPath )
300
+ . then ( ( attrs ) => {
332
301
const fileEntry = generateFileEntry (
333
302
resolvedPath ,
334
- generateDefaultAttributes ( fileType ) ,
303
+ attrs ,
335
304
) ;
336
305
const names = [ fileEntry ] ;
337
306
logger . debug ( 'Response:' , { reqId, names } ) ;
@@ -397,4 +366,19 @@ export class SftpSessionHandler {
397
366
public symLinkHandler = ( ) : void => {
398
367
logger . verbose ( 'SFTP create symlink request (SSH_FXP_SYMLINK)' ) ;
399
368
} ;
369
+
370
+ private readonly genericStatHandler = ( reqId : number , itemPath : Buffer ) : void => {
371
+ this . permanentFileSystem . getItemAttributes ( itemPath . toString ( ) )
372
+ . then ( ( attrs ) => {
373
+ logger . debug ( 'Response:' , { reqId, attrs } ) ;
374
+ this . sftpConnection . attrs (
375
+ reqId ,
376
+ attrs ,
377
+ ) ;
378
+ } )
379
+ . catch ( ( ) => {
380
+ logger . debug ( 'Response: Status (NO_SUCH_FILE)' , { reqId } , SFTP_STATUS_CODE . NO_SUCH_FILE ) ;
381
+ this . sftpConnection . status ( reqId , SFTP_STATUS_CODE . NO_SUCH_FILE ) ;
382
+ } ) ;
383
+ } ;
400
384
}
0 commit comments