Browse code

onas: improving error and edge-case handling and honing recursive walks

Mickey Sola authored on 2015/09/29 23:51:35
Showing 2 changed files
... ...
@@ -189,7 +189,9 @@ static int onas_ddd_watch_hierarchy(const char* pathname, size_t len, int fd, ui
189 189
 		else
190 190
 			snprintf(child_path, size, "%s/%s", hnode->pathname, curr->dirname);
191 191
 
192
-		if(onas_ddd_watch_hierarchy(child_path, strlen(child_path), fd, mask, type)) return CL_EARG;
192
+		if(onas_ddd_watch_hierarchy(child_path, strlen(child_path), fd, mask, type)) {
193
+			return CL_EARG;
194
+		}
193 195
 		free(child_path);
194 196
 	}
195 197
 
... ...
@@ -249,7 +249,7 @@ int onas_ht_get(struct onas_ht *ht, const char *key, size_t klen, struct onas_el
249 249
 
250 250
 	struct onas_element *curr = bckt->head;
251 251
 
252
-	while (curr && strncmp(curr->key, key, klen)) {
252
+	while (curr && strcmp(curr->key, key)) {
253 253
 		curr = curr->next;
254 254
 	}
255 255
 	
... ...
@@ -481,12 +481,23 @@ inline static char *onas_get_parent(const char *pathname, size_t len) {
481 481
 	if (!pathname || len <= 1) return NULL;
482 482
 
483 483
 	int idx = len - 2;
484
+	char *ret = NULL;
484 485
 
485 486
 	while(idx >= 0 && pathname[idx] != '/') {
486 487
 		idx--;
487 488
 	}
488 489
 
489
-	return strndup(pathname, idx);
490
+	if (idx == 0) {
491
+		idx++;
492
+	}
493
+
494
+	ret = strndup(pathname, idx);
495
+	if (!ret) {
496
+		errno = ENOMEM;
497
+		return NULL;
498
+	}
499
+
500
+	return ret;
490 501
 }
491 502
 
492 503
 /* Gets the index at which the name of directory begins from the full pathname. */
... ...
@@ -507,18 +518,23 @@ inline static int onas_get_dirname_idx(const char *pathname, size_t len) {
507 507
 
508 508
 /* Emancipates the specified child from the specified parent. */
509 509
 int onas_ht_rm_child(struct onas_ht *ht, const char *prntpath, size_t prntlen, const char *childpath, size_t childlen) {
510
+
510 511
 	if (!ht || !prntpath || prntlen <= 0 || !childpath || childlen <= 1) return CL_ENULLARG;
511 512
 
512 513
 	struct onas_element *elem = NULL;
513 514
 	struct onas_hnode *hnode = NULL;
514 515
 	int idx = onas_get_dirname_idx(childpath, childlen);
516
+	int ret = 0;
515 517
 
516 518
 	if(idx <= 0) return CL_SUCCESS;
517 519
 
518 520
 	if(onas_ht_get(ht, prntpath, prntlen, &elem) != CL_SUCCESS) return CL_EARG;
521
+
519 522
 	hnode = elem->data;
520 523
 
521
-	return onas_rm_listnode(hnode->childhead, &(childpath[idx]));
524
+	if (ret = onas_rm_listnode(hnode->childhead, &(childpath[idx]))) return CL_EARG;
525
+
526
+	return CL_SUCCESS;
522 527
 }
523 528
 
524 529
 /* The specified parent adds the specified child to its list. */
... ...
@@ -545,7 +561,7 @@ int onas_ht_add_hierarchy(struct onas_ht *ht, const char *pathname) {
545 545
 	if (!ht || !pathname) return CL_ENULLARG;
546 546
 
547 547
 	FTS *ftsp = NULL;
548
-	int ftspopts = FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOCHDIR | FTS_NOSTAT | FTS_XDEV;
548
+	int ftspopts = FTS_PHYSICAL | FTS_XDEV;
549 549
 	FTSENT *curr = NULL;
550 550
 	FTSENT *childlist = NULL;
551 551
 
... ...
@@ -585,9 +601,12 @@ int onas_ht_add_hierarchy(struct onas_ht *ht, const char *pathname) {
585 585
 
586 586
 		if(childlist = fts_children(ftsp, 0)) {
587 587
 			do {
588
-				if (childlist->fts_info == FTS_D)
588
+				if (childlist->fts_info & FTS_D &&
589
+				    !(childlist->fts_info & FTS_DNR) &&
590
+				    !(childlist->fts_info & FTS_SL)) {
589 591
 					if(CL_EMEM == onas_add_hashnode_child(hnode, childlist->fts_name))
590 592
 						return CL_EMEM;
593
+				}
591 594
 
592 595
 			} while (childlist = childlist->fts_link);
593 596
 		}
... ...
@@ -619,8 +638,10 @@ int onas_ht_rm_hierarchy(struct onas_ht *ht, const char* pathname, size_t len, i
619 619
 
620 620
 	if(level == 0) {
621 621
 		if(!(prntname = onas_get_parent(pathname, len))) return CL_EARG;
622
+
622 623
 		prntlen = strlen(prntname);
623
-		onas_ht_rm_child(ht, prntname, prntlen, pathname, len);
624
+		if(onas_ht_rm_child(ht, prntname, prntlen, pathname, len)) return CL_EARG;
625
+
624 626
 		free(prntname);
625 627
 	}
626 628