[PATCH 1/2](fix) Moved mkdir functions, added MAX_LENGTH & MAX_PATH_LENGTH

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



mkdir() function are now called after successfull function
creation.
Added inline to gadget_write_string().
256 hard coded value has been replaced by two defined
constants MAX_LENGTH and MAX_PATH_LENGTH

Change-Id: Ifcfaf9cf95da6558c176ce3367cd553cea54e871
Signed-off-by: Stanislaw Wadas <s.wadas@xxxxxxxxxxx>
---
 include/gadget/gadget.h |   27 ++++++++-------
 src/gadget.c            |   84 +++++++++++++++++++++++------------------------
 2 files changed, 57 insertions(+), 54 deletions(-)

diff --git a/include/gadget/gadget.h b/include/gadget/gadget.h
index 6a32c39..80e9dd6 100644
--- a/include/gadget/gadget.h
+++ b/include/gadget/gadget.h
@@ -33,13 +33,16 @@
 #define DEFAULT_UDC		NULL
 #define LANG_US_ENG		0x0409
 
+#define MAX_LENGTH 256
+#define MAX_PATH_LENGTH 256
+
 /**
  * @struct state
  * @brief State of the gadget devices in the system
  */
 struct state
 {
-	char path[256];
+	char path[MAX_PATH_LENGTH];
 
 	TAILQ_HEAD(ghead, gadget) gadgets;
 };
@@ -51,8 +54,8 @@ struct state
 struct gadget
 {
 	char name[40];
-	char path[256];
-	char udc[256];
+	char path[MAX_PATH_LENGTH];
+	char udc[MAX_LENGTH];
 	int dclass;
 	int dsubclass;
 	int dproto;
@@ -61,9 +64,9 @@ struct gadget
 	int bcdusb;
 	int product;
 	int vendor;
-	char str_ser[256];
-	char str_mnf[256];
-	char str_prd[256];
+	char str_ser[MAX_LENGTH];
+	char str_mnf[MAX_LENGTH];
+	char str_prd[MAX_LENGTH];
 	TAILQ_ENTRY(gadget) gnode;
 	TAILQ_HEAD(chead, config) configs;
 	TAILQ_HEAD(fhead, function) functions;
@@ -81,10 +84,10 @@ struct config
 	struct gadget *parent;
 
 	char name[40];
-	char path[256];
+	char path[MAX_PATH_LENGTH];
 	int maxpower;
 	int bmattrs;
-	char str_cfg[256];
+	char str_cfg[MAX_LENGTH];
 };
 
 /**
@@ -136,7 +139,7 @@ struct serial_attrs {
 struct net_attrs {
 	struct ether_addr dev_addr;
 	struct ether_addr host_addr;
-	char ifname[256];
+	char ifname[MAX_LENGTH];
 	int qmult;
 };
 
@@ -145,7 +148,7 @@ struct net_attrs {
  * @brief Attributes for the phonet USB function
  */
 struct phonet_attrs {
-	char ifname[256];
+	char ifname[MAX_LENGTH];
 };
 
 /**
@@ -168,7 +171,7 @@ struct function
 	struct gadget *parent;
 
 	char name[40];
-	char path[256];
+	char path[MAX_PATH_LENGTH];
 
 	enum function_type type;
 	union attrs attr;
@@ -187,7 +190,7 @@ struct binding
 	struct function *target;
 
 	char name[40];
-	char path[256];
+	char path[MAX_PATH_LENGTH];
 };
 
 /* Library init and cleanup */
diff --git a/src/gadget.c b/src/gadget.c
index 83b8b62..faed675 100644
--- a/src/gadget.c
+++ b/src/gadget.c
@@ -82,7 +82,7 @@ static int file_select(const struct dirent *dent)
 
 static char *gadget_read_buf(char *path, char *name, char *file, char *buf)
 {
-	char p[256];
+	char p[MAX_LENGTH];
 	FILE *fp;
 	char *ret = NULL;
 
@@ -92,7 +92,7 @@ static char *gadget_read_buf(char *path, char *name, char *file, char *buf)
 	if (!fp)
 		goto out;
 
-	ret = fgets(buf, 256, fp);
+	ret = fgets(buf, MAX_LENGTH, fp);
 
 	fclose(fp);
 
@@ -102,7 +102,7 @@ out:
 
 static int gadget_read_int(char *path, char *name, char *file, int base)
 {
-	char buf[256];
+	char buf[MAX_LENGTH];
 
 	if (gadget_read_buf(path, name, file, buf))
 		return strtol(buf, NULL, base);
@@ -125,7 +125,7 @@ static void gadget_read_string(char *path, char *name, char *file, char *buf)
 
 static void gadget_write_buf(char *path, char *name, char *file, char *buf)
 {
-	char p[256];
+	char p[MAX_LENGTH];
 	FILE *fp;
 
 	sprintf(p, "%s/%s/%s", path, name, file);
@@ -143,7 +143,7 @@ static void gadget_write_buf(char *path, char *name, char *file, char *buf)
 
 static void gadget_write_int(char *path, char *name, char *file, int value, char *str)
 {
-	char buf[256];
+	char buf[MAX_LENGTH];
 
 	sprintf(buf, str, value);
 	gadget_write_buf(path, name, file, buf);
@@ -153,7 +153,7 @@ static void gadget_write_int(char *path, char *name, char *file, int value, char
 #define gadget_write_hex16(p, n, f, v)	gadget_write_int(p, n, f, v, "0x%04x\n")
 #define gadget_write_hex8(p, n, f, v)	gadget_write_int(p, n, f, v, "0x%02x\n")
 
-static void gadget_write_string(char *path, char *name, char *file, char *buf)
+static inline void gadget_write_string(char *path, char *name, char *file, char *buf)
 {
 	gadget_write_buf(path, name, file, buf);
 }
@@ -196,7 +196,7 @@ static int gadget_parse_functions(char *path, struct gadget *g)
 	struct function *f;
 	int i, n;
 	struct dirent **dent;
-	char fpath[256];
+	char fpath[MAX_PATH_LENGTH];
 
 	sprintf(fpath, "%s/%s/functions", path, g->name);
 
@@ -227,7 +227,7 @@ static void gadget_parse_config_bindings(struct config *c)
 {
 	int i, n;
 	struct dirent **dent;
-	char bpath[256];
+	char bpath[MAX_PATH_LENGTH];
 	struct gadget *g = c->parent;
 	struct binding *b;
 	struct function *f;
@@ -240,12 +240,12 @@ static void gadget_parse_config_bindings(struct config *c)
 	for (i=0; i < n; i++) {
 		TAILQ_FOREACH(f, &g->functions, fnode) {
 			int n;
-			char contents[256];
-			char cpath[256];
+			char contents[MAX_LENGTH];
+			char cpath[MAX_PATH_LENGTH];
 			char fname[40];
 
 			sprintf(cpath, "%s/%s", bpath, dent[i]->d_name);
-			n = readlink(cpath, contents, 256);
+			n = readlink(cpath, contents, MAX_PATH_LENGTH);
 			if (n<0)
 				ERRORNO("bytes %d contents %s\n", n, contents);
 			strcpy(fname, f->name);
@@ -268,7 +268,7 @@ static int gadget_parse_configs(char *path, struct gadget *g)
 	struct config *c;
 	int i, n;
 	struct dirent **dent;
-	char cpath[256];
+	char cpath[MAX_PATH_LENGTH];
 
 	sprintf(cpath, "%s/%s/configs", path, g->name);
 
@@ -354,7 +354,7 @@ struct state *gadget_init(char *configfs_path)
 {
 	int ret;
 	struct stat sts;
-	char path[256];
+	char path[MAX_PATH_LENGTH];
 	struct state *s = NULL;
 
 	strcpy(path, configfs_path);
@@ -468,7 +468,7 @@ struct binding *gadget_get_link_binding(struct config *c, struct function *f)
 struct gadget *gadget_create_gadget(struct state *s, char *name,
 				    int vendor, int product)
 {
-	char gpath[256];
+	char gpath[MAX_PATH_LENGTH];
 	struct gadget *g, *cur;
 	int ret;
 
@@ -483,12 +483,6 @@ struct gadget *gadget_create_gadget(struct state *s, char *name,
 
 	sprintf(gpath, "%s/%s", s->path, name);
 
-	ret = mkdir(gpath, S_IRWXU|S_IRWXG|S_IRWXO);
-	if (ret < 0) {
-		ERRORNO("%s\n", gpath);
-		return NULL;
-	}
-
 	g = malloc(sizeof(struct gadget));
 	if (!g) {
 		ERRORNO("allocating gadget\n");
@@ -506,6 +500,12 @@ struct gadget *gadget_create_gadget(struct state *s, char *name,
 	gadget_write_hex16(s->path, name, "idVendor", vendor);
 	gadget_write_hex16(s->path, name, "idProduct", product);
 
+	ret = mkdir(gpath, S_IRWXU|S_IRWXG|S_IRWXO);
+	if (ret < 0) {
+		ERRORNO("%s\n", gpath);
+		return NULL;
+	}
+
 	/* Insert in string order */
 	if (TAILQ_EMPTY(&s->gadgets) ||
 	    (strcmp(name, TAILQ_FIRST(&s->gadgets)->name) < 0))
@@ -560,7 +560,7 @@ void gadget_set_gadget_device_bcd_usb(struct gadget *g, int bcdusb)
 
 void gadget_set_gadget_serial_number(struct gadget *g, int lang, char *serno)
 {
-	char path[256];
+	char path[MAX_PATH_LENGTH];
 
 	sprintf(path, "%s/%s/%s/0x%x", g->path, g->name, "strings", lang);
 
@@ -573,7 +573,7 @@ void gadget_set_gadget_serial_number(struct gadget *g, int lang, char *serno)
 
 void gadget_set_gadget_manufacturer(struct gadget *g, int lang, char *mnf)
 {
-	char path[256];
+	char path[MAX_PATH_LENGTH];
 
 	sprintf(path, "%s/%s/%s/0x%x", g->path, g->name, "strings", lang);
 
@@ -586,7 +586,7 @@ void gadget_set_gadget_manufacturer(struct gadget *g, int lang, char *mnf)
 
 void gadget_set_gadget_product(struct gadget *g, int lang, char *prd)
 {
-	char path[256];
+	char path[MAX_PATH_LENGTH];
 
 	sprintf(path, "%s/%s/%s/0x%x", g->path, g->name, "strings", lang);
 
@@ -599,8 +599,8 @@ void gadget_set_gadget_product(struct gadget *g, int lang, char *prd)
 
 struct function *gadget_create_function(struct gadget *g, enum function_type type, char *instance)
 {
-	char fpath[256];
-	char name[256];
+	char fpath[MAX_PATH_LENGTH];
+	char name[MAX_LENGTH];
 	struct function *f, *cur;
 	int ret;
 
@@ -619,12 +619,6 @@ struct function *gadget_create_function(struct gadget *g, enum function_type typ
 
 	sprintf(fpath, "%s/%s/functions/%s", g->path, g->name, name);
 
-	ret = mkdir(fpath, S_IRWXU|S_IRWXG|S_IRWXO);
-	if (ret < 0) {
-		ERRORNO("%s\n", fpath);
-		return NULL;
-	}
-
 	f = malloc(sizeof(struct function));
 	if (!f) {
 		ERRORNO("allocating function\n");
@@ -637,6 +631,12 @@ struct function *gadget_create_function(struct gadget *g, enum function_type typ
 
 	gadget_parse_function_attrs(f);
 
+	ret = mkdir(fpath, S_IRWXU|S_IRWXG|S_IRWXO);
+	if (ret < 0) {
+		ERRORNO("%s\n", fpath);
+		return NULL;
+	}
+
 	/* Insert in string order */
 	if (TAILQ_EMPTY(&g->functions) ||
 	    (strcmp(name, TAILQ_FIRST(&g->functions)->name) < 0))
@@ -655,7 +655,7 @@ struct function *gadget_create_function(struct gadget *g, enum function_type typ
 
 struct config *gadget_create_config(struct gadget *g, char *name)
 {
-	char cpath[256];
+	char cpath[MAX_PATH_LENGTH];
 	struct config *c, *cur;
 	int ret;
 
@@ -673,12 +673,6 @@ struct config *gadget_create_config(struct gadget *g, char *name)
 
 	sprintf(cpath, "%s/%s/configs/%s", g->path, g->name, name);
 
-	ret = mkdir(cpath, S_IRWXU|S_IRWXG|S_IRWXO);
-	if (ret < 0) {
-		ERRORNO("%s\n", cpath);
-		return NULL;
-	}
-
 	c = malloc(sizeof(struct config));
 	if (!c) {
 		ERRORNO("allocating configuration\n");
@@ -689,6 +683,12 @@ struct config *gadget_create_config(struct gadget *g, char *name)
 	strcpy(c->name, name);
 	sprintf(c->path, "%s/%s/%s/%s", g->path, g->name, "configs", name);
 
+	ret = mkdir(cpath, S_IRWXU|S_IRWXG|S_IRWXO);
+	if (ret < 0) {
+		ERRORNO("%s\n", cpath);
+		return NULL;
+	}
+
 	/* Insert in string order */
 	if (TAILQ_EMPTY(&g->configs) ||
 	    (strcmp(name, TAILQ_FIRST(&g->configs)->name) < 0))
@@ -719,7 +719,7 @@ void gadget_set_config_bm_attrs(struct config *c, int bmattrs)
 
 void gadget_set_config_string(struct config *c, int lang, char *str)
 {
-	char path[256];
+	char path[MAX_PATH_LENGTH];
 
 	sprintf(path, "%s/%s/0x%x", c->path, "strings", lang);
 
@@ -732,8 +732,8 @@ void gadget_set_config_string(struct config *c, int lang, char *str)
 
 int gadget_add_config_function(struct config *c, char *name, struct function *f)
 {
-	char bpath[256];
-	char fpath[256];
+	char bpath[MAX_PATH_LENGTH];
+	char fpath[MAX_PATH_LENGTH];
 	struct binding *b;
 	struct binding *cur;
 	int ret = -1;
@@ -795,7 +795,7 @@ int gadget_get_udcs(struct dirent ***udc_list)
 
 void gadget_enable_gadget(struct gadget *g, char *udc)
 {
-	char gudc[256];
+	char gudc[MAX_LENGTH];
 	struct dirent **udc_list;
 	int n;
 
-- 
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux