From patchwork Fri Jun 5 22:13:22 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Nicholson X-Patchwork-Id: 6558311 X-Patchwork-Delegate: tiwai@suse.de Return-Path: X-Original-To: patchwork-alsa-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 837A3C0020 for ; Fri, 5 Jun 2015 22:13:52 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A9F2A207D3 for ; Fri, 5 Jun 2015 22:13:51 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) by mail.kernel.org (Postfix) with ESMTP id 6BA7520778 for ; Fri, 5 Jun 2015 22:13:50 +0000 (UTC) Received: by alsa0.perex.cz (Postfix, from userid 1000) id 1A18F266960; Sat, 6 Jun 2015 00:13:49 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Received: from alsa0.perex.cz (localhost [IPv6:::1]) by alsa0.perex.cz (Postfix) with ESMTP id 71BE0266943; Sat, 6 Jun 2015 00:13:41 +0200 (CEST) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa0.perex.cz (Postfix, from userid 1000) id 08010266950; Sat, 6 Jun 2015 00:13:39 +0200 (CEST) Received: from mail-qk0-f178.google.com (mail-qk0-f178.google.com [209.85.220.178]) by alsa0.perex.cz (Postfix) with ESMTP id CF938266938 for ; Sat, 6 Jun 2015 00:13:32 +0200 (CEST) Received: by qkhq76 with SMTP id q76so47553619qkh.2 for ; Fri, 05 Jun 2015 15:13:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=etDbFyhRSTXbKT3prVVF5LLDGd8dzQfhbBOIhFr3sRY=; b=dhu0PotkZpDBcPxhpcLwvmfMGH2jzCthRc4GpYeXPG3TtEI28tFKIUbF4xzGPxmGaJ iGy3eoIstafip/fLMwrAxdxToKQp7XOLfAMnpiAJTyk4JOP7CnkG+4GJU1C3DOo2ieRu 6JYEl04PIsuK5OT+WHusnX8/lqzgFXj1pkGmaLPzHgRvmlsNJSkj85/k0iUb4j4I+t3b IIOmAGN0153cwrffjvEgeL5Culcu4AypAP2TWiIBTy0JU62IfpGQaQYNlqjYjk5Jfw8K 6A2oOEpn+cqU974I43hLJ9XcEPbrIga9Kl9V7qUnFXqD2wX2wPaXsDPagUNnyJfyq9DU 1utA== X-Gm-Message-State: ALoCoQlcUBfcjj8a0XuJ23U3JIKQJVApkI1ELsjM3J53Oys4qSJ8L0OUwx+V4tWLS6xq4L0JZ/J7 X-Received: by 10.140.232.3 with SMTP id d3mr7043129qhc.46.1433542411971; Fri, 05 Jun 2015 15:13:31 -0700 (PDT) Received: from midnight.endlessm-sf.com ([204.28.125.50]) by mx.google.com with ESMTPSA id c73sm3394774qka.24.2015.06.05.15.13.30 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 05 Jun 2015 15:13:30 -0700 (PDT) From: Dan Nicholson To: alsa-devel@alsa-project.org Date: Fri, 5 Jun 2015 15:13:22 -0700 Message-Id: <1433542402-665-1-git-send-email-nicholson@endlessm.com> X-Mailer: git-send-email 1.9.3 In-Reply-To: <1433541647-300-1-git-send-email-nicholson@endlessm.com> References: <1433541647-300-1-git-send-email-nicholson@endlessm.com> Subject: [alsa-devel] [PATCH] alsactl: Try to create state file directory X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: alsa-devel-bounces@alsa-project.org Sender: alsa-devel-bounces@alsa-project.org X-Virus-Scanned: ClamAV using ClamSMTP Try to create the directory for the state file when saving so we don't depend on it being created ahead of time. This only checks for failures on existing directories and doesn't try to create the leading directories or workaround any other errors. This should catch the common case where /var/lib exists, but /var/lib/alsa doesn't. Signed-off-by: Dan Nicholson --- v2: I forgot to set err if mkdir failed. alsactl/state.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/alsactl/state.c b/alsactl/state.c index 3908ec4..faa1579 100644 --- a/alsactl/state.c +++ b/alsactl/state.c @@ -27,6 +27,9 @@ #include #include #include +#include +#include +#include #include #include "alsactl.h" @@ -1544,6 +1547,7 @@ int save_state(const char *file, const char *cardname) snd_output_t *out; int stdio; char *nfile = NULL; + char *filedir = NULL; int lock_fd = -EINVAL; err = snd_config_top(&config); @@ -1553,6 +1557,9 @@ int save_state(const char *file, const char *cardname) } stdio = !strcmp(file, "-"); if (!stdio) { + char *tmp; + mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; + nfile = malloc(strlen(file) + 5); if (nfile == NULL) { error("No enough memory..."); @@ -1561,6 +1568,22 @@ int save_state(const char *file, const char *cardname) } strcpy(nfile, file); strcat(nfile, ".new"); + filedir = strdup(file); + if (filedir == NULL) { + error("Not enough memory..."); + err = -ENOMEM; + goto out; + } + tmp = strrchr(filedir, '/'); + if (tmp && tmp != filedir) { + *tmp = '\0'; + if (mkdir(filedir, mode) != 0 && errno != EEXIST) { + error("Could not create directory %s: %s", + filedir, strerror(errno)); + err = -errno; + goto out; + } + } lock_fd = state_lock(file, 10); if (lock_fd < 0) { err = lock_fd; @@ -1640,6 +1663,7 @@ out: if (!stdio && lock_fd >= 0) state_unlock(lock_fd, file); free(nfile); + free(filedir); snd_config_delete(config); snd_config_update_free_global(); return err;