/* * Reading local directory to get specific module item's contents. * In future if needed we can change this to reading from smd metadata. */ int udf_cask_info_get(char *name, char * params, cf_dyn_buf * out) { int resp = 0; char filename[128] = {0}; int filename_len = sizeof(filename); uint8_t * content = NULL; size_t content_len = 0; unsigned char content_gen[256] = {0}; uint8_t udf_type = AS_UDF_TYPE_LUA; cf_debug(AS_INFO, "UDF CASK INFO GET"); // get (required) script filename if ( as_info_parameter_get(params, "filename", filename, &filename_len) ) { cf_info(AS_INFO, "invalid or missing filename"); cf_dyn_buf_append_string(out, "error=invalid_filename"); return 0; } mod_lua_rdlock(&mod_lua); // read the script from filesystem resp = file_read(filename, &content, &content_len, content_gen); mod_lua_unlock(&mod_lua); if ( resp ) { switch ( resp ) { case 1 : { cf_dyn_buf_append_string(out, "error=not_found"); break; } case 2 : { cf_dyn_buf_append_string(out, "error=empty"); break; } default : { cf_dyn_buf_append_string(out, "error=unknown_error"); break; // complier complains without a break; } } } else { // put back the result cf_dyn_buf_append_string(out, "gen="); cf_dyn_buf_append_string(out, (char *) content_gen); cf_dyn_buf_append_string(out, ";type="); cf_dyn_buf_append_string(out, as_udf_type_name[udf_type]); cf_dyn_buf_append_string(out, ";content="); cf_dyn_buf_append_buf(out, content, content_len); cf_dyn_buf_append_string(out, ";"); } if ( content ) { cf_free(content); content = NULL; } return 0; }
int udf_cask_info_list(char *name, cf_dyn_buf * out) { DIR * dir = NULL; bool not_empty = false; struct dirent * entry = NULL; int count = 0; uint8_t * content = NULL; size_t content_len = 0; unsigned char content_gen[256] = {0}; unsigned char hash[SHA_DIGEST_LENGTH]; // hex string to be returned to the client unsigned char sha1_hex_buff[CF_SHA_HEX_BUFF_LEN]; cf_debug(AS_INFO, "UDF CASK INFO LIST"); // Currently just return directly for LUA uint8_t udf_type = AS_UDF_TYPE_LUA; dir = opendir(g_config.mod_lua.user_path); if ( dir == 0 ) { cf_warning(AS_UDF, "could not open udf directory %s: %s", g_config.mod_lua.user_path, cf_strerror(errno)); return -1; } while ( (entry = readdir(dir)) && entry->d_name ) { char * name = entry->d_name; size_t len = strlen(name); // if ( len < 4 ) continue; if ( name[0] == '.' ) continue; if ( not_empty ) { cf_dyn_buf_append_char(out, ';'); } else { not_empty = true; } cf_dyn_buf_append_string(out, "filename="); cf_dyn_buf_append_buf(out, (uint8_t *) name, len); cf_dyn_buf_append_string(out, ","); mod_lua_rdlock(&mod_lua); if (file_read(name, &content, &content_len, content_gen) != 0) { cf_info(AS_UDF, "UDF-list : file not readable"); cf_dyn_buf_append_string(out, "error=file_not_readable"); mod_lua_unlock(&mod_lua); return 0; } mod_lua_unlock(&mod_lua); SHA1(content, content_len, hash); // Convert to a hexadecimal string cf_convert_sha1_to_hex(hash, sha1_hex_buff); cf_dyn_buf_append_string(out, "hash="); cf_dyn_buf_append_buf(out, sha1_hex_buff, CF_SHA_HEX_BUFF_LEN); cf_dyn_buf_append_string(out, ",type="); cf_dyn_buf_append_string(out, as_udf_type_name[udf_type]); count ++; } if (not_empty) { cf_dyn_buf_append_string(out, ";"); } closedir(dir); return 0; }