@@ -1017,45 +1017,25 @@ FS.staticInit();
10171017      } 
10181018      throw  new  FS . ErrnoError ( { { {  cDefs. EPERM  } } } ) ; 
10191019    } , 
1020-     truncate ( path ,  len )  { 
1020+     truncateCommon ( arg ,  len ,   ftruncate )  { 
10211021      if  ( len  <  0 )  { 
10221022        throw  new  FS . ErrnoError ( { { {  cDefs . EINVAL  } } } ) ; 
10231023      } 
10241024      var  node ; 
1025-       if  ( typeof  path  ==  'string' )  { 
1026-         var  lookup  =  FS . lookupPath ( path ,  {  follow : true  } ) ; 
1025+       var  stream ; 
1026+       if  ( ftruncate )  { 
1027+         stream  =  FS . getStreamChecked ( arg ) ; 
1028+         if  ( ( stream . flags  &  { { {  cDefs . O_ACCMODE  } } } )  ===  { { {  cDefs . O_RDONLY } } } )  { 
1029+           throw  new  FS . ErrnoError ( { { {  cDefs . EINVAL  } } } ) ; 
1030+         }   
1031+         node  =  stream . node ; 
1032+       }  else  if  ( typeof  arg  = =  'string' )  { 
1033+         var  lookup =  FS . lookupPath ( arg ,  {  follow : true  } ) ; 
10271034        node  =  lookup . node ; 
10281035      }  else  { 
1029-         node  =  path ; 
1030-       } 
1031-       if  ( ! node . node_ops . setattr )  { 
1032-         throw  new  FS . ErrnoError ( { { {  cDefs . EPERM  } } } ) ; 
1033-       } 
1034-       if  ( FS . isDir ( node . mode ) )  { 
1035-         throw  new  FS . ErrnoError ( { { {  cDefs . EISDIR  } } } ) ; 
1036-       } 
1037-       if  ( ! FS . isFile ( node . mode ) )  { 
1038-         throw  new  FS . ErrnoError ( { { {  cDefs . EINVAL  } } } ) ; 
1039-       } 
1040-       var  errCode  =  FS . nodePermissions ( node ,  'w' ) ; 
1041-       if  ( errCode )  { 
1042-         throw  new  FS . ErrnoError ( errCode ) ; 
1043-       } 
1044-       node . node_ops . setattr ( node ,  { 
1045-         size : len , 
1046-         timestamp : Date . now ( ) 
1047-       } ) ; 
1048-     } , 
1049-     ftruncate ( fd ,  len )  { 
1050-       if  ( len  <  0 )  { 
1051-         throw  new  FS . ErrnoError ( { { {  cDefs . EINVAL  } } } ) ; 
1036+         node =  arg ; 
10521037      } 
1053-       var  stream  =  FS . getStreamChecked ( fd ) ; 
1054-       var  node  =  stream . node ; 
1055-       if  ( ( stream . flags  &  { { {  cDefs . O_ACCMODE  } } } )  ===  { { {  cDefs . O_RDONLY } } } )  { 
1056-         throw  new  FS . ErrnoError ( { { {  cDefs . EINVAL  } } } ) ; 
1057-       } 
1058-       if  ( ! node . node_ops . setattr  &&  ! stream . stream_ops . setattr )  { 
1038+       if  ( ! node . node_ops . setattr  &&  ! stream ?. stream_ops . setattr )  { 
10591039        throw  new  FS . ErrnoError ( { { {  cDefs. EPERM  } } } ) ; 
10601040      } 
10611041      if  ( FS . isDir ( node . mode ) )  { 
@@ -1072,12 +1052,18 @@ FS.staticInit();
10721052        size : len , 
10731053        timestamp : Date . now ( ) 
10741054      } ; 
1075-       if  ( stream . stream_ops . setattr )  { 
1055+       if  ( stream ? .stream_ops . setattr )  { 
10761056        stream . stream_ops . setattr ( stream ,  attrs ) ; 
10771057      }  else  { 
10781058        node . node_ops . setattr ( node ,  attrs ) ; 
10791059      } 
10801060    } , 
1061+     truncate ( path ,  len )  { 
1062+       FS . truncateCommon ( path ,  len ,  false ) ; 
1063+     } , 
1064+     ftruncate ( fd ,  len )  { 
1065+       FS . truncateCommon ( fd ,  len ,  true ) ; 
1066+     } , 
10811067    utime ( path ,  atime ,  mtime )  { 
10821068      var  lookup =  FS . lookupPath ( path ,  {  follow : true  } ) ; 
10831069      var  node  =  lookup . node ; 
0 commit comments