From patchwork Fri Jul 20 02:57:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 142423 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp2442036ljj; Thu, 19 Jul 2018 20:05:59 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdkM3szVAiGbTZsNmb4GWUnG4gt6nyFY2hk/fXR0+682L4GpNKxfJIMewvj0FKhYczG2ZZn X-Received: by 2002:a50:b5e6:: with SMTP id a93-v6mr525100ede.94.1532055959755; Thu, 19 Jul 2018 20:05:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532055959; cv=none; d=google.com; s=arc-20160816; b=D5t6D9td4L2MxEVIzOQD6/ueoQVz4lprSV8cnWhsJOb0DQZNReRzh+xia3a9Pd7g6W 24TMmgsqCQMhUMnV3pkxrdzts0OlpWNIYp1F/Q+NtH+xy45MkFfFjdt3oDdrHM+h1qtI xaRlRDxNQys1U7INfhBXzU9d7ONYCtI/8mFximHmcdsmdEj5hjPyVPFix+lVKq2BdVFO mdovS3onfI6vhaFuSVvkkiM5cgUf0oCwhSKJY853iepIi8+OmWi47U/RcgCqjA7UPVIz yCRt8Km50JZsfMh8NmYkYS8dQEpzlvhmFD2GHoC3HetvWOkAQrTu+fPlG5MHaX1WyBKz /5EA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:cc:references:in-reply-to:message-id :date:to:from:dkim-signature:arc-authentication-results; bh=STxayVKeXQmEO/Mq6xtOm2ibwlpyEYRp2gnN7HX0C+o=; b=RIUTexywLkZES2p2AV4/wT4/csRPIbRo7Iol+seRGja7uqtA1cStVVIASygArSQ2Gw Egy9IHZrICeVzlaSEnn2+m4HHwPwilc7zX/BCJiiDqGabTL38whG1xRig3ezjprBhvtW pJM7DEj123jjnn+nmEP8ph0FjioQEkPT0X8f/+YSiWmY2sccutztQA3109eKjDT/xm0T KVufISDwHlJo9lclXTH190tuWS1nXRLoFGHhqZroETj9jTLwleVezbVm85ZqdNsdFPYH 02draCdqzkmWKxG/WanxmT29uqOzkA6TqLXJz++3NEiSajJA1QldJHZeOb/T6wwP27P1 m5+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=OFecIrcp; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.denx.de (dione.denx.de. [81.169.180.215]) by mx.google.com with ESMTP id y25-v6si925510edl.232.2018.07.19.20.05.59; Thu, 19 Jul 2018 20:05:59 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) client-ip=81.169.180.215; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=OFecIrcp; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by lists.denx.de (Postfix, from userid 105) id 64FFFC21E53; Fri, 20 Jul 2018 03:02:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id E32B6C21C4A; Fri, 20 Jul 2018 03:02:08 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id B1465C21E1D; Fri, 20 Jul 2018 02:58:29 +0000 (UTC) Received: from mail-pg1-f193.google.com (mail-pg1-f193.google.com [209.85.215.193]) by lists.denx.de (Postfix) with ESMTPS id 0F60EC21D9A for ; Fri, 20 Jul 2018 02:58:29 +0000 (UTC) Received: by mail-pg1-f193.google.com with SMTP id v13-v6so5508831pgr.10 for ; Thu, 19 Jul 2018 19:58:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=dip1A6GwtL4I4c5LfupKMoK0g2B5ytXLslfNbqfARDo=; b=OFecIrcpCiC3TCXs70ZF3QlZlCWAyzuaC2NdubgYzLqZrNICZkL2kcitfc9uEAA25E 0JadKF+y0EqUeYJKZLV3Pp8XT/7632JskuBhgnFPksE/TB9r9p6Pa+kgbqY/kECnk8ZJ mBft2QUrJ+s7cOxp3AKK/D7VZ7gp/nsvVCjxc= 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; bh=dip1A6GwtL4I4c5LfupKMoK0g2B5ytXLslfNbqfARDo=; b=AVq11LyLM099Jjajrzqm2QzTRoDaX+h0rcYUlabFQbZJsaSzsXGUX8/apzJwVwYcoU LkIwn+w3AWVFas0m2kfdbZ9xkPuqOJXjWsk+fYPeUMc4R4Vt0B0RI/g6ohNFg+cJk9qV iQgvYVKBoodt4gpoOu/JloR5kDTCNAkQb4jxHzvPym1ZQ8jzVmwvH8HF+noCX5gZYx41 r2Sh2sEheFXpjTl/xk2oA3qNxfKqXdd5Dvwq9DJ1AmHLNEJJwyjq82mFRhTT91owhNRA 16o16mIcuQLnyfjDJJgBFL+F57JvaQ4NRFErZLVbkryRUt6DGCI1s3b6N3kYq724ssaJ QK/Q== X-Gm-Message-State: AOUpUlE9VO98BU5hd3t08LkCWL3GKMPOF3VJUvABrFn5OCn35z7B7F4r RzOOdLwsVLvFcPQWBfV7dHovj9Vvi6s= X-Received: by 2002:a62:245d:: with SMTP id r90-v6mr326047pfj.108.1532055507649; Thu, 19 Jul 2018 19:58:27 -0700 (PDT) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id l71-v6sm524968pgd.70.2018.07.19.19.58.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Jul 2018 19:58:27 -0700 (PDT) From: AKASHI Takahiro To: trini@konsulko.com Date: Fri, 20 Jul 2018 11:57:19 +0900 Message-Id: <20180720025723.6736-14-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180720025723.6736-1-takahiro.akashi@linaro.org> References: <20180720025723.6736-1-takahiro.akashi@linaro.org> Cc: u-boot@lists.denx.de, xypron.glpk@gmx.de, agraf@suse.de Subject: [U-Boot] [PATCH 13/17] fs: fat: support mkdir X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" In this patch, mkdir support is added to FAT file system. A newly created directory contains only "." and ".." entries. Signed-off-by: AKASHI Takahiro --- fs/fat/fat_write.c | 138 +++++++++++++++++++++++++++++++++++++++++++++ fs/fs.c | 3 +- include/fat.h | 1 + 3 files changed, 141 insertions(+), 1 deletion(-) diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c index cc45a33876..781883c9f4 100644 --- a/fs/fat/fat_write.c +++ b/fs/fat/fat_write.c @@ -1185,3 +1185,141 @@ int file_fat_write(const char *filename, void *buffer, loff_t offset, { return file_fat_write_at(filename, offset, buffer, maxsize, actwrite); } + +int fat_mkdir(const char *new_dirname) +{ + dir_entry *retdent; + fsdata datablock = { .fatbuf = NULL, }; + fsdata *mydata = &datablock; + fat_itr *itr = NULL; + char *dirname_copy, *parent, *dirname; + char l_dirname[VFAT_MAXLEN_BYTES]; + int ret = -1; + loff_t actwrite; + unsigned int bytesperclust; + dir_entry *dotdent = NULL; + + dirname_copy = strdup(new_dirname); + if (!dirname_copy) + goto exit; + + split_filename(dirname_copy, &parent, &dirname); + if (!strlen(dirname)) { + ret = -EINVAL; + goto exit; + } + + if (normalize_longname(l_dirname, dirname)) { + printf("FAT: illegal filename (%s)\n", dirname); + ret = -EINVAL; + goto exit; + } + + itr = malloc_cache_aligned(sizeof(fat_itr)); + if (!itr) { + ret = -ENOMEM; + goto exit; + } + + ret = fat_itr_root(itr, &datablock); + if (ret) + goto exit; + + total_sector = datablock.bs_total_sect; + if (total_sector == 0) + total_sector = (int)cur_part_info.size; /* cast of lbaint_t */ + + ret = fat_itr_resolve(itr, parent, TYPE_DIR); + if (ret) { + printf("%s: doesn't exist (%d)\n", parent, ret); + goto exit; + } + + retdent = find_directory_entry(itr, l_dirname); + + if (retdent) { + printf("%s: already exists\n", l_dirname); + ret = -EEXIST; + goto exit; + } else { + if (itr->is_root) { + /* root dir cannot have "." or ".." */ + if (!strcmp(l_dirname, ".") || + !strcmp(l_dirname, "..")) { + ret = -EINVAL; + goto exit; + } + } + + if (!itr->dent) { + printf("Error: allocating new dir entry\n"); + ret = -EIO; + goto exit; + } + + memset(itr->dent, 0, sizeof(*itr->dent)); + + /* Set short name to set alias checksum field in dir_slot */ + set_name(itr->dent, dirname); + fill_dir_slot(itr, dirname); + + /* Set attribute as archieve for regular file */ + fill_dentry(itr->fsdata, itr->dent, dirname, 0, 0, + ATTR_DIR | ATTR_ARCH); + + retdent = itr->dent; + } + + /* Default entries */ + bytesperclust = mydata->clust_size * mydata->sect_size; + dotdent = malloc_cache_aligned(bytesperclust); + if (!dotdent) { + ret = -ENOMEM; + goto exit; + } + memset(dotdent, 0, bytesperclust); + + memcpy(dotdent[0].name, ". ", 8); + memcpy(dotdent[0].ext, " ", 3); + dotdent[0].attr = ATTR_DIR | ATTR_ARCH; + + memcpy(dotdent[1].name, ".. ", 8); + memcpy(dotdent[1].ext, " ", 3); + dotdent[1].attr = ATTR_DIR | ATTR_ARCH; + set_start_cluster(mydata, &dotdent[1], itr->start_clust); + + ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent, bytesperclust, + &actwrite); + if (ret < 0) { + printf("Error: writing contents\n"); + goto exit; + } + /* Write twice for "." */ + set_start_cluster(mydata, &dotdent[0], START(retdent)); + ret = set_contents(mydata, retdent, 0, (__u8 *)dotdent, bytesperclust, + &actwrite); + if (ret < 0) { + printf("Error: writing contents\n"); + goto exit; + } + + /* Flush fat buffer */ + ret = flush_dirty_fat_buffer(mydata); + if (ret) { + printf("Error: flush fat buffer\n"); + goto exit; + } + + /* Write directory table to device */ + ret = set_cluster(mydata, itr->clust, itr->block, + mydata->clust_size * mydata->sect_size); + if (ret) + printf("Error: writing directory entry\n"); + +exit: + free(dirname_copy); + free(mydata->fatbuf); + free(itr); + free(dotdent); + return ret; +} diff --git a/fs/fs.c b/fs/fs.c index 3cb6b21fe9..a92e060296 100644 --- a/fs/fs.c +++ b/fs/fs.c @@ -164,14 +164,15 @@ static struct fstype_info fstypes[] = { .read = fat_read_file, #ifdef CONFIG_FAT_WRITE .write = file_fat_write, + .mkdir = fat_mkdir, #else .write = fs_write_unsupported, + .mkdir = fs_mkdir_unsupported, #endif .uuid = fs_uuid_unsupported, .opendir = fat_opendir, .readdir = fat_readdir, .closedir = fat_closedir, - .mkdir = fs_mkdir_unsupported, }, #endif #ifdef CONFIG_FS_EXT4 diff --git a/include/fat.h b/include/fat.h index 295da0f243..039b6e03de 100644 --- a/include/fat.h +++ b/include/fat.h @@ -237,5 +237,6 @@ int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len, int fat_opendir(const char *filename, struct fs_dir_stream **dirsp); int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp); void fat_closedir(struct fs_dir_stream *dirs); +int fat_mkdir(const char *dirname); void fat_close(void); #endif /* _FAT_H_ */