• Stefan Tauner's avatar
    Rigorously check integrity of I/O stream data · 1668770c
    Stefan Tauner authored
    
    Even if fwrite() succeeds the data is not necessarily out of the clib's buffers
    and writing it eventually could fail. Even if the data is flushed out (explicitly by
    fflush() or implicitly by fclose()) the kernel might still hold a buffer.
    
    Previously we have ignored this to a large extent - even in important cases
    like writing the flash contents to a file. The results can be truncated
    images that would brick the respective machine if written back as is (though
    flashrom would not allow that due to a size mismatch). flashrom would not
    indicate the problem in any output - so far we only check the return value
    of fwrite() that is not conclusive.
    
    This patch checks the return values of all related system calls like fclose()
    unless we only read the file and are not really interested in output errors.
    In the latter case the return value is casted to void to document this fact.
    Additionally, this patch explicitly calls fflush() and fsync() (on regular files only)
    to do the best we can to guarantee the read image reaches the disk safely
    and at least inform the user if it did not work.
    
    Corresponding to flashrom svn r1902.
    Signed-off-by: default avatarStefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
    Acked-by: default avatarUrja Rannikko <urjaman@gmail.com>
    1668770c
layout.c 8.46 KB