11const fs = require ( 'fs' )
22const Issue = require ( '../../utils/issues' ) . Issue
33const remoteFiles = require ( './remoteFiles' )
4+ const { getOptions } = require ( '../../utils/options' )
45
56/**
67 * Test File
@@ -10,52 +11,60 @@ const remoteFiles = require('./remoteFiles')
1011 * or null and stats if it is.
1112 */
1213function testFile ( file , annexed , dir , callback ) {
13- fs . stat ( file . path , function ( statErr , stats ) {
14- if ( statErr ) {
14+ fs . access ( file . path , function ( accessErr ) {
15+ if ( ! accessErr ) {
16+ // accessible
17+ handleFsAccess ( file , callback )
18+ } else {
19+ // inaccessible
1520 fs . lstat ( file . path , function ( lstatErr , lstats ) {
16- if ( lstatErr ) {
17- callback ( new Issue ( { code : 44 , file : file } ) , stats )
18- } else if ( lstats && lstats . isSymbolicLink ( ) ) {
19- if ( annexed ) {
20- // Set byte retrieval limits based on file type
21- const limit = file . name . includes ( '.nii' ) ? 500 : false
22- // Call process to get remote files
23- // It will call callback with content or error
24- remoteFiles . getAnnexedFile ( file , dir , limit , callback )
25- } else {
26- callback ( new Issue ( { code : 43 , file : file } ) , stats )
27- }
21+ if ( ! lstatErr && lstats && lstats . isSymbolicLink ( ) ) {
22+ // symlink
23+ if ( getOptions ( ) . remoteFiles )
24+ // only follow symlinks when --remoteFiles option is on
25+ handleRemoteAccess ( file , annexed , dir , callback )
26+ else
27+ callback (
28+ new Issue ( {
29+ code : 114 ,
30+ file,
31+ } ) ,
32+ file . stats ,
33+ )
2834 } else {
29- callback ( new Issue ( { code : 44 , file : file } ) , stats )
35+ // inaccessible local file
36+ callback ( new Issue ( { code : 44 , file : file } ) , file . stats )
3037 }
3138 } )
32- } else {
33- fs . access ( file . path , function ( accessErr ) {
34- handleFsAccess ( accessErr , file , stats , callback )
35- } )
3639 }
3740 } )
3841}
3942
40- function handleFsAccess ( accessErr , file , stats , callback ) {
41- if ( ! accessErr ) {
42- process . nextTick ( function ( ) {
43- if ( stats . size === 0 ) {
44- callback (
45- new Issue ( {
46- code : 99 ,
47- file : file ,
48- reason : `Empty files (${ file . path } ) not allowed.` ,
49- } ) ,
50- stats ,
51- )
52- }
53- callback ( null , stats )
54- } )
43+ function handleFsAccess ( file , callback ) {
44+ process . nextTick ( function ( ) {
45+ if ( file . stats . size === 0 ) {
46+ callback (
47+ new Issue ( {
48+ code : 99 ,
49+ file : file ,
50+ reason : `Empty files (${ file . path } ) not allowed.` ,
51+ } ) ,
52+ file . stats ,
53+ )
54+ }
55+ callback ( null , file . stats )
56+ } )
57+ }
58+
59+ function handleRemoteAccess ( file , annexed , dir , callback ) {
60+ if ( annexed ) {
61+ // Set byte retrieval limits based on file type
62+ const limit = file . name . includes ( '.nii' ) ? 500 : false
63+ // Call process to get remote files
64+ // It will call callback with content or error
65+ remoteFiles . getAnnexedFile ( file , dir , limit , callback )
5566 } else {
56- process . nextTick ( function ( ) {
57- callback ( new Issue ( { code : 44 , file : file } ) , stats )
58- } )
67+ callback ( new Issue ( { code : 43 , file : file } ) , file . stats )
5968 }
6069}
6170
0 commit comments