From patchwork Fri Oct 2 17:06:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 40647 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4D17C398E44A; Fri, 2 Oct 2020 17:06:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4D17C398E44A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1601658396; bh=i5w1Ncxkbq1PvhPT78w61pRht1KfXCXubEwqp4OtlvY=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=wMe2YeUxZ6UU5D1jtiTwWai6rbxMTwOLQn+/eIDy1kfdFZa7NLwIrFT8K52Q7tMa4 pGAYPDecoLOyASwVYFcZhQy2avpKUleQgAv6HCwsQdd6SOrsyFGhPLwV5eA/tueCmU Of/Zr2rpP/jR7t+vCY+yf/pLIa44+WuGbkDJ/598= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x842.google.com (mail-qt1-x842.google.com [IPv6:2607:f8b0:4864:20::842]) by sourceware.org (Postfix) with ESMTPS id B758C398E451 for ; Fri, 2 Oct 2020 17:06:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org B758C398E451 Received: by mail-qt1-x842.google.com with SMTP id m23so1843205qtq.4 for ; Fri, 02 Oct 2020 10:06:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=i5w1Ncxkbq1PvhPT78w61pRht1KfXCXubEwqp4OtlvY=; b=At4Eff1FSgb6ttkBBBtNl1ucswZmulFP7/LQki7cujP1n3xf2kT2uTfTgVOHC1K7nE J7hjs8WTwRaWaoRB6RI1ikbpTn8567ZE0PtXUose0uIy076RgHx3ZrJ4X8RM0FJ0y2gn ImkwUw9ddi8LtFZxNiYB5fBctfsPaBGUx/ux1nlzwGeSH6/csC7EbMd8yhEaqo3MB710 noBVbZHKM2bHMZcjDpYa8J0fIARRT6lc8ndF1AiQUL+n7rrMsMwy4kOmzLwxVGA7Tr0S Jgy4j/strUjvFwB6V7T6SRUxejWdFdABkhM7hIfmz3Cc4TynhX7/ME6Lh++LpxAw+Ooy v4jA== X-Gm-Message-State: AOAM533V0Z/80fCqVEenYh05HgxffZsXTbh7Mwh0NG6kfq+K5stJ6nK2 vGLm3V8psnP83IjYB/1kZ+yjCDhlfWPHmQ== X-Google-Smtp-Source: ABdhPJwBe/vwM07TpTYJHlzG1W7D1hbwdFxzDFNvsbe0qNNHp3ZKcC2anzbILAT70nbnaYhOy/kyiw== X-Received: by 2002:ac8:4e0b:: with SMTP id c11mr3308256qtw.37.1601658392775; Fri, 02 Oct 2020 10:06:32 -0700 (PDT) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id d10sm1436891qkk.1.2020.10.02.10.06.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Oct 2020 10:06:32 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2 3/9] linux: Add __readdir_unlocked Date: Fri, 2 Oct 2020 14:06:14 -0300 Message-Id: <20201002170620.1611673-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201002170620.1611673-1-adhemerval.zanella@linaro.org> References: <20201002170620.1611673-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Cc: James Clarke , John Paul Adrian Glaubitz Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" And use it on readdir_r implementation. Checked on i686-linux-gnu. --- include/dirent.h | 1 + sysdeps/unix/sysv/linux/readdir.c | 18 +++++-- sysdeps/unix/sysv/linux/readdir_r.c | 79 +++++++---------------------- 3 files changed, 31 insertions(+), 67 deletions(-) diff --git a/include/dirent.h b/include/dirent.h index fdf4c4a2f1..8325a19e5f 100644 --- a/include/dirent.h +++ b/include/dirent.h @@ -20,6 +20,7 @@ extern DIR *__opendirat (int dfd, const char *__name) attribute_hidden; extern DIR *__fdopendir (int __fd) attribute_hidden; extern int __closedir (DIR *__dirp) attribute_hidden; extern struct dirent *__readdir (DIR *__dirp) attribute_hidden; +extern struct dirent *__readdir_unlocked (DIR *__dirp) attribute_hidden; extern struct dirent64 *__readdir64 (DIR *__dirp); libc_hidden_proto (__readdir64) extern int __readdir_r (DIR *__dirp, struct dirent *__entry, diff --git a/sysdeps/unix/sysv/linux/readdir.c b/sysdeps/unix/sysv/linux/readdir.c index 2e03e66e69..ca2a8964e9 100644 --- a/sysdeps/unix/sysv/linux/readdir.c +++ b/sysdeps/unix/sysv/linux/readdir.c @@ -23,15 +23,11 @@ /* Read a directory entry from DIRP. */ struct dirent * -__readdir (DIR *dirp) +__readdir_unlocked (DIR *dirp) { struct dirent *dp; int saved_errno = errno; -#if IS_IN (libc) - __libc_lock_lock (dirp->lock); -#endif - do { size_t reclen; @@ -75,6 +71,18 @@ __readdir (DIR *dirp) /* Skip deleted files. */ } while (dp->d_ino == 0); + return dp; +} + +struct dirent * +__readdir (DIR *dirp) +{ + struct dirent *dp; + +#if IS_IN (libc) + __libc_lock_lock (dirp->lock); +#endif + dp = __readdir_unlocked (dirp); #if IS_IN (libc) __libc_lock_unlock (dirp->lock); #endif diff --git a/sysdeps/unix/sysv/linux/readdir_r.c b/sysdeps/unix/sysv/linux/readdir_r.c index 0069041394..a01d2845a6 100644 --- a/sysdeps/unix/sysv/linux/readdir_r.c +++ b/sysdeps/unix/sysv/linux/readdir_r.c @@ -25,89 +25,44 @@ __readdir_r (DIR *dirp, struct dirent *entry, struct dirent **result) { struct dirent *dp; size_t reclen; - const int saved_errno = errno; - int ret; __libc_lock_lock (dirp->lock); - do + while (1) { - if (dirp->offset >= dirp->size) - { - /* We've emptied out our buffer. Refill it. */ - - size_t maxread = dirp->allocation; - ssize_t bytes; - - maxread = dirp->allocation; - - bytes = __getdents (dirp->fd, dirp->data, maxread); - if (bytes <= 0) - { - /* On some systems getdents fails with ENOENT when the - open directory has been rmdir'd already. POSIX.1 - requires that we treat this condition like normal EOF. */ - if (bytes < 0 && errno == ENOENT) - { - bytes = 0; - __set_errno (saved_errno); - } - if (bytes < 0) - dirp->errcode = errno; - - dp = NULL; - break; - } - dirp->size = (size_t) bytes; - - /* Reset the offset into the buffer. */ - dirp->offset = 0; - } - - dp = (struct dirent *) &dirp->data[dirp->offset]; + dp = __readdir_unlocked (dirp); + if (dp == NULL) + break; reclen = dp->d_reclen; + if (reclen <= offsetof (struct dirent, d_name) + NAME_MAX + 1) + break; - dirp->offset += reclen; - - dirp->filepos = dp->d_off; - - if (reclen > offsetof (struct dirent, d_name) + NAME_MAX + 1) + /* The record is very long. It could still fit into the caller-supplied + buffer if we can skip padding at the end. */ + size_t namelen = _D_EXACT_NAMLEN (dp); + if (namelen <= NAME_MAX) { - /* The record is very long. It could still fit into the - caller-supplied buffer if we can skip padding at the - end. */ - size_t namelen = _D_EXACT_NAMLEN (dp); - if (namelen <= NAME_MAX) - reclen = offsetof (struct dirent, d_name) + namelen + 1; - else - { - /* The name is too long. Ignore this file. */ - dirp->errcode = ENAMETOOLONG; - dp->d_ino = 0; - continue; - } + reclen = offsetof (struct dirent, d_name) + namelen + 1; + break; } - /* Skip deleted and ignored files. */ + /* The name is too long. Ignore this file. */ + dirp->errcode = ENAMETOOLONG; + dp->d_ino = 0; } - while (dp->d_ino == 0); if (dp != NULL) { *result = memcpy (entry, dp, reclen); entry->d_reclen = reclen; - ret = 0; } else - { - *result = NULL; - ret = dirp->errcode; - } + *result = NULL; __libc_lock_unlock (dirp->lock); - return ret; + return dp != NULL ? 0 : dirp->errcode; } weak_alias (__readdir_r, readdir_r)