From patchwork Fri Jan 27 17:28:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 1732979 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=JG58KSbM; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4P3Phm72z6z23hr for ; Sat, 28 Jan 2023 04:29:44 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E81883881D16 for ; Fri, 27 Jan 2023 17:29:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E81883881D16 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1674840582; bh=o2tpFjiSTKzuUsNPGft3LdlxIcipwUC8LWTHKf5ayng=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=JG58KSbMzUrF0yWoWuAfQHHy8yeWfW18d1Qf8+IM79+vz384zcxmkji0dfuWx9nnR Ie092QPB+KTnFIjbw9ec7dtwchOkIo0g8YlpOiRuNh9ujBuu4ELpTL01/ONdZ71UD9 UTSqFRAZ7idGHLqeJqM/j2mQiRsNhTatd9NMA11U= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x233.google.com (mail-oi1-x233.google.com [IPv6:2607:f8b0:4864:20::233]) by sourceware.org (Postfix) with ESMTPS id 45945385B50A for ; Fri, 27 Jan 2023 17:28:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 45945385B50A Received: by mail-oi1-x233.google.com with SMTP id o66so4739113oia.6 for ; Fri, 27 Jan 2023 09:28:53 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=o2tpFjiSTKzuUsNPGft3LdlxIcipwUC8LWTHKf5ayng=; b=65EWf1c3Jz6VemebUh8uVwLhr+CMtpeF6hDG+YfyioYTwX8WDldnyEh+ecsXEOi3vD vk6V5LcE0POUAOcfdROda0tO3bOXp2i3d8ZbAmdlkNORk0gUA9zCsBdzlwwYj/AXdwWL LPmKycK8fnuSS1sUYvd5xW3CWRk59tNAcfdOzC+R2YjvffDoK+BXtUXo0xx9KBj7Qdil SZRjs4Gn1pwQvnQOFRODmQaab1129xYkK9F8QaWBmG6wFPmajVyrFGyP9Rzga4hg2kPT qzYa5HZFaZLLZwfgoSho+m/sT+8q8zvT/Y7VIp6up+thr/gxFoCb0xeFshiyuBt+rckK gdmA== X-Gm-Message-State: AO0yUKUwY/DhIBY7J56IcpupMupVot6CuSYMidSwDGBHTMqeGQi/CYYC nlD/Tnv2k6IJwUmPyix1UAnES5Dzv2STY7cVnfE= X-Google-Smtp-Source: AK7set+PeboZbGEb2+zt/utkQg53ZGEYFCTScVWFgoCxmttwz5nJZ5QlPX3vbM1/iqBm7AqkUUwHVA== X-Received: by 2002:a05:6808:1649:b0:35e:d67e:3dcd with SMTP id az9-20020a056808164900b0035ed67e3dcdmr2473131oib.58.1674840531364; Fri, 27 Jan 2023 09:28:51 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c1:7e99:a00f:8f3d:7ba5:11f1]) by smtp.gmail.com with ESMTPSA id r131-20020acada89000000b0035aa617156bsm1803940oig.17.2023.01.27.09.28.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Jan 2023 09:28:49 -0800 (PST) To: libc-alpha@sourceware.org, "Andreas K . Huettel" , Paul Eggert , Florian Weimer Subject: [PATCH v5 3/5] linux: Add __readdir64_unlocked Date: Fri, 27 Jan 2023 14:28:32 -0300 Message-Id: <20230127172834.391311-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230127172834.391311-1-adhemerval.zanella@linaro.org> References: <20230127172834.391311-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 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.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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 Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@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/readdir64.c | 20 +++++-- sysdeps/unix/sysv/linux/readdir64_r.c | 80 ++++++--------------------- 3 files changed, 33 insertions(+), 68 deletions(-) diff --git a/include/dirent.h b/include/dirent.h index 17827176ba..f391476298 100644 --- a/include/dirent.h +++ b/include/dirent.h @@ -21,6 +21,7 @@ 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_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/readdir64.c b/sysdeps/unix/sysv/linux/readdir64.c index db1c6214d8..2327511736 100644 --- a/sysdeps/unix/sysv/linux/readdir64.c +++ b/sysdeps/unix/sysv/linux/readdir64.c @@ -28,15 +28,11 @@ /* Read a directory entry from DIRP. */ struct dirent64 * -__readdir64 (DIR *dirp) +__readdir64_unlocked (DIR *dirp) { struct dirent64 *dp; int saved_errno = errno; -#if IS_IN (libc) - __libc_lock_lock (dirp->lock); -#endif - if (dirp->offset >= dirp->size) { /* We've emptied out our buffer. Refill it. */ @@ -68,6 +64,20 @@ __readdir64 (DIR *dirp) dirp->offset += dp->d_reclen; dirp->filepos = dp->d_off; + return dp; +} + +struct dirent64 * +__readdir64 (DIR *dirp) +{ + struct dirent64 *dp; + +#if IS_IN (libc) + __libc_lock_lock (dirp->lock); +#endif + + dp = __readdir64_unlocked (dirp); + #if IS_IN (libc) __libc_lock_unlock (dirp->lock); #endif diff --git a/sysdeps/unix/sysv/linux/readdir64_r.c b/sysdeps/unix/sysv/linux/readdir64_r.c index 285dc99509..5ae099bde7 100644 --- a/sysdeps/unix/sysv/linux/readdir64_r.c +++ b/sysdeps/unix/sysv/linux/readdir64_r.c @@ -32,89 +32,43 @@ __readdir64_r (DIR *dirp, struct dirent64 *entry, struct dirent64 **result) { struct dirent64 *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 = __getdents64 (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 dirent64 *) &dirp->data[dirp->offset]; + dp = __readdir64_unlocked (dirp); + if (dp == NULL) + break; reclen = dp->d_reclen; + if (reclen <= offsetof (struct dirent64, d_name) + NAME_MAX + 1) + break; - dirp->offset += reclen; - - dirp->filepos = dp->d_off; - - if (reclen > offsetof (struct dirent64, 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 dirent64, d_name) + namelen + 1; - else - { - /* The name is too long. Ignore this file. */ - dirp->errcode = ENAMETOOLONG; - dp->d_ino = 0; - continue; - } + reclen = offsetof (struct dirent64, 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; }