scsi: move spdk_strcpy_pad() into util/string.c

This will be useful outside of the SCSI code, so put it in the common
string utility file.

Also reorder the parameters so they match the order used in strncpy().

Change-Id: I9e25a59b64e4bedf04e5a96de463b1d8aa0ddac3
Signed-off-by: Daniel Verkamp <daniel.verkamp@intel.com>
This commit is contained in:
Daniel Verkamp 2016-08-19 10:01:13 -07:00
parent 759dbf46d8
commit 98c8867e5a
4 changed files with 37 additions and 23 deletions

View File

@ -42,6 +42,8 @@
extern "C" {
#endif
#include <stddef.h>
/**
* sprintf with automatic buffer allocation.
*
@ -78,6 +80,18 @@ char *spdk_strsepq(char **stringp, const char *delim);
*/
char *spdk_str_trim(char *s);
/**
* Copy a string into a fixed-size buffer, padding extra bytes with a specific character.
*
* \param dst Pointer to destination fixed-size buffer to fill.
* \param src Pointer to source null-terminated string to copy into dst.
* \param size Number of bytes to fill in dst.
* \param pad Character to pad extra space in dst beyond the size of src.
*
* If src is longer than size, only size bytes will be copied.
*/
void spdk_strcpy_pad(void *dst, const char *src, size_t size, int pad);
#ifdef __cplusplus
}
#endif

View File

@ -34,6 +34,7 @@
#include "scsi_internal.h"
#include "spdk/endian.h"
#include "spdk/string.h"
#define SPDK_WORK_BLOCK_SIZE (1ULL * 1024ULL * 1024ULL)
#define SPDK_WORK_ATS_BLOCK_SIZE (1ULL * 1024ULL * 1024ULL)
@ -44,20 +45,6 @@
#define DEFAULT_DISK_ROTATION_RATE 7200 /* 7200 rpm */
#define DEFAULT_DISK_FORM_FACTOR 0x02 /* 3.5 inch */
static void
spdk_strcpy_pad(uint8_t *dst, size_t size, const char *src, int pad)
{
size_t len;
len = strlen(src);
if (len < size) {
memcpy(dst, src, len);
memset(dst + len, pad, (size - len));
} else {
memcpy(dst, src, size);
}
}
static int
spdk_hex2bin(char ch)
{
@ -232,7 +219,7 @@ spdk_bdev_scsi_inquiry(struct spdk_bdev *bdev, struct spdk_scsi_task *task,
len = MAX_SERIAL_STRING;
}
spdk_strcpy_pad(vpage->params, len, bdev->name, ' ');
spdk_strcpy_pad(vpage->params, bdev->name, len, ' ');
/* PAGE LENGTH */
to_be16(vpage->alloc_len, len);
@ -286,10 +273,9 @@ spdk_bdev_scsi_inquiry(struct spdk_bdev *bdev, struct spdk_scsi_task *task,
desig->piv = 1;
desig->reserved1 = 0;
desig->len = 8 + 16 + MAX_SERIAL_STRING;
spdk_strcpy_pad(desig->desig, 8, DEFAULT_DISK_VENDOR, ' ');
spdk_strcpy_pad(&desig->desig[8], 16, bdev->product_name, ' ');
spdk_strcpy_pad(&desig->desig[24], MAX_SERIAL_STRING,
bdev->name, ' ');
spdk_strcpy_pad(desig->desig, DEFAULT_DISK_VENDOR, 8, ' ');
spdk_strcpy_pad(&desig->desig[8], bdev->product_name, 16, ' ');
spdk_strcpy_pad(&desig->desig[24], bdev->name, MAX_SERIAL_STRING, ' ');
len += sizeof(struct spdk_scsi_desig_desc) + 8 + 16 + MAX_SERIAL_STRING;
buf += sizeof(struct spdk_scsi_desig_desc) + desig->len;
@ -701,14 +687,13 @@ spdk_bdev_scsi_inquiry(struct spdk_bdev *bdev, struct spdk_scsi_task *task,
inqdata->flags3 = 0x2;
/* T10 VENDOR IDENTIFICATION */
spdk_strcpy_pad(inqdata->t10_vendor_id, 8, DEFAULT_DISK_VENDOR, ' ');
spdk_strcpy_pad(inqdata->t10_vendor_id, DEFAULT_DISK_VENDOR, 8, ' ');
/* PRODUCT IDENTIFICATION */
spdk_strcpy_pad(inqdata->product_id, 16,
bdev->product_name, ' ');
spdk_strcpy_pad(inqdata->product_id, bdev->product_name, 16, ' ');
/* PRODUCT REVISION LEVEL */
spdk_strcpy_pad(inqdata->product_rev, 4, DEFAULT_DISK_REVISION, ' ');
spdk_strcpy_pad(inqdata->product_rev, DEFAULT_DISK_REVISION, 4, ' ');
/* Vendor specific */
memset(inqdata->vendor, 0x20, 20);

View File

@ -200,3 +200,17 @@ spdk_str_trim(char *s)
return s;
}
void
spdk_strcpy_pad(void *dst, const char *src, size_t size, int pad)
{
size_t len;
len = strlen(src);
if (len < size) {
memcpy(dst, src, len);
memset((char *)dst + len, pad, size - len);
} else {
memcpy(dst, src, size);
}
}

View File

@ -35,6 +35,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk
SPDK_LIBS += $(SPDK_ROOT_DIR)/lib/log/libspdk_log.a \
$(SPDK_ROOT_DIR)/lib/util/libspdk_util.a \
$(SPDK_ROOT_DIR)/lib/cunit/libspdk_cunit.a
CFLAGS += -I$(SPDK_ROOT_DIR)/test