From patchwork Tue Nov 14 07:46:59 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: QI Fuli X-Patchwork-Id: 10057095 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id AAB19602A7 for ; Tue, 14 Nov 2017 07:47:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8984E28F97 for ; Tue, 14 Nov 2017 07:47:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7E71A28FD0; Tue, 14 Nov 2017 07:47:22 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 3025828F97 for ; Tue, 14 Nov 2017 07:47:22 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id F272C220757FF; Mon, 13 Nov 2017 23:43:14 -0800 (PST) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=202.219.69.171; helo=mgwkm04.jp.fujitsu.com; envelope-from=qi.fuli@jp.fujitsu.com; receiver=linux-nvdimm@lists.01.org Received: from mgwkm04.jp.fujitsu.com (mgwkm04.jp.fujitsu.com [202.219.69.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 335EA2035A7DB for ; Mon, 13 Nov 2017 23:43:12 -0800 (PST) Received: from kw-mxoi1.gw.nic.fujitsu.com (unknown [192.168.231.131]) by mgwkm04.jp.fujitsu.com with smtp id 63b5_55f7_896f884a_84c3_4470_85ca_b261afd01031; Tue, 14 Nov 2017 16:47:13 +0900 Received: from m3051.s.css.fujitsu.com (m3051.s.css.fujitsu.com [10.134.21.209]) by kw-mxoi1.gw.nic.fujitsu.com (Postfix) with ESMTP id 3B917AC0078 for ; Tue, 14 Nov 2017 16:47:13 +0900 (JST) Received: from qi-fedora.fujitsu.com (unknown [10.124.196.110]) by m3051.s.css.fujitsu.com (Postfix) with ESMTP id 1B66B1C4; Tue, 14 Nov 2017 16:47:13 +0900 (JST) From: QI Fuli To: linux-nvdimm@lists.01.org Subject: [RFC PATCH v2 2/7] ndctl: nvdimmd: add nvdimmd necessary util functions Date: Tue, 14 Nov 2017 16:46:59 +0900 Message-Id: <20171114074704.3446-3-qi.fuli@jp.fujitsu.com> X-Mailer: git-send-email 2.9.5 In-Reply-To: <20171114074704.3446-1-qi.fuli@jp.fujitsu.com> References: <20171114074704.3446-1-qi.fuli@jp.fujitsu.com> X-TM-AS-MML: disable X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Virus-Scanned: ClamAV using ClamSMTP This patch is used to provide helper functions needed for nvdimm daemon. These util functions can be used by other features as well in the future. Signed-off-by: QI Fuli --- nvdimmd/util.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ nvdimmd/util.h | 33 ++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 nvdimmd/util.c create mode 100644 nvdimmd/util.h diff --git a/nvdimmd/util.c b/nvdimmd/util.c new file mode 100644 index 0000000..ef6819e --- /dev/null +++ b/nvdimmd/util.c @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2017, FUJITSU LIMITED. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for + * more details. + */ + +#include +#include +#include +#include "util.h" + +void logrotate(char *file) +{ + time_t c_time; + char buf[4096], tstamp[32]; + + c_time = time(NULL); + strftime(tstamp, sizeof(tstamp), ".%Y%m%d%H%M%S", localtime(&c_time)); + strcpy(buf, file); + strcat(buf, tstamp); + rename(file, buf); +} + +long get_size(char *file) +{ + struct stat buf; + if (stat(file, &buf) == 0) + return buf.st_size; + return -1L; +} + +char *set_string(char **str, const char *value) +{ + *str = strdup(value); + return *str; +} + +char *trim_string(char *str) +{ + char *start; + char *end; + int len = strlen(str); + + if (len == 0) + return NULL; + + if (str[len-1] == '\n') { + len--; + str[len] = 0; + } + + start = str; + end = str + len - 1; + while(*start && isspace(*start)) + start++; + while(*end && isspace(*end)) + *end-- = 0; + strcpy(str, start); + return str; +} + +int split_string(char *str, const char *deli, char *outlist[]) +{ + int cnt = 0; + char *temp; + + temp = strtok(str, deli); + while (temp != NULL && cnt < NUM_MAX_DIMM) { + outlist[cnt++] = temp; + temp = strtok(NULL, deli); + } + return cnt; +} diff --git a/nvdimmd/util.h b/nvdimmd/util.h new file mode 100644 index 0000000..9648256 --- /dev/null +++ b/nvdimmd/util.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2017, FUJITSU LIMITED. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for + * more details. + */ + +#ifndef _UTIL_H_ +#define _UTIL_H_ + +#include +#include +#include +#include +#define NUM_MAX_DIMM 1024 + +void logrotate(char *file); + +long get_size(char *file); + +char *set_string(char **str, const char *value); + +char *trim_string(char *str); + +int split_string(char *str, const char *deli, char *outlist[]); + +#endif