1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2017-2018 Western Digital Corporation or its affiliates.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
* Author: Jeff Lien <jeff.lien@wdc.com>,
*/
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "nvme.h"
#include "libnvme.h"
#include "nvme-print.h"
#include "wdc-utils.h"
int wdc_UtilsSnprintf(char *buffer, unsigned int sizeOfBuffer, const char *format, ...)
{
int res = 0;
va_list vArgs;
va_start(vArgs, format);
res = vsnprintf(buffer, sizeOfBuffer, format, vArgs);
va_end(vArgs);
return res;
}
void wdc_UtilsDeleteCharFromString(char *buffer, int buffSize, char charToRemove)
{
int i = 0;
int count = 0;
if (!buffer || !buffSize)
return;
/*
* Traverse the given string. If current character is not charToRemove,
* then place it at index count++
*/
for (i = 0; ((i < buffSize) && (buffer[i] != '\0')); i++) {
if (buffer[i] != charToRemove)
buffer[count++] = buffer[i];
}
buffer[count] = '\0';
}
int wdc_UtilsGetTime(PUtilsTimeInfo timeInfo)
{
time_t currTime;
struct tm currTimeInfo;
if (!timeInfo)
return WDC_STATUS_INVALID_PARAMETER;
tzset();
time(&currTime);
localtime_r(&currTime, &currTimeInfo);
timeInfo->year = currTimeInfo.tm_year + 1900;
timeInfo->month = currTimeInfo.tm_mon + 1;
timeInfo->dayOfWeek = currTimeInfo.tm_wday;
timeInfo->dayOfMonth = currTimeInfo.tm_mday;
timeInfo->hour = currTimeInfo.tm_hour;
timeInfo->minute = currTimeInfo.tm_min;
timeInfo->second = currTimeInfo.tm_sec;
timeInfo->msecs = 0;
timeInfo->isDST = currTimeInfo.tm_isdst;
#if defined(__GLIBC__) && !defined(__UCLIBC__) && !defined(__MUSL__)
timeInfo->zone = -currTimeInfo.tm_gmtoff / 60;
#else
timeInfo->zone = -1 * (timezone / SECONDS_IN_MIN);
#endif
return WDC_STATUS_SUCCESS;
}
int wdc_UtilsCreateDir(char *path)
{
int retStatus;
int status = WDC_STATUS_SUCCESS;
if (!path)
return WDC_STATUS_INVALID_PARAMETER;
retStatus = mkdir(path, 0x999);
if (retStatus < 0) {
if (errno == EEXIST)
status = WDC_STATUS_DIR_ALREADY_EXISTS;
else if (errno == ENOENT)
status = WDC_STATUS_PATH_NOT_FOUND;
else
status = WDC_STATUS_CREATE_DIRECTORY_FAILED;
}
return status;
}
int wdc_WriteToFile(char *fileName, char *buffer, unsigned int bufferLen)
{
int status = WDC_STATUS_SUCCESS;
FILE *file;
size_t bytesWritten = 0;
file = fopen(fileName, "ab+");
if (!file) {
status = WDC_STATUS_UNABLE_TO_OPEN_FILE;
goto end;
}
bytesWritten = fwrite(buffer, 1, bufferLen, file);
if (bytesWritten != bufferLen)
status = WDC_STATUS_UNABLE_TO_WRITE_ALL_DATA;
end:
if (file)
fclose(file);
return status;
}
/**
* Compares the strings ignoring their cases.
*
* @param pcSrc Points to a null terminated string for comparing.
* @param pcDst Points to a null terminated string for comparing.
*
* @returns zero if the string matches or
* 1 if the pcSrc string is lexically higher than pcDst or
* -1 if the pcSrc string is lexically lower than pcDst.
*/
int wdc_UtilsStrCompare(char *pcSrc, char *pcDst)
{
while ((toupper(*pcSrc) == toupper(*pcDst)) && (*pcSrc != '\0')) {
pcSrc++;
pcDst++;
}
return *pcSrc - *pcDst;
}
void wdc_StrFormat(char *formatter, size_t fmt_sz, char *tofmt, size_t tofmtsz)
{
fmt_sz = snprintf(formatter, fmt_sz, "%-*.*s", (int)tofmtsz, (int)tofmtsz, tofmt);
/* trim() the obnoxious trailing white lines */
while (fmt_sz) {
if (formatter[fmt_sz - 1] != ' ' && formatter[fmt_sz - 1] != '\0') {
formatter[fmt_sz] = '\0';
break;
}
fmt_sz--;
}
}
bool wdc_CheckUuidListSupport(struct nvme_dev *dev, struct nvme_id_uuid_list *uuid_list)
{
int err;
struct nvme_id_ctrl ctrl;
memset(&ctrl, 0, sizeof(struct nvme_id_ctrl));
err = nvme_identify_ctrl(dev_fd(dev), &ctrl);
if (err) {
fprintf(stderr, "ERROR: WDC: nvme_identify_ctrl() failed 0x%x\n", err);
return false;
}
if ((ctrl.ctratt & NVME_CTRL_CTRATT_UUID_LIST) == NVME_CTRL_CTRATT_UUID_LIST) {
err = nvme_identify_uuid(dev_fd(dev), uuid_list);
if (!err)
return true;
else if (err > 0)
nvme_show_status(err);
else
nvme_show_error("identify UUID list: %s", nvme_strerror(errno));
}
return false;
}
bool wdc_UuidEqual(struct nvme_id_uuid_list_entry *entry1, struct nvme_id_uuid_list_entry *entry2)
{
int i;
for (i = 0; i < 16; i++) {
if (entry1->uuid[i] != entry2->uuid[i])
break;
}
if (i == 16)
return true;
else
return false;
}
|