Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFile::size() returning incorrect value when reading through a LAN on Windows.
Forum Updated to NodeBB v4.3 + New Features

QFile::size() returning incorrect value when reading through a LAN on Windows.

Scheduled Pinned Locked Moved General and Desktop
16 Posts 3 Posters 7.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ivan.todorovich
    wrote on last edited by
    #7

    [quote author="Andre" date="1300032579"]

    Yeah, those are very hard to debug issues, I know. What I am wondering though, is if it is really a good idea to keep a file, that is being altered by another process and that you access over a network open for so long. Personally, I would think that that is a recepe for trouble. There are just too many things that can go wrong. Perhaps you could considder using a QFileSystemWatcher instead?

    [/quote]

    Yes, you might be right. Although theoretically it should work..
    I'll try using a QFileSystemWatcher and opening the file only as needed..

    I could even fix this doing something like this: (haven't test it though)
    This way it will only parse lines on size change.

    @
    int CLogWatcher::getNewLines()
    {

    if (!m_file->isOpen()) {
        qWarning() << tr("LogWatcher: Log file is closed.");
        return -1;
    }
    
    // if this is 1, it means the log file has been cleared
    // and we have to start reading from the beggining
    if (m_file->pos() > m_file->size()) {
        qWarning() << tr("LogWatcher: File pointer was bigger than file size. -> %1/%2")
                .arg(m_file->pos())
                .arg(m_file->size());
        m_file->seek(0);
    }
    

    static int oldSize;
    if (m_file->size() > oldSize) {

    int lineCount = 0;
    while (!m_file->atEnd()) {
        QString line = m_file->readLine();
        if (!line.trimmed().isEmpty()) {
            lineCount++;
            emit gotLine(line);
        }
    }
    
    oldSize = m_file->size();
    

    }

    return lineCount;
    

    }
    @

    Since QFile::atEnd() checks for buffered data to return, maybe I'm getting the lines even before the file size attr is updated. (?)
    Sounds strange, but that's the only theory I have right now.

    o_o Sorry for my rusted english.

    1 Reply Last reply
    0
    • I Offline
      I Offline
      ivan.todorovich
      wrote on last edited by
      #8

      [quote author="Andre" date="1300033834"]Just a thought... I assume it isn't an option to have a watcher app run locally on the server? That app could watch the logfile, and provide an interface to connect with over the network (perhaps using QxtRPCPeer or something like that?)

      Might proof more reliable than directly accessing files over the network... I do realize that that would be a work-around, not a solution. [/quote]

      Not an option. In this particular case, the point of using LAN file access it to leave the server intact.
      In a future, I need to be able to support ftp also.

      o_o Sorry for my rusted english.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #9

        [quote author="ivan.todorovich" date="1300034188"][quote author="Andre" date="1300033834"]Just a thought... I assume it isn't an option to have a watcher app run locally on the server? That app could watch the logfile, and provide an interface to connect with over the network (perhaps using QxtRPCPeer or something like that?)

        Might proof more reliable than directly accessing files over the network... I do realize that that would be a work-around, not a solution. [/quote]

        Not an option. In this particular case, the point of using LAN file access it to leave the server intact.
        In a future, I need to be able to support ftp also.[/quote]

        OK, just tossing ideas around :)

        1 Reply Last reply
        0
        • I Offline
          I Offline
          ivan.todorovich
          wrote on last edited by
          #10

          [quote author="ivan.todorovich" date="1300034077"]
          [...]
          Since QFile::atEnd() checks for buffered data to return, maybe I'm getting the lines even before the file size attr is updated. (?)
          Sounds strange, but that's the only theory I have right now.
          [/quote]
          Following this theory, I'm trying this:

          @
          int CLogWatcher::getNewLines()
          {

          if (!m_file->isOpen()) {
              qWarning() << tr("LogWatcher: Log file is closed.");
              return -1;
          }
          
          // if this is 1, it means the log file has been cleared
          // and we have to start reading from the beggining
          if (m_file->pos() > m_file->size()) {
              qWarning() << tr("LogWatcher: File pointer was bigger than file size. -> %1/%2")
                      .arg(m_file->pos())
                      .arg(m_file->size());
              m_file->seek(0);
          }
          
          int lineCount = 0;
          //while (!m_file->atEnd()) {
          while (m_file->pos() < m_file->size()) {
              QString line = m_file->readLine();
              if (!line.trimmed().isEmpty()) {
                  lineCount++;
                  emit gotLine(line);
              }
          }
          
          return lineCount;
          

          }
          @

          So far so good

          o_o Sorry for my rusted english.

          1 Reply Last reply
          0
          • I Offline
            I Offline
            ivan.todorovich
            wrote on last edited by
            #11

            Bleh, same error.

            And to declare it official - I HAVE NO CLUE OF WHY THIS IS HAPPENING >.<

            How the hell can (m_file->pos() > m_file->size()) be TRUE if I only read on (m_file->pos() < m_file->size())
            It makes no sense! (at least to me) :/

            o_o Sorry for my rusted english.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #12

              Perhaps you should file a bugreport?

              1 Reply Last reply
              0
              • I Offline
                I Offline
                ivan.todorovich
                wrote on last edited by
                #13

                [quote author="Andre" date="1300040355"]Perhaps you should file a bugreport? [/quote]

                Yes I will.. thanks for your help!

                o_o Sorry for my rusted english.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #14

                  [quote author="ivan.todorovich" date="1300040952"][quote author="Andre" date="1300040355"]Perhaps you should file a bugreport? [/quote]

                  Yes I will.. thanks for your help![/quote]
                  Sorry I could not come up with a real solution.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #15

                    I suspect it to be some caching problem on the OS level (could be some race condition too).

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      ivan.todorovich
                      wrote on last edited by
                      #16

                      [quote author="Volker" date="1300056406"]I suspect it to be some caching problem on the OS level (could be some race condition too).[/quote]

                      I don't know what you mean by race condition but I agree on the OS caching. Although using the same algorithm works on PHP just fine (using filesize(), fopen(), feof() and fgets()) and on Ruby too (file(), filestats() to find the size, tell() for the cursor pos and readlines()), so maybe the framework should prevent this issue somehow.

                      Tomorrow I'll take a deeper look at QFile's source code to see if I can find something supicious (I doubt it though).

                      Also if the problem persists I'll try a different aproach. (Haven't tried QFileSystemWatcher as suggested by Andre yet..)

                      It's really annoying to debug, after the last modification it failed only ONCE, and then worked for 3-4 hours until I closed it without problems!

                      o_o Sorry for my rusted english.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved