Menu

Fix for copytruncate when file is in use

2015-03-25
2015-03-25
  • Geert De Peuter

    Geert De Peuter - 2015-03-25

    Sorry for using the Forums for this purpose, but while testing - noticed that copytruncate will not truncate a file in use.

    When this happens, you get following stack trace

    C:>logrotate.exe -f "logrotate.conf"
    logrotate: Force option set to true
    logrotate: Error truncating file D:\BMCSoftware\BMCARSystem\Arserver\Db\arerror.log
    logrotate: Exception: The process cannot access the file 'D:\BMCSoftware\BMCARSystem\Arserver\Db\arerror.log' because it is being used by another process.
    logrotate: StackTrace: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, St
    ring msgPath, Boolean bFromProxy)
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
    at System.IO.FileStream..ctor(String path, FileMode mode)
    at logrotate.Program.RotateFile(logrotateconf lrc, FileInfo fi)

    Solution is simple and elegant (change to program.cs - line 836)

    Instead of doing this - where the ctor will fail

    FileStream fs = new FileStream(fi.FullName, FileMode.Open);
    fs.SetLength(0);
    fs.Close();

    Change the FileShare to ReadWrite :

    FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
    fs.SetLength(0);
    fs.Close();

    Cheers,
    -- Geert

     
    • Ken Salter

      Ken Salter - 2015-03-25

      Thanks for this...will include this fix in next update.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.