Tuesday, May 3, 2011

First one!

It will be a first time I add the post here.
Create a framework is not an easy task.
Create a cross-platform application and UI framework is much more difficult one.
The framework that is used for more than 15 years and helped the hundreds of thousands of companies to deliver the services to the their clients is really something.
To find a mistake or a little thingy for improvement sometimes is not a big deal. Much easier to find something in someone's work than in your own.
Here is my finding in Qt framework version 4.6.3 and it applies to the latest 4.7.2 version of Qt.

qfsfileengine_win.cpp, line 1801:
bool QFSFileEngine::setSize(qint64 size)
{
    Q_D(QFSFileEngine);

    if (d->fileHandle != INVALID_HANDLE_VALUE || d->fd != -1) {
        // resize open file
        HANDLE fh = d->fileHandle;
#if !defined(Q_OS_WINCE)
        if (fh == INVALID_HANDLE_VALUE)
            fh = (HANDLE)_get_osfhandle(d->fd);
#endif
        if (fh == INVALID_HANDLE_VALUE)
            return false;
        qint64 currentPos = pos();

        if (seek(size) && SetEndOfFile(fh)) {
            seek(qMin(currentPos, size));
            return true;
        }

        seek(currentPos);
        return false;
    }

    if (!d->nativeFilePath.isEmpty()) {
        // resize file on disk
        QFile file(d->filePath);
        if (file.open(QFile::ReadWrite)) {
            bool ret = file.resize(size);
            if (!ret)
                setError(QFile::ResizeError, file.errorString());
            return ret;
        }
    }
    return false;
}

The almost everything is fine except one thing. Read the function from top to bottom and see what we have.
  1. If the file is open (handle is not INVALID one) then set a new size of file.
  2. If the file is NOT open then OPEN the file and set a new size.
Done?

Nope. The right way to close the file and then boil out. No one outside should care about stuff the function did for its own needs. The function opened the file to set a new size and leave the file handle opened. The current implementation (I believe) will close that kind of opened files in a destructor QFile::~QFile() and therefore there is no visible impact to the end users of that code.

Reading source code is a good habit.

1 comment:

  1. I've got questions:
    + how come I looked that long at the code even though I can't tell what a single symbol of it means?
    + how do I contact the International Association of Buddhism monks to tell I found a perfect meditation technique?
    + is there the International Association of Buddhism monks wondering about brand new meditation techniques?
    + man, how are you doing?

    ReplyDelete