From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 40725 invoked by alias); 19 Dec 2017 15:47:03 -0000 Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com Received: (qmail 40703 invoked by uid 89); 19 Dec 2017 15:47:02 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy=U*corinna-cygwin, corinna-cygwin@cygwin.com, corinnacygwincygwincom, sk:corinna X-HELO: mail-qt0-f175.google.com Received: from mail-qt0-f175.google.com (HELO mail-qt0-f175.google.com) (209.85.216.175) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 19 Dec 2017 15:46:59 +0000 Received: by mail-qt0-f175.google.com with SMTP id 33so24700619qtv.1 for ; Tue, 19 Dec 2017 07:46:59 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=4Ac4XPt+1YksZVXWuriXu6VCvHzrZL71nACBN6v8+Io=; b=P2NUBZ2JSvCa1iBHfScC25J47VL8u4nockrH3kFXfRkvF/cEofGfMC5gZlPajnHEGU S+01DTdDmmrmksp2IiztzZxtgMalHiZYB9UE7uYtx1mbjhcsir2L3YZREzEPKFaO24na COEkonAcBWXydSbOQjsWDVIQ3S/JA2BNwVaAV5z6lueridU29zv9wUA+j9OgUAPmeDVi VsPPNiu9UZRo1vqXNZISfrmB94Fgi3YxPqE6o7xjIbS2k8+Wci8LfwIrQaPAqmjpEJeY +1mSJFeGNPvwJEgeZxIajfuPO1AcRrWgXlt/UONPLYemlTXJulikGOuNq6NSeHVfTsBp Id1A== X-Gm-Message-State: AKGB3mKuaBGfYh1LDHkv108sEXRhV2S4UVpBDizJcg2m9wyX1Rxiy8sn eUpYQQ0As0egz0WlSEjntXsejKlviSCR39kw34YCszu0 X-Google-Smtp-Source: ACJfBovpy/8EBoJuRHlrubLMAOlAmewNuacQKO0LkH1LLVsc+4779ja3RHWLlF97+k1d9sV8h9syShfEhF1LU9ZXkOI= X-Received: by 10.200.46.50 with SMTP id r47mr5436213qta.314.1513698416526; Tue, 19 Dec 2017 07:46:56 -0800 (PST) MIME-Version: 1.0 Received: by 10.237.58.41 with HTTP; Tue, 19 Dec 2017 07:46:24 -0800 (PST) In-Reply-To: <20171219091431.GF11285@calimero.vinschen.de> References: <20171218131035.GB11285@calimero.vinschen.de> <5a385ced.195b9d0a.d434.5400@mx.google.com> <20171219091431.GF11285@calimero.vinschen.de> From: Ivan Kozik Date: Tue, 19 Dec 2017 16:36:00 -0000 Message-ID: Subject: Re: Wrong file position after writing 65537 bytes to block device To: cygwin@cygwin.com Content-Type: text/plain; charset="UTF-8" X-IsSubscribed: yes X-SW-Source: 2017-12/txt/msg00197.txt.bz2 On Tue, Dec 19, 2017 at 9:14 AM, Corinna Vinschen wrote: > Neither glibc nor FreeBSD show this behaviour. Keep in mind that stdio > is designed for buffered I/O. What should happen, basically, is that a > multiple of the stdio buffersize is written and the remainder is kept in > the stdio buffer: > > fwrite(65537) > -> write(65536) > -> store 1 byte in FILE._buf > > ftell calls lseek which returns 65536. It adds the number of bytes > still in the buffer, so it should return 65537. Further fwrite's > seemlessly append to the bytes already written, as expected. ftell > calling fflush and thus setting the current file position to the next > sector boundary breaks this expectation. > > I pushed a patch yesterday and uploaded new developer snapshots to > https://cygwin.com/snapshots/ > > Please test. Thanks, I can confirm that the 2017-12-18 snapshot fixed the test program I posted. What about the harder case where the program calls fflush, though? #include int main(int argc, char *argv[]) { FILE *f = fopen(argv[1], "w"); char x[65536 + 1]; fwrite(x, 1, 65536 + 1, f); fflush(f); printf("%ld", ftell(f)); return 0; } cygwin reports 66048, while Linux reports 65537. In cygwin, if such a write is done in a loop, for example, you can get garbled output on disk. fflush can be visibly unnecessary when done from C, but python3 (where I originally observed the problem) appears to do implicit flushing. If this is annoying to fix and I am the only one who notices, please don't worry about it, I can just write in proper block sizes to block devices. Best regards, Ivan -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple