Line data Source code
1 : //
2 : // repo_io.c
3 : // deployctl
4 : //
5 : // Created by Danny Goossen on 29/6/17.
6 : // Copyright (c) 2017 Danny Goossen. All rights reserved.
7 : //
8 :
9 : #include "deployd.h"
10 :
11 :
12 :
13 : /*------------------------------------------------------------------------
14 : * Read a JSON file with dirpath/filename
15 : * returns NULL on errors
16 : * returns a cJSON object, caller is responsible for freeing it!
17 : *------------------------------------------------------------------------*/
18 7 : cJSON * read_json(void * opaque, const char * base_path,const char * sub_path,const char * filename)
19 : {
20 : // debug
21 : //printf("%s\n",dirpath);
22 7 : if (!base_path) return NULL;
23 6 : if (!sub_path) return NULL;
24 5 : if (!filename || strlen(filename)==0) return NULL;
25 : char path[1024];
26 3 : sprintf(path,"%s%s/%s",base_path,sub_path,filename);
27 :
28 3 : FILE *f = fopen(path, "r");
29 3 : if (f == NULL)
30 : {
31 1 : debug("ERR: read json file %s:%s\n",path,strerror(errno));
32 1 : return NULL;
33 : }
34 : struct MemoryStruct mem;
35 2 : init_dynamicbuf(&mem);
36 : char buf[1024];
37 2 : bzero(buf, 1024);
38 8 : while( fgets( buf, 1024, f ) != NULL )
39 : {
40 4 : Writedynamicbuf(buf, &mem );
41 4 : bzero(buf, 1024);
42 : }
43 2 : fclose(f);
44 2 : cJSON * result=cJSON_Parse(mem.memory);
45 2 : free(mem.memory);
46 2 : return result;
47 : }
48 :
49 :
50 :
51 0 : cJSON * JSON_dir_list_x( const char *path,const char *subpath,int recursive, const char * extension)
52 : {
53 0 : DIR *dp=NULL;
54 : struct dirent *ep;
55 : char dirpath[1024];
56 : char filepath[1024];
57 : char thissubpath[1024];
58 0 : if(subpath)
59 : {
60 0 : sprintf(dirpath, "%s/%s",path,subpath);
61 :
62 : }
63 : else
64 : {
65 0 : sprintf(dirpath, "%s",path);
66 :
67 : }
68 0 : dp = opendir (dirpath);
69 0 : cJSON * filearray=NULL;;
70 0 : cJSON * tmp=NULL;
71 0 : if (dp != NULL)
72 : {
73 0 : while ((ep = readdir (dp)))
74 : {
75 0 : if (ep->d_name[0]!='.' && ep->d_type!=DT_DIR && (strstr(ep->d_name, extension)))
76 : {
77 0 : tmp= cJSON_CreateObject();
78 0 : cJSON_AddStringToObject(tmp, "filename", ep->d_name);
79 0 : cJSON_AddStringToObject(tmp, "path", dirpath);
80 0 : sprintf(filepath, "%s/%s",dirpath,ep->d_name);
81 0 : cJSON_AddStringToObject(tmp, "filepath",filepath);
82 0 : if (recursive)
83 : {
84 0 : cJSON_AddStringToObject(tmp, "subpath",subpath);
85 : }
86 0 : if (!filearray) filearray=cJSON_CreateArray();
87 0 : cJSON_AddItemToArray(filearray, tmp);
88 0 : } else if (ep->d_name[0]!='.' && ep->d_type==DT_DIR && recursive )
89 : {
90 0 : sprintf(thissubpath, "%s/%s",subpath,ep->d_name);
91 0 : cJSON * temp=JSON_dir_list_x( path,thissubpath,recursive, extension);
92 : // now merge temp into filearray
93 0 : if (temp)
94 : {
95 0 : cJSON * pos=NULL;
96 0 : cJSON_ArrayForEach(pos,temp)
97 : {
98 0 : if (!filearray) filearray=cJSON_CreateArray();
99 0 : cJSON_AddItemToArray(filearray, cJSON_Duplicate(pos, 1));
100 : }
101 0 : cJSON_Delete(temp);
102 0 : pos=NULL;
103 0 : temp=NULL;
104 : }
105 : }
106 : }
107 0 : (void) closedir (dp);
108 : }
109 : else
110 : {
111 0 : debug("Err: Directory %s JSON parser %s", dirpath, strerror(errno));
112 0 : return NULL;
113 : }
114 0 : return filearray;
115 : }
|