move logging from SafeReader into caller

prevent unnecessary logging when looking at headers of files where
hitting the end of the file is expected.
This commit is contained in:
Brian Gough 2017-10-02 15:44:00 +01:00
parent 1e5a6a7adc
commit 392e96b81d
2 changed files with 8 additions and 6 deletions

View file

@ -22,6 +22,7 @@ module.exports = ResourceStateManager =
# files are moved, added, deleted or renamed.
SYNC_STATE_FILE: ".project-sync-state"
SYNC_STATE_MAX_SIZE: 128*1024
saveProjectState: (state, resources, basePath, callback = (error) ->) ->
stateFile = Path.join(basePath, @SYNC_STATE_FILE)
@ -39,8 +40,11 @@ module.exports = ResourceStateManager =
checkProjectStateMatches: (state, basePath, callback = (error, resources) ->) ->
stateFile = Path.join(basePath, @SYNC_STATE_FILE)
SafeReader.readFile stateFile, 128*1024, 'utf8', (err, result) ->
size = @SYNC_STATE_MAX_SIZE
SafeReader.readFile stateFile, size, 'utf8', (err, result, bytesRead) ->
return callback(err) if err?
if bytesRead is size
logger.error file:stateFile, size:size, bytesRead:bytesRead, "project state file truncated"
[resourceList..., oldState] = result?.toString()?.split("\n") or []
newState = "stateHash:#{state}"
logger.log state:state, oldState: oldState, basePath:basePath, stateMatches: (newState is oldState), "checking sync state"

View file

@ -12,16 +12,14 @@ module.exports = SafeReader =
return callback(err) if err?
# safely return always closing the file
callbackWithClose = (err, result) ->
callbackWithClose = (err, result...) ->
fs.close fd, (err1) ->
return callback(err) if err?
return callback(err1) if err1?
callback(null, result)
callback(null, result...)
buff = new Buffer(size, 0) # fill with zeros
fs.read fd, buff, 0, buff.length, 0, (err, bytesRead, buffer) ->
return callbackWithClose(err) if err?
result = buffer.toString(encoding, 0, bytesRead)
if bytesRead is size
logger.error file:file, size:size, bytesRead:bytesRead, "file truncated"
callbackWithClose(null, result)
callbackWithClose(null, result, bytesRead)