eclipse / mosquitto

Eclipse Mosquitto - An open source MQTT broker
https://mosquitto.org
Other
8.93k stars 2.37k forks source link

mosquitto 2.0.10 logging on Windows - Won't log append to log file if log file already exists (i.e. on subsequent starts after the first) #2170

Open NiclasLindgren opened 3 years ago

NiclasLindgren commented 3 years ago

I might be doing something wrong, but when mosquitto starts the when the log file already exists (it was created by mosquitto) it won't log to the end of the file.

If I add code to fclose and fopen the file (instead of mosquitto__fopen) it will work, looking at the code in "restrict_read" path you find that int fd = _open_osfhandle((intptr_t)hfile, 0);

should be (if "a" is used, or OPEN_ALWAYS)

        int fd = _open_osfhandle((intptr_t)hfile, _O_APPEND);

Maybe this should be on a mailing list of sorts as a question first.

NiclasLindgren commented 3 years ago

It should possibly be like this

include

. . int openModeFlags = 0;

        switch (mode[0]) {
        case 'a':
            dwCreationDisposition = OPEN_ALWAYS;
            openModeFlags |= _O_APPEND;
            break;
        case 'r':
            dwCreationDisposition = OPEN_EXISTING;
            break;
        case 'w':
            dwCreationDisposition = CREATE_ALWAYS;
            break;
        default:
            return NULL;
        }
        if (mode[1] != 0) {
            switch (mode[1]) {
            case 't':
                openModeFlags |= _O_TEXT;
                break;
            }
        }

. . .

        int fd = _open_osfhandle((intptr_t)hfile, openModeFlags);

I can make a pull request if you agree