Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,313 additions
and
806 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/*****************************************************************************\ | ||
* Copyright (c) 2014 Lawrence Livermore National Security, LLC. Produced at | ||
* the Lawrence Livermore National Laboratory (cf, AUTHORS, DISCLAIMER.LLNS). | ||
* LLNL-CODE-658032 All rights reserved. | ||
* | ||
* This file is part of the Flux resource manager framework. | ||
* For details, see https://github.com/flux-framework. | ||
* | ||
* 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. | ||
* | ||
* Flux 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 terms and conditions of 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., | ||
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | ||
* See also: http://www.gnu.org/licenses/ | ||
\*****************************************************************************/ | ||
|
||
#if HAVE_CONFIG_H | ||
#include "config.h" | ||
#endif | ||
#include <string.h> | ||
#include <assert.h> | ||
#include <errno.h> | ||
#include <jansson.h> | ||
|
||
#include "src/common/libutil/blobref.h" | ||
#include "src/common/libutil/oom.h" | ||
|
||
#include "jansson_dirent.h" | ||
|
||
json_t *j_dirent_create (const char *type, void *arg) | ||
{ | ||
json_t *dirent; | ||
bool valid_type = false; | ||
|
||
if (!(dirent = json_object ())) | ||
oom (); | ||
|
||
if (!strcmp (type, "FILEREF") || !strcmp (type, "DIRREF")) { | ||
char *ref = arg; | ||
json_t *o; | ||
|
||
if (!(o = json_string (ref))) | ||
oom (); | ||
if (json_object_set_new (dirent, type, o) < 0) | ||
oom (); | ||
|
||
valid_type = true; | ||
} else if (!strcmp (type, "FILEVAL") || !strcmp (type, "DIRVAL") | ||
|| !strcmp (type, "LINKVAL")) { | ||
json_t *val = arg; | ||
|
||
if (val) | ||
json_incref (val); | ||
else { | ||
if (!(val = json_object ())) | ||
oom (); | ||
} | ||
if (json_object_set_new (dirent, type, val) < 0) | ||
oom (); | ||
valid_type = true; | ||
} | ||
assert (valid_type == true); | ||
|
||
return dirent; | ||
} | ||
|
||
bool j_dirent_match (json_t *dirent1, json_t *dirent2) | ||
{ | ||
if (!dirent1 && !dirent2) | ||
return true; | ||
if ((dirent1 && !dirent2) || (!dirent1 && dirent2)) | ||
return false; | ||
if (json_equal (dirent1, dirent2)) | ||
return true; | ||
return false; | ||
} | ||
|
||
void j_dirent_append (json_t **array, const char *key, json_t *dirent) | ||
{ | ||
json_t *op; | ||
json_t *o; | ||
|
||
if (!(op = json_object ())) | ||
oom (); | ||
|
||
if (!(o = json_string (key))) | ||
oom (); | ||
if (json_object_set_new (op, "key", o) < 0) | ||
oom (); | ||
if (dirent) { | ||
if (json_object_set (op, "dirent", dirent) < 0) | ||
oom (); | ||
} | ||
else { | ||
json_t *null; | ||
if (!(null = json_null ())) | ||
oom (); | ||
if (json_object_set_new (op, "dirent", null) < 0) | ||
oom (); | ||
} | ||
if (!*array) { | ||
if (!(*array = json_array ())) | ||
oom (); | ||
} | ||
if (json_array_append (*array, op) < 0) | ||
oom (); | ||
} | ||
|
||
int j_dirent_validate (json_t *dirent) | ||
{ | ||
json_t *o; | ||
|
||
if (!dirent) | ||
goto error; | ||
if ((o = json_object_get (dirent, "DIRVAL"))) { | ||
const char *key; | ||
json_t *val; | ||
json_object_foreach (o, key, val) { | ||
if (j_dirent_validate (val) < 0) | ||
goto error; | ||
} | ||
} | ||
else if ((o = json_object_get (dirent, "FILEVAL"))) { | ||
/* Any json type is valid here */ | ||
} | ||
else if ((o = json_object_get (dirent, "LINKVAL"))) { | ||
if (json_typeof (o) != JSON_STRING) | ||
goto error; | ||
} | ||
else if ((o = json_object_get (dirent, "DIRREF")) | ||
|| (o = json_object_get (dirent, "FILEREF"))) { | ||
if (json_typeof (o) != JSON_STRING) | ||
goto error; | ||
const char *s = json_string_value (o); | ||
uint8_t hash[BLOBREF_MAX_DIGEST_SIZE]; | ||
if (blobref_strtohash (s, hash, sizeof (hash)) < 0) | ||
goto error; | ||
} | ||
else | ||
goto error; | ||
return 0; | ||
error: | ||
errno = EINVAL; | ||
return -1; | ||
} | ||
|
||
/* | ||
* vi:tabstop=4 shiftwidth=4 expandtab | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <stdbool.h> | ||
#include <jansson.h> | ||
|
||
/* JSON directory object: | ||
* list of key-value pairs where key is a name, value is a dirent | ||
* | ||
* JSON dirent objects: | ||
* object containing one key-value pair where key is one of | ||
* "FILEREF", "DIRREF", "FILEVAL", "DIRVAL", "LINKVAL", and value is a | ||
* blobref key into ctx->store (FILEREF, DIRREF), an actual directory, file | ||
* (value), or link target JSON object (FILEVAL, DIRVAL, LINKVAL). | ||
* | ||
* For example, consider KVS containing: | ||
* a="foo" | ||
* b="bar" | ||
* c.d="baz" | ||
* X -> c.d | ||
* | ||
* Root directory: | ||
* {"a":{"FILEREF":"f1d2d2f924e986ac86fdf7b36c94bcdf32beec15"}, | ||
* "b":{"FILEREF","8714e0ef31edb00e33683f575274379955b3526c"}, | ||
* "c":{"DIRREF","6eadd3a778e410597c85d74c287a57ad66071a45"}, | ||
* "X":{"LINKVAL","c.d"}} | ||
* | ||
* Deep copy of root directory: | ||
* {"a":{"FILEVAL":"foo"}, | ||
* "b":{"FILEVAL","bar"}, | ||
* "c":{"DIRVAL",{"d":{"FILEVAL":"baz"}}}, | ||
* "X":{"LINKVAL","c.d"}} | ||
* | ||
* On LINKVAL's: | ||
* - target is always fully qualified key name | ||
* - links are always followed in path traversal of intermediate directories | ||
* - for kvs_get, terminal links are only followed if 'readlink' flag is set | ||
* - for kvs_put, terminal links are never followed | ||
*/ | ||
|
||
/* Create a KVS dirent. | ||
* 'type' is one of { "FILEREF", "DIRREF", "FILEVAL", "DIRVAL", "LINKVAL" }. | ||
* 'arg' is dependent on the type. This function asserts on failure. | ||
*/ | ||
json_t *j_dirent_create (const char *type, void *arg); | ||
|
||
/* Append a JSON object containing | ||
* { "key" : key, "dirent" : dirent } | ||
* { "key" : key, "dirent" : nil } | ||
* to a json array, creating '*array' if necessary. This is used to build | ||
* a KVS commit, where each new object is an ordered operation that adds/ | ||
* changes/unlinks a key in KVS namespace. This function asserts on failure. | ||
*/ | ||
void j_dirent_append (json_t **array, const char *key, json_t *dirent); | ||
|
||
/* Compare two dirents. | ||
* N.B. The serialize/strcmp method used here can return false negatives, | ||
* but a positive can be relied on. | ||
*/ | ||
bool j_dirent_match (json_t *dirent1, json_t *dirent2); | ||
|
||
int j_dirent_validate (json_t *dirent); | ||
|
||
/* | ||
* vi:tabstop=4 shiftwidth=4 expandtab | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.