From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS31976 209.132.180.0/23 X-Spam-Status: No, score=-3.7 required=3.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD shortcircuit=no autolearn=ham autolearn_force=no version=3.4.0 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by dcvr.yhbt.net (Postfix) with ESMTP id 2328020282 for ; Fri, 23 Jun 2017 19:46:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754854AbdFWTqO (ORCPT ); Fri, 23 Jun 2017 15:46:14 -0400 Received: from cloud.peff.net ([104.130.231.41]:51058 "HELO cloud.peff.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1754781AbdFWTqN (ORCPT ); Fri, 23 Jun 2017 15:46:13 -0400 Received: (qmail 13790 invoked by uid 109); 23 Jun 2017 19:46:13 -0000 Received: from Unknown (HELO peff.net) (10.0.1.2) by cloud.peff.net (qpsmtpd/0.94) with SMTP; Fri, 23 Jun 2017 19:46:13 +0000 Authentication-Results: cloud.peff.net; auth=none Received: (qmail 18414 invoked by uid 111); 23 Jun 2017 19:46:18 -0000 Received: from sigill.intra.peff.net (HELO sigill.intra.peff.net) (10.0.0.7) by peff.net (qpsmtpd/0.84) with SMTP; Fri, 23 Jun 2017 15:46:18 -0400 Received: by sigill.intra.peff.net (sSMTP sendmail emulation); Fri, 23 Jun 2017 15:46:10 -0400 Date: Fri, 23 Jun 2017 15:46:10 -0400 From: Jeff King To: Michael Haggerty Cc: Junio C Hamano , =?utf-8?B?Tmd1eeG7hW4gVGjDoWkgTmfhu41j?= Duy , Stefan Beller , =?utf-8?B?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , David Turner , Brandon Williams , git@vger.kernel.org Subject: Re: [PATCH v2 22/29] commit_packed_refs(): use a staging file separate from the lockfile Message-ID: <20170623194610.o2eyte4xmlsscrlz@sigill.intra.peff.net> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org On Fri, Jun 23, 2017 at 09:01:40AM +0200, Michael Haggerty wrote: > @@ -522,10 +529,16 @@ int lock_packed_refs(struct ref_store *ref_store, int flags) > timeout_configured = 1; > } > > + /* > + * Note that we close the lockfile immediately because we > + * don't write new content to it, but rather to a separate > + * tempfile. > + */ > if (hold_lock_file_for_update_timeout( > &refs->lock, > refs->path, > - flags, timeout_value) < 0) > + flags, timeout_value) < 0 || > + close_lock_file(&refs->lock)) > return -1; I was wondering whether we'd catch a case which accidentally wrote to the lockfile (instead of the new tempfile, but this close() is a good safety check). > - if (commit_lock_file(&refs->lock)) { > - strbuf_addf(err, "error overwriting %s: %s", > + if (rename_tempfile(&refs->tempfile, refs->path)) { > + strbuf_addf(err, "error replacing %s: %s", > refs->path, strerror(errno)); > goto out; > } So our idea of committing now is the tempfile rename, and then... > @@ -617,9 +640,10 @@ int commit_packed_refs(struct ref_store *ref_store, struct strbuf *err) > goto out; > > error: > - rollback_lock_file(&refs->lock); > + delete_tempfile(&refs->tempfile); > > out: > + rollback_lock_file(&refs->lock); We always rollback the lockfile regardless, because committing it would overwrite our new content with an empty file. There's no real safety against somebody calling commit_lock_file() on it, but it also seems like an uncommon error to make. So this all looks good to me. -Peff