int _search(char *source_path, INPUT *keyword) { int ret = 0; struct stat statbuf; struct dirent *dir = NULL; DIR *file; char *fullpath; if ((file=opendir(source_path))==NULL) return 0; fullpath = (char*) __ax_malloc(MAX_FILE_PATH * sizeof(char)); while ((dir=readdir(file))) { if((strlen(source_path) + strlen(dir->d_name) + 2) > MAX_FILE_PATH) { FCGI_LOG("%s", "Error: path size too large"); continue; } sprintf(fullpath, "%s/%s", source_path, dir->d_name); fullpath[strlen(fullpath)] = '\0'; if (lstat(fullpath, &statbuf)<0) { ret = -1; break; } if (S_ISDIR(statbuf.st_mode)==0) { // file or link if (is_match(dir->d_name, keyword)) { total_file++; file_list = (char (*)[MAX_FILE_PATH]) __ax_realloc((void*)file_list, total_file * MAX_FILE_PATH * sizeof(char)); strcpy(file_list[total_file - 1], fullpath); } if (S_ISLNK(statbuf.st_mode)) { // do nothing continue; } else { } } else { // dir //do not include "." and ".." and ".@__thumb" if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..") || !strcmp(dir->d_name, ".@__thumb")) continue; if (is_match(dir->d_name, keyword)) { //print_file_xml(fullpath); //sort_by_type(fullpath, &list); total_folder++; folder_list = (char (*)[MAX_FILE_PATH]) __ax_realloc((void*)folder_list, total_folder * MAX_FILE_PATH * sizeof(char)); strcpy(folder_list[total_folder - 1], fullpath); } _search(fullpath, keyword); } } closedir(file); if (fullpath) __ax_free(fullpath); return ret; }
/* read one line from a file, allocating space as need be adjust length on return */ char *grab_line(FILE *f, const char *terminator, int *length) { int len = 1024; char *ret = __ax_malloc(len); int i = 0; int tlen = strlen(terminator); while (*length) { int c; if (i == len) { len *= 2; ret = __ax_realloc(ret, len); } c = fgetc(f); (*length)--; if (c == EOF) { (*length) = 0; break; } ret[i++] = c; if (memcmp(terminator, &ret[i-tlen], tlen) == 0) { i -= tlen; break; } } ret[i] = 0; return ret; }
struct cgi_state *cgi_init(void) { struct cgi_state *cgi; cgi = __ax_malloc(sizeof(*cgi)); memcpy(cgi, &cgi_base, sizeof(*cgi)); return cgi; }
void *x_malloc(size_t size) { void *ret; ret = __ax_malloc(size); if (!ret) { fprintf(stderr, "Out of memory on size %d\n", (int)size); _exit(1); } return ret; }
INPUT* CGI_INIT() { cgi = cgi_init(); setup(cgi); load_variables(cgi); struct cgi_var *var; INPUT *tmp, *head = NULL; for (var = cgi->variables; var; var = var->next) { tmp = (INPUT*)__ax_malloc(sizeof(INPUT)); strcpy(tmp->name, var->name); strcpy(tmp->val, var->value); tmp->next = head; head = tmp; } tmp = head; destroy(cgi); return head; }
/* add a name/value pair to the list of cgi variables */ void put(struct cgi_state *cgi, const char *name, const char *value) { struct cgi_var *var; int len; char *cgi_name, *p; if (!name || !value) return; var = __ax_malloc(sizeof(*var)); memset(var, 0, sizeof(*var)); var->next = cgi->variables; /* trim leading spaces */ while (*name && (*name == '+' || *name == ' ')) name++; var->name = __ax_strdup(name); var->value = __ax_strdup(value); unescape(var->name); unescape(var->value); /* trim trailing spaces */ len = strlen(var->value); while (len && isspace(var->value[len-1])) { var->value[len-1] = 0; len--; } for (p=var->name; *p; p++) { if (!isalnum(*p) && !strchr("_-", *p)) { *p = '_'; } } cgi->variables = var; __ax_asprintf(&cgi_name, "CGI_%s", var->name); __ax_free(cgi_name); }
int _get_tree(char *inode, int is_iso) { struct dirent **namelist; int i, total = 0; char *path = NULL; int exist, permission = 0; char (*share)[MAX_FILE_LENGTH], *inode_tmp; char *delim = "/", (*property)[8] = NULL, (*type)[16] = NULL; int g_access=SHARE_NOACCESS; WFM_SECTION_OBJECT *secList=NULL; path = (char *)__ax_malloc(strlen(inode) + SHARE_PATH_LENGTH + 15); exist = check_exist(inode, path); if(exist < 0){ Status_To_JSON(WFM2_FILE_NO_EXIST); if (path) __ax_free(path); return -1; } //for fselect to tell which one is folder or file tmp_path = (char *)__ax_malloc(strlen(path) + 1); strncpy(tmp_path, path, strlen(path)); tmp_path[strlen(path)] = 0x0; total = scandir(path, &namelist, fselect, alphasort); if(total < 0){ perror("scandir"); if (path) __ax_free(path); if (tmp_path) __ax_free(tmp_path); return -1; } else { char *full_path = NULL; share = (char (*)[MAX_FILE_LENGTH]) __ax_malloc(total * MAX_FILE_LENGTH * sizeof(char)); type = (char (*)[16])__ax_malloc(total * 16 * sizeof(char)); #ifdef ACL property = (char (*)[8])__ax_malloc(total * 8 * sizeof(char)); #endif for(i=0; i<total; i++) { int mode = 0; strcpy(share[i], namelist[i]->d_name); strcpy(type[i], "folder"); #ifdef ACL full_path = (char *)__ax_malloc(strlen(path) + strlen(namelist[i]->d_name) + 2); sprintf(full_path, "%s/%s", path, namelist[i]->d_name); if (Is_ACL_Enabled()) { mode = Get_ACL_User_Group_Security_For_Share(remote_user, full_path); sprintf(property[i], "%d", mode); } else { sprintf(property[i], "%d", 7); } if (full_path) __ax_free(full_path); #endif } // for natural sorting secList = __ax_calloc(total, sizeof(WFM_SECTION_OBJECT)); for(i=0; i<total; i++) { strncpy((secList+i)->share, share[i], strlen(share[i])); if(split_sort_filename(share[i], &(secList+i)->split_name) < 0) { int j; for(j=0; j<(i+1); j++) { free_wfm2_split_data((secList+j)->split_name.data); } if(secList != NULL) __ax_free(secList); if(share) __ax_free(share); return -1; } } if(strcmp(gSort_type, "DESC")) sorting_by_natural_tree("ASC", total, secList); else sorting_by_natural_tree("DESC", total, secList); for(i=0; i<total; i++) { memset(share[i], 0, sizeof(share[i])); strncpy(share[i], (secList+i)->share, strlen((secList+i)->share)); } Share_List_To_JSON(inode, share, total, 1, property, type); } // __ax_free for(i=0; i<total; i++) { free_wfm2_split_data((secList+i)->split_name.data); } if(secList != NULL) __ax_free(secList); // end natural sorting if (path) __ax_free(path); if (share) __ax_free(share); if (tmp_path) __ax_free(tmp_path); if (type) __ax_free(type); return -1; }
//type: 4 = folder, 8 = file, 0 = unknow int fselect(const struct dirent *dir) { VETO_INFO *veto_files; int file_count = 0, veto_hidden = 0, i; char tmp[16]; // Get_Private_Profile_String(GLOBAL, VETO_HIDDEN, " ", tmp, sizeof(tmp), WFM2_CONFIG_PATH); if (!strcmp(tmp, "yes")) file_count = 0; //file_count = get_veto_files_Ex(&veto_files, g_hidden_file, remote_user); /* for (i=0; i<file_count; i++) { //if veto file from list include *, hidden if (strstr((veto_files + i)->file_name, "*")) { char *p = NULL, *delim = "*", str[512], *pstr = NULL; strcpy(str, (veto_files + i)->file_name); p = strtok(str, delim); //if veto file = *XXXX, hidden if (((veto_files + i)->file_name)[0] == '*') { pstr = ((char *)dir->d_name) + strlen(dir->d_name); pstr = pstr - strlen(p); if (!strcmp(pstr, p)) { veto_hidden = 1; break; } } //if veto file = XXXX*, hidden else if (((veto_files + i)->file_name)[strlen((veto_files + i)->file_name) - 1] == '*'){ if (!strncmp(dir->d_name, p, strlen(p))) { veto_hidden = 1; break; } } //if veto file = XXXX*XXXX, hiddden else { if (!strncmp(dir->d_name, p, strlen(p))) { p = strtok(NULL, delim); pstr = ((char *)dir->d_name) + strlen(dir->d_name); pstr = pstr - strlen(p); if (!strcmp(pstr, p)) { veto_hidden = 1; break; } } } } else { if (!strcmp(dir->d_name, (veto_files + i)->file_name)) { veto_hidden = 1; break; } } } */ if (veto_hidden) return 0; else if(!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) return 0; else if(dir->d_type == 4) return 1; else if(dir->d_type == 0 || dir->d_type == 10 ) { char *full_path = NULL; struct stat buf; full_path = (char *)__ax_malloc(strlen(tmp_path) + strlen(dir->d_name) + 2); sprintf(full_path, "%s/%s", tmp_path, dir->d_name); stat(full_path, &buf); if (full_path) __ax_free(full_path); if (S_ISDIR(buf.st_mode)) return 1; else return 0; } else return 0; }
int handle_file_list(WFM2_FILE_LIST_PARAM *aListParam, WFM2_FILE_OBJECT *aFileObj) { char *full_path=NULL; int exist, i; int g_access=SHARE_NOACCESS, nPermission=0, is_iso=0; char *delim="/", *node_tmp=NULL; int nShareCount=0, nTotalShare=0; //SECTION_INFO *shareList=NULL; char *node1=NULL, *iso=NULL; int nLen=0, nLen1=0; // check iso section node1 = strstr(&aListParam->node[1], "/"); nLen = strlen(aListParam->node); if(node1 != NULL) { nLen1 = strlen(node1); iso = __ax_calloc(1, (nLen - nLen1 + 1)); strncpy(iso, &aListParam->node[1], (nLen - nLen1)); } else { iso = __ax_calloc(1, (nLen + 1)); strncpy(iso, &aListParam->node[1], nLen); } is_iso = 0; /* nShareCount = Get_NAS_Share_List_V2_ISO(&shareList, 0, 0, NULL, ISO_FILTER_ONLY, &nTotalShare); for(i=0; i<nTotalShare; i++){ if (!strcasecmp(iso, shareList[i].section_name)) { is_iso = 1; break; } } if(shareList != NULL) Release_List(shareList); */ if(iso) __ax_free(iso); /*The first request node is "/", but we do not display folder in "/", so * give null share json*/ if (!strcmp(aListParam->node, "/")) { File_List_To_JSON(NULL, 0, 0, 0, 0); return -1; } /* node_tmp = __ax_strdup(aListParam->node); g_access = Get_NAS_User_Security_For_Share_Ex(aListParam->remoteUser, strtok(node_tmp, delim)); #ifdef STORAGE_V2 if(g_access == SHARE_NOACCESS && aListParam->no_check == 0) #else if(g_access == SHARE_NOACCESS) #endif { Status_To_JSON(WFM2_PERMISSION_DENY); if(node_tmp) __ax_free(node_tmp); return -1; } if(node_tmp) __ax_free(node_tmp); */ //nEnd = nStart + nLimit - 1; aListParam->end = aListParam->start + aListParam->limit - 1; full_path = (char *)__ax_malloc(strlen(aListParam->node) + MAX_FILE_LENGTH + PATH_PREFIX_LENGTH + 256); exist = check_exist(aListParam->node, full_path); if(exist < 0) { #ifdef STORAGE_V2 if(aListParam->no_check == 0) #endif #ifdef LIBRSYNC if(aListParam->view_versioning == 0) #endif { Status_To_JSON(WFM2_FILE_NO_EXIST); if(full_path) __ax_free(full_path); return -1; } } #ifdef ACL //if not iso folder, check permission //the external disk and non-ext3 or non-ext4 vdd are not //supported for ACL //so do not check the acl permission if(Is_ACL_Enabled() && No_Support_ACL(aListParam->node) == 0) { #ifdef LIBRSYNC if(aListParam->view_versioning == 0) #endif if(!is_iso) { aListParam->permission = Get_ACL_User_Group_Security_For_Share(aListParam->remoteUser, full_path); if(aListParam->permission < 5) { Status_To_JSON(WFM2_PERMISSION_DENY); if (full_path) __ax_free(full_path); return -1; } } } else nPermission = g_access; #endif // ACL for (i=0; i<sizeof(sort_func)/sizeof(SORT_FUNC); i++) { if (!strcmp(sort_func[i].sort_mode, aListParam->sort)) { aListParam->sortmode = i; break; } } #ifdef STORAGE_V2 if (aListParam->no_check) { if(access(aListParam->node, 0) < 0) { Status_To_JSON(WFM2_FILE_NO_EXIST); if (full_path) __ax_free(full_path); return -1; } else { _get_file_list(aListParam->node, aListParam, aFileObj); } } else _get_file_list(full_path, aListParam, aFileObj); #else _get_file_list(full_path, aListParam, aFileObj); #endif if(full_path) __ax_free(full_path); return 0; }
int _get_file_list(char *path, WFM2_FILE_LIST_PARAM *aListParam, WFM2_FILE_OBJECT *aFileObj) { char tmp[MAX_FILE_PATH]; DIR *dir, *_dir; struct dirent * ptr; struct stat buf; int total=0, count=0, i, j, nFindPageNum=0;; int start=aListParam->start, end=aListParam->end, sort_mode=aListParam->sortmode, file_type=aListParam->filetype, permission=aListParam->permission, hidden_file=aListParam->hidden_file, recycle=aListParam->recycle; char q_dir_1[MAX_FILE_PATH]="Photos"; char q_dir_2[MAX_FILE_PATH]="Videos"; char q_dir_3[MAX_FILE_PATH]="Documents"; char q_dir_4[MAX_FILE_PATH]="Others"; char q_dir_5[MAX_FILE_PATH]="Music"; dir = opendir(path); while ((ptr = readdir(dir))!=NULL) { //do not include "." and ".." if (!strcmp(ptr->d_name, ".") || !strcmp(ptr->d_name, "..")) continue; //do not include q_dir if(!strcmp(path, "/tmp/mnt/Qsync")){ if (!strcmp(ptr->d_name,q_dir_1) || !strcmp(ptr->d_name,q_dir_2) || !strcmp(ptr->d_name,q_dir_3) || !strcmp(ptr->d_name,q_dir_4) || !strcmp(ptr->d_name,q_dir_5)) continue; } total++; } closedir(dir); _dir = opendir(path); all_file = (JSON_FILE_OBJECT *)__ax_malloc(total * sizeof(JSON_FILE_OBJECT)); memset(all_file, 0, (total * sizeof(JSON_FILE_OBJECT))); while ((ptr = readdir(_dir))!=NULL) { //do not include "." and ".." if (!strcmp(ptr->d_name, ".") || !strcmp(ptr->d_name, "..")) continue; //do not include q_dir if (!strcmp(path, "/tmp/mnt/Qsync")){ if (!strcmp(ptr->d_name, q_dir_1) || !strcmp(ptr->d_name,q_dir_2) || !strcmp(ptr->d_name,q_dir_3) || !strcmp(ptr->d_name,q_dir_4) || !strcmp(ptr->d_name,q_dir_5)) continue; } struct passwd *user; struct group *data; user = getpwuid(buf.st_uid); if (user == NULL) sprintf((all_file+count)->owner, "%d", buf.st_uid); else strcpy((all_file+count)->owner, user->pw_name); data = getgrgid(buf.st_gid); if (data == NULL) sprintf((all_file+count)->group, "%d", buf.st_gid); else strcpy((all_file+count)->group, data->gr_name); sprintf(tmp, "%s/%s", path, ptr->d_name); stat(tmp, &buf); (all_file+count)->filesize = (unsigned long long)(buf.st_size); (all_file+count)->mt = buf.st_mtime; strcpy((all_file+count)->filename, ptr->d_name); sprintf((all_file+count)->privilege, "%03o", (buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))); if (S_ISDIR(buf.st_mode)) { (all_file+count)->isfolder = 1; strcpy((all_file+count)->type_str, " "); } count++; if(count >= total) { break; } } closedir(_dir); // split sort filename for natural sort if(sort_mode == 1) { // sort by natural for (i=0; i<total; i++) { if(split_sort_filename((all_file+i)->filename, &(all_file+i)->split_name) < 0) { if(all_file) { for(j=0; j<(i+1); j++) free_wfm2_split_data((all_file+j)->split_name.data); __ax_free(all_file); } return -1; } } } //sorting by type (sort_func[sort_mode].function)(aListParam->dir, total); temp_file = (JSON_FILE_OBJECT *)__ax_malloc(total * sizeof(JSON_FILE_OBJECT)); memset(temp_file, 0, (total * sizeof(JSON_FILE_OBJECT))); j=0; //display folder first for (i=0; i<total; i++) { if ((all_file+i)->isfolder == 1) { *(temp_file+j) = *(all_file+i); j++; } } for (i=0; i<total; i++) { if ((all_file+i)->isfolder == 0) { *(temp_file+j) = *(all_file+i); if(aListParam->filename != NULL && strlen(aListParam->filename) > 0) { if(!strcmp((temp_file+j)->filename, aListParam->filename)) { nFindPageNum = (int)((i+1) / (end-start+1)); if(((i+1)%(end-start+1)) > 0) nFindPageNum += 1; if(nFindPageNum > 1) { int nLimit=0; nLimit = end-start+1; start = (nFindPageNum-1) * nLimit; end = start + (nLimit-1); } else start = 0; } } j++; } } if (all_file) __ax_free(all_file); if(total < 0){ perror("scandir"); //if (all_file) __ax_free(all_file); if (temp_file) __ax_free(temp_file); return -1; } else{ int total_obj = 0; JSON_FILE_OBJECT *file=NULL; if(total > end) total_obj = end - start + 1; else if(total <= end && total >= start) total_obj = total - start; else { //if (all_file) __ax_free(all_file); if (temp_file) __ax_free(temp_file); return -1; } file = (JSON_FILE_OBJECT *)__ax_malloc(total_obj * sizeof(JSON_FILE_OBJECT)); memset(file, 0, (total_obj * sizeof(JSON_FILE_OBJECT))); for (i=0; i<total_obj; i++) { strcpy((file+i)->filename, (temp_file+start)->filename); (file+i)->isfolder = (temp_file+start)->isfolder; (file+i)->filesize = (temp_file+start)->filesize; strcpy((file+i)->owner, (temp_file+start)->owner); strcpy((file+i)->group, (temp_file+start)->group); (file+i)->mt = (temp_file+start)->mt; strcpy((file+i)->mt_str, (temp_file+start)->mt_str); strcpy((file+i)->privilege, (temp_file+start)->privilege); start++; } if(temp_file) __ax_free(temp_file); File_List_To_JSON(file, total_obj, total, permission, 0); if(file) __ax_free(file); } return 0; }
int op_get_file_list(INPUT *input) { INPUT *tmp; WFM2_FILE_LIST_PARAM sFileListParam; char *full_path = NULL; int nRet=0; memset(&sFileListParam, 0, sizeof(WFM2_FILE_LIST_PARAM)); if((tmp = CGI_Find_Parameter(input, "path"))) { full_path = (char *)__ax_malloc(strlen(tmp->val) + MAX_FILE_LENGTH + PATH_PREFIX_LENGTH + 256); int exist = check_exist(tmp->val, full_path); if (exist < 0) { Status_To_JSON(WFM2_FILE_NO_EXIST); if(full_path) __ax_free(full_path); return -1; } sFileListParam.node = __ax_strdup(tmp->val); } if((tmp = CGI_Find_Parameter(input, "list_mode"))) { strncpy(sFileListParam.listMode, tmp->val, WFM2_LIST_MODE_LEN); } if((tmp = CGI_Find_Parameter(input, "start"))) { sFileListParam.start = atoi(tmp->val); } else { sFileListParam.start = 0; } if((tmp = CGI_Find_Parameter(input, "limit"))) { sFileListParam.limit = atoi(tmp->val); } else { sFileListParam.limit = 0; } if((tmp = CGI_Find_Parameter(input, "is_iso"))) { sFileListParam.is_iso = atoi(tmp->val); } else { sFileListParam.is_iso = 0; } if((tmp = CGI_Find_Parameter(input, "type"))) { // 1:music, 2:video, 3:photo, 4: folder sFileListParam.filetype = atoi(tmp->val); if(sFileListParam.filetype < MUSIC || sFileListParam.filetype > MEDIA) sFileListParam.filetype = 0; } if((tmp = CGI_Find_Parameter(input, "mp4_360"))) { sFileListParam.mp4_360 = atoi(tmp->val); } else { sFileListParam.mp4_360 = 0; } if((tmp = CGI_Find_Parameter(input, "mp4_720"))) { sFileListParam.mp4_720 = atoi(tmp->val); } else { sFileListParam.mp4_720 = 0; } if((tmp = CGI_Find_Parameter(input, "mp4_240"))) { sFileListParam.mp4_240 = atoi(tmp->val); } else { sFileListParam.mp4_240 = 0; } if((tmp = CGI_Find_Parameter(input, "mp4_480"))) { sFileListParam.mp4_480 = atoi(tmp->val); } else { sFileListParam.mp4_480 = 0; } if((tmp = CGI_Find_Parameter(input, "mp4_1080"))) { sFileListParam.mp4_1080 = atoi(tmp->val); } else { sFileListParam.mp4_1080 = 0; } // for special video file if((tmp = CGI_Find_Parameter(input, "filename"))) { strncpy(sFileListParam.filename, tmp->val, (MAX_FILE_LENGTH-1)); } if((tmp = CGI_Find_Parameter(input, "sort"))) { strncpy(sFileListParam.sort, tmp->val, WFM2_SORT_MODE_LEN); } if((tmp = CGI_Find_Parameter(input, "dir"))) { strncpy(sFileListParam.dir, tmp->val, WFM2_SORT_TYPE_LEN); } if((tmp = CGI_Find_Parameter(input, "hidden_file"))) { sFileListParam.hidden_file = atoi(tmp->val); } else { sFileListParam.hidden_file = 0; } nRet = handle_file_list(&sFileListParam, NULL); //MARK: original handle list function //sFileListParam.end = sFileListParam.start + sFileListParam.limit - 1; //_get_file_list(full_path, &sFileListParam, NULL); if(full_path) __ax_free(full_path); if(sFileListParam.node != NULL) __ax_free(sFileListParam.node); return nRet; }
int op_search(INPUT *input) { int ret = 0; INPUT *keyword = NULL, *start = NULL, *limit = NULL; INPUT *source = NULL, *tmp = NULL, *time = NULL; char *full_path = NULL, *source_tmp = NULL, *sort_type = "ASC"; int exist = 0, end = 0, sort_mode = 0, permission = NO, is_iso = 0; //only for internal use ret = special_search(input); if (ret == 0) return 0; source = CGI_Find_Parameter(input, "source_path"); if (source == NULL) return -1; if(!Check_Illegal_Path(source->val)) { Status_To_JSON(WFM2_ILLEGAL_NAME); return -1; } time = CGI_Find_Parameter(input, "time"); if (time != NULL) { char *ptr = NULL, tmp[12]; struct tm *p; time_t timep; struct stat buf; struct passwd *user; struct group *data; int i; JSON_FILE_OBJECT *file; ptr = time->val; strncpy(tmp, ptr, 4); tmp[4] = 0x0; p = (struct tm *)__ax_malloc(sizeof(struct tm)); p->tm_year = atoi(tmp) - 1900; ptr = ptr + 4; strncpy(tmp, ptr, 2); tmp[2] = 0x0; p->tm_mon = atoi(tmp) - 1; ptr = ptr + 2; strncpy(tmp, ptr, 2); tmp[2] = 0x0; p->tm_mday = atoi(tmp); p->tm_hour = 0; p->tm_min = 0; p->tm_sec = 0; timep = mktime(p); if (p) __ax_free(p); full_path = (char *)__ax_malloc(strlen(source->val) + MAX_FILE_LENGTH + 15); exist = check_exist(source->val, full_path); if (exist < 0) { Status_To_JSON(WFM2_FILE_NO_EXIST); if (full_path) __ax_free(full_path); return -1; } _search_by_time(full_path, timep, 0); file = (JSON_FILE_OBJECT *)__ax_malloc( total_file * sizeof(JSON_FILE_OBJECT)); for (i=0;i<total_file;i++) { lstat(file_list[i], &buf); strcpy((file+i)->filename, file_list[i]); (file+i)->isfolder = 0; (file+i)->filesize = (unsigned long long)(buf.st_size); user = getpwuid(buf.st_uid); if (user == NULL) sprintf((file+i)->owner, "%d", buf.st_uid); else strcpy((file+i)->owner, user->pw_name); data = getgrgid(buf.st_gid); if (data == NULL) sprintf((file+i)->group, "%d", buf.st_gid); else strcpy((file+i)->group, data->gr_name); /* { char sDateStr[64]={0}; char sTimeStr[64]={0}; Change_Date_To_String_By_Format(buf.st_mtime, sDateStr, nDate_format); Change_Time_To_String_By_Format(buf.st_mtime, sTimeStr, 0, nTime_format); sprintf((file+i)->mt_str, "%s %s", sDateStr, sTimeStr); } */ sprintf((file+i)->privilege, "%o", (buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))); } File_List_To_JSON(file, total_file, total_file, 0, 0); if (full_path) __ax_free(full_path); if (file) __ax_free(file); return 0; } if((tmp = CGI_Find_Parameter(input, "is_iso"))) is_iso = atoi(tmp->val); /* source_tmp = (char *)__ax_malloc(strlen(source->val) + 1); strcpy(source_tmp, source->val); g_access = Get_NAS_User_Security_For_Share_Ex(remote_user, strtok(source_tmp, delim)); if (g_access == SHARE_NOACCESS) { Status_To_JSON(WFM2_PERMISSION_DENY); if(source_tmp) __ax_free(source_tmp); return -1; } */ full_path = (char *)__ax_malloc(strlen(source->val) + MAX_FILE_LENGTH + 15); exist = check_exist(source->val, full_path); if (exist < 0) { Status_To_JSON(WFM2_FILE_NO_EXIST); if (full_path) __ax_free(full_path); return -1; } #ifdef ACL //if not iso folder, check permission if (Is_ACL_Enabled() && No_Support_ACL(source_tmp) == 0) { if (!is_iso) { permission= Get_ACL_User_Group_Security_For_Share(remote_user, full_path); if (permission < 5) { Status_To_JSON(WFM2_PERMISSION_DENY); if (full_path) __ax_free(full_path); return -1; } } } #endif if(source_tmp) __ax_free(source_tmp); keyword = CGI_Find_Parameter(input, "keyword"); if (keyword == NULL) { //CGI_Free_Input(source); if (full_path) __ax_free(full_path); return -1; } start = CGI_Find_Parameter(input, "start"); if (start == NULL) { if (full_path) __ax_free(full_path); return -1; } limit = CGI_Find_Parameter(input, "limit"); if (limit == NULL) { if (full_path) __ax_free(full_path); return -1; } _search(full_path, keyword); /* if (keyword!=NULL) CGI_Free_Input(keyword); if (source!=NULL) CGI_Free_Input(source); */ int i; end = atoi(start->val) + atoi(limit->val) - 1; if ((tmp = CGI_Find_Parameter(input, "sort"))) { for (i=0; i<sizeof(sort_func)/sizeof(SORT_FUNC); i++) { if (!strcmp(sort_func[i].sort_mode, tmp->val)) { sort_mode = i; break; } } } if ((tmp = CGI_Find_Parameter(input, "dir"))) sort_type = tmp->val; output_search_to_json(atoi(start->val), end, sort_mode, sort_type, permission); if (full_path) __ax_free(full_path); if (file_list) __ax_free(file_list); file_list = NULL; if(folder_list) __ax_free(folder_list); folder_list = NULL; total_match = 0; total_search = 0; total_file = 0; total_folder = 0; return ret; }
int special_search(INPUT *input) { INPUT *tmp = NULL; char *ptr = NULL, tmp_time[12], *folder = NULL; struct tm *p; time_t timep_begin, timep_end; int i; //SECTION_INFO *shareList=NULL; JSON_FILE_OBJECT *file; struct stat buf; struct passwd *user; struct group *data; tmp = CGI_Find_Parameter(input, "Time_begin"); if (tmp == NULL) return -1; else { ptr = tmp->val; strncpy(tmp_time, ptr, 4); tmp_time[4] = 0x0; p = (struct tm *)__ax_malloc(sizeof(struct tm)); p->tm_year = atoi(tmp_time) - 1900; ptr = ptr + 4; strncpy(tmp_time, ptr, 2); tmp_time[2] = 0x0; p->tm_mon = atoi(tmp_time) - 1; ptr = ptr + 2; strncpy(tmp_time, ptr, 2); tmp_time[2] = 0x0; p->tm_mday = atoi(tmp_time); p->tm_hour = 0; p->tm_min = 0; p->tm_sec = 0; timep_begin = mktime(p); if (p) __ax_free(p); } tmp = CGI_Find_Parameter(input, "Time_end"); if (tmp == NULL) return -1; else { ptr = tmp->val; strncpy(tmp_time, ptr, 4); tmp_time[4] = 0x0; p = (struct tm *)__ax_malloc(sizeof(struct tm)); p->tm_year = atoi(tmp_time) - 1900; ptr = ptr + 4; strncpy(tmp_time, ptr, 2); tmp_time[2] = 0x0; p->tm_mon = atoi(tmp_time) - 1; ptr = ptr + 2; strncpy(tmp_time, ptr, 2); tmp_time[2] = 0x0; p->tm_mday = atoi(tmp_time); p->tm_hour = 0; p->tm_min = 0; p->tm_sec = 0; timep_end = mktime(p); if (p) __ax_free(p); } tmp = CGI_Find_Parameter(input, "Folder_name"); if (tmp == NULL) return -1; else folder = tmp->val; /* Get_NAS_Share_List_V2(&shareList, 0, 0, NULL, &total_share); for (i=0; i<total_share; i++) { sprintf(buffer, "/%s/%s", shareList[i].section_name, folder); full_path = (char *)__ax_malloc(strlen(folder) + MAX_FILE_LENGTH + 15); exist = check_exist(buffer, full_path); if (exist < 0) { //Status_To_JSON(WFM2_FILE_NO_EXIST); //if (full_path) __ax_free(full_path); // return -1; } else _search_by_time(full_path, timep_begin, timep_end); if (full_path) __ax_free(full_path); } */ file = (JSON_FILE_OBJECT *)__ax_malloc( total_file * sizeof(JSON_FILE_OBJECT)); for (i=0;i<total_file;i++) { lstat(file_list[i], &buf); strcpy((file+i)->filename, file_list[i]); (file+i)->isfolder = 0; (file+i)->filesize = (unsigned long long)(buf.st_size); user = getpwuid(buf.st_uid); if (user == NULL) sprintf((file+i)->owner, "%d", buf.st_uid); else strcpy((file+i)->owner, user->pw_name); data = getgrgid(buf.st_gid); if (data == NULL) sprintf((file+i)->group, "%d", buf.st_gid); else strcpy((file+i)->group, data->gr_name); /* { char sDateStr[64]={0}; char sTimeStr[64]={0}; Change_Date_To_String_By_Format(buf.st_mtime, sDateStr, nDate_format); Change_Time_To_String_By_Format(buf.st_mtime, sTimeStr, 0, nTime_format); sprintf((file+i)->mt_str, "%s %s", sDateStr, sTimeStr); } */ sprintf((file+i)->privilege, "%o", (buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))); } File_List_To_JSON(file, total_file, total_file, 0, 0); if (file) __ax_free(file); //if(shareList != NULL) Release_List(shareList); return 0; }
int output_search_to_json(int start, int end, int sort_mode, char *sort_type, int permission) { int total_obj = 0, total = total_file + total_folder, i = 0, j = 0, k = 0; int file_start = 0, folder_start = 0, file_end = 0, folder_end = 0; JSON_FILE_OBJECT *file; struct stat buf; struct passwd *user; struct group *data; char *p = NULL; all_file = (JSON_FILE_OBJECT *)__ax_malloc(total_file * sizeof(JSON_FILE_OBJECT)); all_folder = (JSON_FILE_OBJECT *)__ax_malloc(total_folder * sizeof(JSON_FILE_OBJECT)); for (k=0; k<total_folder; k++) { lstat(folder_list[k], &buf); if (S_ISDIR(buf.st_mode)) { strcpy((all_folder+k)->filename, folder_list[k]); (all_folder+k)->isfolder = 1; strcpy((all_folder+k)->type_str, " "); (all_folder+k)->filesize = (unsigned long long)(buf.st_size); user = getpwuid(buf.st_uid); if (user == NULL) sprintf((all_folder+k)->owner, "%d", buf.st_uid); else strcpy((all_folder+k)->owner, user->pw_name); data = getgrgid(buf.st_gid); if (data == NULL) sprintf((all_folder+k)->group, "%d", buf.st_gid); else strcpy((all_folder+k)->group, data->gr_name); (all_folder+k)->mt = buf.st_mtime; /* { char sDateStr[64]={0}; char sTimeStr[64]={0}; Change_Date_To_String_By_Format(buf.st_mtime, sDateStr, nDate_format); Change_Time_To_String_By_Format(buf.st_mtime, sTimeStr, 0, nTime_format); sprintf((all_folder+k)->mt_str, "%s %s", sDateStr, sTimeStr); } */ strftime((all_folder+k)->mt_str, 32, "%Y-%m-%d %H:%M:%S", localtime(&buf.st_mtime)); sprintf((all_folder+k)->privilege, "%o", (buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))); } } for (k=0; k<total_file; k++) { lstat(file_list[k], &buf); if (S_ISDIR(buf.st_mode) == 0) { strcpy((all_file+k)->filename, file_list[k]); (all_file+k)->isfolder = 0; p = strrchr(file_list[k], '.'); if (p != 0) { p = p + 1; strncpy((all_file+k)->type_str, p, 11); (all_file+k)->type_str[11] = '\0'; } else{ strcpy((all_file+k)->type_str, " "); } (all_file+k)->filesize = (unsigned long long)(buf.st_size); user = getpwuid(buf.st_uid); if (user == NULL) sprintf((all_file+k)->owner, "%d", buf.st_uid); else strcpy((all_file+k)->owner, user->pw_name); data = getgrgid(buf.st_gid); if (data == NULL) sprintf((all_file+k)->group, "%d", buf.st_gid); else strcpy((all_file+k)->group, data->gr_name); (all_file+k)->mt = buf.st_mtime; /* { char sDateStr[64]={0}; char sTimeStr[64]={0}; Change_Date_To_String_By_Format(buf.st_mtime, sDateStr, nDate_format); Change_Time_To_String_By_Format(buf.st_mtime, sTimeStr, 0, nTime_format); sprintf((all_file+k)->mt_str, "%s %s", sDateStr, sTimeStr); } */ strftime((all_file+k)->mt_str, 32, "%Y-%m-%d %H:%M:%S", localtime(&buf.st_mtime)); sprintf((all_file+k)->privilege, "%o", (buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))); } } //sorting by type (sort_func[sort_mode].function)(sort_type, total); if (total > end) total_obj = end - start + 1; else if (total <= end && total >= start) total_obj = total - start; else { if (all_file) __ax_free(all_file); if (all_folder) __ax_free(all_folder); return -1; } file = (JSON_FILE_OBJECT *)__ax_malloc(total_obj * sizeof(JSON_FILE_OBJECT)); memset(file, 0, (total_obj * sizeof(JSON_FILE_OBJECT))); //output file and folder count. k = 0; //output folder first. if (start < total_folder) { folder_start = start; if (total_folder > end) folder_end = end + 1; else if (total_folder <= end && total_folder >= start) folder_end = total_folder; else goto output_file; for (i=folder_start; i<folder_end; i++) { strcpy((file+k)->filename, (all_folder+folder_start)->filename); (file+k)->isfolder = 1; (file+k)->filesize = (all_folder+folder_start)->filesize; strcpy((file+k)->owner, (all_folder+folder_start)->owner); strcpy((file+k)->group, (all_folder+folder_start)->group); (file+k)->mt = (all_folder+folder_start)->mt; strcpy((file+k)->mt_str, (all_folder+folder_start)->mt_str); strcpy((file+k)->privilege, (all_folder+folder_start)->privilege); folder_start++; k++; } } output_file: if (start < (total_folder + total_file)) { if (start < total_folder) file_start = 0; else file_start = start - total_folder; if ((total_folder+total_file) > end) file_end = end + 1; else if ((total_folder+total_file) <= end && (total_folder+total_file) >= start) file_end = total_folder+total_file; else goto output_end; if (start > folder_end) folder_end = start; for (j=folder_end; j<file_end; j++) { strcpy((file+k)->filename, (all_file+file_start)->filename); (file+k)->isfolder = 0; (file+k)->filesize = (all_file+file_start)->filesize; strcpy((file+k)->owner, (all_file+file_start)->owner); strcpy((file+k)->group, (all_file+file_start)->group); (file+k)->mt = (all_file+file_start)->mt; strcpy((file+k)->mt_str, (all_file+file_start)->mt_str); strcpy((file+k)->privilege, (all_file+file_start)->privilege); file_start++; k++; } } File_List_To_JSON(file, total_obj, total, permission, 0); output_end: if (file) __ax_free(file); if (all_file) __ax_free(all_file); if (all_folder) __ax_free(all_folder); return 0; }
/* * Write your current content to file * */ int write_one_part(struct cgi_state *cgi, FILE *f, int *len, char *boundary, char *filename, char *name, char *tmp_path, FILE *tmpfile) { int ret = 1; int towrite = 0; unsigned content_len=0; char *content, *buf_content; unsigned boundary_len = strlen(boundary); int c; int current_index = 0, write_index = 0, buf_index = 0; int bytes = 0; content = __ax_malloc(boundary_len + 5); // "boundary" + "--" + "\r\n" + "\0" buf_content = __ax_malloc(BLK_SIZE + 1); while (*len && (c = fgetc(f)) != EOF) { (*len)--; /* * reference_boundry[boundary_len + 3]; * write_index = 0; * if(write_index < boundary_len) { * reference_boundry[write_index] = c; * write_index ++; * } else { * memcpy(reference_boundry, reference_boundry + 1, boundary_len + 1); * reference_boundry[write_index] = c; * } * * */ current_index = content_len++; // increase content length if (write_index == (boundary_len + 4)) { // if content is full, try to compare boundary // If grab boundary .... if (memcmp(boundary, &content[4], boundary_len) == 0 && memcmp("--", &content[2], 2) == 0) { content_len -= boundary_len+4; if (name) { put(cgi, name, filename?filename:""); cgi->variables->content_len = content_len; } else { content[boundary_len + 2] = 0; put(cgi, name, content); } fgetc(f); fgetc(f); (*len) -= 2; towrite = 1; } else { // Cannot grab boundary, just pop out current character to buffer buf_content[buf_index] = content[0]; buf_content[buf_index + 1] = 0; memcpy(content, content+1, boundary_len + 3); content[write_index - 1] = c; buf_index++; } } else { // if content is not full content[write_index] = c; write_index++; } if ( ((buf_index == BLK_SIZE) || feof(f) || (towrite)) && (tmpfile != NULL)) { // Write file if (buf_index && towrite){ bytes = fwrite(buf_content, 1, buf_index, tmpfile); } buf_index = 0; __ax_free(buf_content); if(towrite) { return ret; } buf_content = __ax_malloc(BLK_SIZE + 1); } } if (buf_content) __ax_free(buf_content); return ret; }
/* parse a single element of a multipart encoded form It's rather more complex than I would like :( */ int load_one_part(struct cgi_state *cgi, FILE *f, int *len, char *boundary) { char *line; char *name=NULL; char *content; char *filename=NULL; unsigned content_len=0, content_alloc=1024; unsigned boundary_len = strlen(boundary); int c; int raw_data = 0; int towrite = 0; const char *chunked_upload = get(cgi, "func"); const char *upload_root_dir = get(cgi, "upload_root_dir"); const char *upload_id = get(cgi, "upload_id"); char tmp_path[2048] = ""; FILE *file = NULL; int is_chunked_upload = 0; if (chunked_upload){ if (!strcmp(chunked_upload, "chunked_upload")) { is_chunked_upload = 1; // Write file with upload_id : Combine the path to upload_id temp file strcpy(tmp_path, "/tmp/mnt"); strcat(tmp_path, upload_root_dir); strcat(tmp_path, "/"); strcat(tmp_path, ".upload_cache"); strcat(tmp_path, "/"); strcat(tmp_path, upload_id); // Open file file = fopen(tmp_path, "a"); } } while (*len && (line=grab_line(f, CRLF, len))) { if (*line == 0) break; if (strcmp(line,"--") == 0) return 1; if (strncasecmp(line, CONTENT_TYPE, strlen(CONTENT_TYPE)) == 0) { raw_data = 1; } if (strncasecmp(line, CONTENT_DISPOSITION, strlen(CONTENT_DISPOSITION)) == 0) { char *p = strstr(line,"; name="); if (!p) continue; p += 7; if (*p == '"') p++; name = strndup(p, strcspn(p, "\";")); p = strstr(line,"; filename=\""); if (p) { p += 12; filename = strndup(p, strcspn(p, "\";")); } } } if (raw_data && name && (tmp_path[0] != '\0') && file) { // If you want to write file towrite = write_one_part(cgi, f, len, boundary, filename, name, tmp_path, file); } else { // If you don't want to write file content = __ax_malloc(content_alloc); while (*len && (c = fgetc(f)) != EOF) { (*len)--; if (content_len >= (content_alloc-1)) { content_alloc *= 2; content = __ax_realloc(content, content_alloc); } content[content_len++] = c; /* we keep grabbing content until we hit a boundary */ if (memcmp(boundary, &content[content_len-boundary_len], boundary_len) == 0 && memcmp("--", &content[content_len-boundary_len-2], 2) == 0) { content_len -= boundary_len+4; if (name) { if (raw_data || filename) { put(cgi, name, filename?filename:""); cgi->variables->content = content; cgi->variables->content_len = content_len; } else { content[content_len] = 0; put(cgi, name, content); __ax_free(name); __ax_free(content); } } else { __ax_free(content); } fgetc(f); fgetc(f); (*len) -= 2; return 0; } } } if (filename) __ax_free(filename); if (file) fclose(file); return 1; }