Fix string parsing

Change-Id: I0edd8f1e247ae802cdccf871a40b50d8a8714433
Signed-off-by: Jacob Malevich <jam@daterainc.com>
This commit is contained in:
Jacob Malevich 2014-11-24 15:53:35 -08:00
parent 7a80b660c2
commit 0098187201
4 changed files with 42 additions and 47 deletions

View File

@ -503,7 +503,7 @@ int dev_open(const char *dev, bool wipe_bcache)
struct cache_sb sb;
blkid_probe pr;
int fd;
char err[256];
char err[MAX_PATH];
if ((fd = open(dev, O_RDWR|O_EXCL)) == -1) {
sprintf(err, "Can't open dev %s: %s\n", dev, strerror(errno));
@ -910,7 +910,7 @@ struct cache_sb *query_dev(char *dev, bool force_csum,
return sb;
}
static void dev_name(char *ugly_path) {
static void dev_name(const char *ugly_path) {
char buf[32];
int i, end = strlen(ugly_path);
@ -932,16 +932,13 @@ static void list_cacheset_devs(char *cset_dir, char *cset_name, bool parse_dev_n
DIR *cachedir;
struct stat cache_stat;
char intbuf[4];
char entry[256];
char entry[MAX_PATH];
strcpy(entry, cset_dir);
strcat(entry, "/");
strcat(entry, cset_name);
strcat(entry, "/cache0");
snprintf(entry, MAX_PATH, "%s/%s/cache0", cset_dir, cset_name);
snprintf(intbuf, 4, "%d", i);
while(true) {
char buf[256];
char buf[MAX_PATH];
int len;
if((cachedir = opendir(entry)) == NULL)
@ -976,15 +973,14 @@ void find_matching_uuid(char *stats_dir, char *subdir, const char *stats_dev_uui
DIR *cachedir;
struct stat cache_stat;
char intbuf[4];
char entry[256];
char entry[MAX_PATH];
strcpy(entry, stats_dir);
strcat(entry, subdir);
snprintf(entry, MAX_PATH, "%s%s", stats_dir, subdir);
snprintf(intbuf, 4, "%d", i);
strcat(entry, intbuf);
while(true) {
char buf[256];
char buf[MAX_PATH];
int len;
if((cachedir = opendir(entry)) == NULL)
@ -1041,16 +1037,13 @@ int list_cachesets(char *cset_dir, bool list_devs)
while ((ent = readdir(dir)) != NULL) {
struct stat statbuf;
char entry[256];
char entry[MAX_PATH];
struct dirent *cache_ent;
if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
continue;
strcpy(entry, cset_dir);
strcat(entry, "/");
strcat(entry, ent->d_name);
snprintf(entry, MAX_PATH, "%s/%s", cset_dir, ent->d_name);
if(stat(entry, &statbuf) == -1) {
fprintf(stderr, "Failed to stat %s\n", entry);
return 1;
@ -1070,9 +1063,10 @@ int list_cachesets(char *cset_dir, bool list_devs)
return 0;
}
static int get_bcache_fd()
static int get_bcache_fd(void)
{
int bcachefd = open("/dev/bcache", O_RDWR);
if (bcachefd < 0) {
perror("Can't open bcache device\n");
exit(EXIT_FAILURE);
@ -1091,6 +1085,8 @@ int register_bcache(char *const *devs)
fprintf(stderr, "ioctl register error: %s\n", strerror(ret));
exit(EXIT_FAILURE);
}
close(bcachefd);
return 0;
}
@ -1105,6 +1101,8 @@ int unregister_bcache(char *const *devs)
fprintf(stderr, "ioctl unregister error: %s\n", strerror(ret));
exit(EXIT_FAILURE);
}
close(bcachefd);
return 0;
}
@ -1186,18 +1184,16 @@ void sb_state(struct cache_sb *sb, char *dev)
void read_stat_dir(DIR *dir, char *stats_dir, char *stat_name, bool print_val)
{
struct stat statbuf;
char entry[150];
char entry[MAX_PATH];
strcpy(entry, stats_dir);
strcat(entry, "/");
strcat(entry, stat_name);
snprintf(entry, MAX_PATH, "%s/%s", stats_dir, stat_name);
if(stat(entry, &statbuf) == -1) {
fprintf(stderr, "Failed to stat %s\n", entry);
return;
}
if (S_ISREG(statbuf.st_mode)) {
char buf[100];
char buf[MAX_PATH];
FILE *fp = NULL;
fp = fopen(entry, "r");
@ -1207,7 +1203,7 @@ void read_stat_dir(DIR *dir, char *stats_dir, char *stat_name, bool print_val)
return;
}
while(fgets(buf, 100, fp));
while(fgets(buf, MAX_PATH, fp));
if(print_val)
printf("%s\n", buf);

View File

@ -21,6 +21,7 @@ typedef __s32 s32;
typedef __s64 s64;
#define SB_START (SB_SECTOR * 512)
#define MAX_PATH 256
#define max(x, y) ({ \

View File

@ -69,12 +69,7 @@ Create a cache
Create a backing device
.TP
.BR \-b\ --bucket-size=
Specifies the bucket size. Allocation is done in terms of buckets, and cache
hits are counted per bucket; thus a smaller bucket size will give better cache
utilization, but poorer write performance. The bucket size is intended to be
equal to the size of your SSD's erase blocks, which seems to be 128k-512k for
most SSDs. Must be a power of two; accepts human readable units. Defaults to
128k.
Specifies the bucket size.
.TP
.BR \-l\ --label=
label

View File

@ -114,11 +114,16 @@ static int set_cache(NihOption *option, const char *arg)
bdev = 0;
cache_devices[nr_cache_devices] = (char *)malloc(sizeof(char *) *
strlen(arg) + 1);
strcpy(cache_devices[nr_cache_devices], arg);
cache_devices[nr_cache_devices] = strdup(arg);
if(!tier)
tier_mapping[nr_cache_devices] = 0;
else
tier_mapping[nr_cache_devices] = atoi(tier);
else {
int ntier = atoi(tier);
if(tier == 0 || tier == 1)
tier_mapping[nr_cache_devices] = ntier;
else
printf("Invalid tier\n");
}
devs++;
nr_cache_devices++;
@ -131,12 +136,12 @@ static int set_bdev(NihOption *option, const char *arg)
if(label) {
backing_dev_labels[nr_backing_devices] =
(char *)malloc(sizeof(char *) * strlen(label) + 1);
strcpy(backing_dev_labels[nr_backing_devices], label);
backing_dev_labels[nr_backing_devices] = strdup(label);
}
backing_devices[nr_backing_devices] = (char *)malloc(sizeof(char *) *
strlen(arg) + 1);
strcpy(backing_devices[nr_backing_devices], arg);
backing_devices[nr_backing_devices] = strdup(arg);
nr_backing_devices++;
devs++;
@ -239,7 +244,7 @@ static NihOption options[] = {
/* commands */
int make_bcache (NihCommand *command, char *const *args)
int make_bcache(NihCommand *command, char *const *args)
{
int cache_dev_fd[devs];
@ -342,7 +347,7 @@ int make_bcache (NihCommand *command, char *const *args)
return 0;
}
int probe_bcache (NihCommand *command, char *const *args)
int probe_bcache(NihCommand *command, char *const *args)
{
int i;
@ -353,26 +358,26 @@ int probe_bcache (NihCommand *command, char *const *args)
return 0;
}
int bcache_register (NihCommand *command, char *const *args)
int bcache_register(NihCommand *command, char *const *args)
{
int ret = register_bcache(args);
return ret;
}
int bcache_unregister (NihCommand *command, char *const *args)
int bcache_unregister(NihCommand *command, char *const *args)
{
int ret = unregister_bcache(args);
return ret;
}
int bcache_list_cachesets (NihCommand *command, char *const *args)
int bcache_list_cachesets(NihCommand *command, char *const *args)
{
return list_cachesets(cset_dir, list_devs);
}
int bcache_query_devs (NihCommand *command, char *const *args)
int bcache_query_devs(NihCommand *command, char *const *args)
{
int i;
@ -384,7 +389,7 @@ int bcache_query_devs (NihCommand *command, char *const *args)
}
}
int bcache_status (NihCommand *command, char *const *args)
int bcache_status(NihCommand *command, char *const *args)
{
int i;
struct cache_sb *sb_tier0 = NULL, *sb_tier1 = NULL;
@ -434,17 +439,15 @@ static void stats_subdir(char* stats_dir)
strcat(stats_dir, tmp);
}
int bcache_stats (NihCommand *command, char *const *args)
int bcache_stats(NihCommand *command, char *const *args)
{
int i;
char stats_dir[200];
char stats_dir[MAX_PATH];
DIR *dir = NULL;
struct dirent *ent = NULL;
if (stats_uuid) {
strcpy(stats_dir, cset_dir);
strcat(stats_dir, "/");
strcat(stats_dir, stats_uuid);
snprintf(stats_dir, MAX_PATH, "%s/%s", cset_dir, stats_uuid);
stats_subdir(stats_dir);
dir = opendir(stats_dir);
if (!dir) {