/* permssions string */ static void push_st_perm(lua_State *L, STAT_STRUCT *info) { lua_pushstring(L, perm2string(info->st_mode)); }
rdirectory *ssh_read_directory(const char *path) { char *p = ftp_path_absolute(path); stripslash(p); sftp_dir dir = sftp_opendir(ftp->sftp_session, p); if (!dir) { free(p); return 0; } ftp_trace("*** start parsing directory listing ***\n"); rdirectory* rdir = rdir_create(); sftp_attributes attrib = NULL; while ((attrib = sftp_readdir(ftp->sftp_session, dir)) != NULL) { ftp_trace("%s\n", attrib->longname); rfile* rf = rfile_create(); rf->perm = perm2string(attrib->permissions); rf->nhl = 0; // atoi(e); if (attrib->owner) rf->owner = xstrdup(attrib->owner); if (attrib->group) rf->group = xstrdup(attrib->group); if (asprintf(&rf->path, "%s/%s", p, attrib->name) == -1) { ftp_err(_("Failed to allocate memory.\n")); sftp_closedir(dir); free(p); rdir_destroy(rdir); rfile_destroy(rf); } rf->mtime = attrib->mtime; rf->date = time_to_string(rf->mtime); rf->size = attrib->size; rfile_parse_colors(rf); rf->link = NULL; if (rislink(rf) && ftp->ssh_version > 2) rf->link = sftp_readlink(ftp->sftp_session, rf->path); list_additem(rdir->files, (void *)rf); sftp_attributes_free(attrib); } ftp_trace("*** end parsing directory listing ***\n"); if (!sftp_dir_eof(dir)) { ftp_err(_("Couldn't list directory: %s\n"), ssh_get_error(ftp->session)); sftp_closedir(dir); free(p); rdir_destroy(rdir); return NULL; } sftp_closedir(dir); rdir->path = p; ftp_trace("added directory '%s' to cache\n", p); list_additem(ftp->cache, rdir); return rdir; }
/* permssions string */ static void push_st_perm (lua_State *L, struct yaffs_stat *info) { lua_pushstring (L, perm2string (info->st_mode)); }
rdirectory *ssh_read_directory(const char *path) { rdirectory *rdir; int i; SFTP_DIRENT **dir; char *p = ftp_path_absolute(path); stripslash(p); if(ssh_readdir(p, &dir) != 0) { free(p); return 0; } rdir = rdir_create(); ftp_trace("*** start parsing directory listing ***\n"); for(i = 0; dir[i]; i++) { rfile *rf; char *e, *cf = dir[i]->longname; ftp_trace("%s\n", dir[i]->longname); rf = rfile_create(); rf->perm = perm2string(dir[i]->a.perm); e = strqsep(&cf, ' '); /* skip permissions */ e = strqsep(&cf, ' '); /* nhl? */ /* if(ftp->ssh_version > 2) {*/ rf->nhl = atoi(e); /* } else*/ /* rf->nhl = 0;*/ #if 1 e = strqsep(&cf, ' '); rf->owner = xstrdup(e); e = strqsep(&cf, ' '); rf->group = xstrdup(e); #else asprintf(&rf->owner, "%d", dir[i]->a.uid); asprintf(&rf->group, "%d", dir[i]->a.gid); #endif asprintf(&rf->path, "%s/%s", p, dir[i]->filename); rf->mtime = dir[i]->a.mtime; if(rf->mtime == 0) { char *m, *d, *y; while(e && month_number(e) == -1) e = strqsep(&cf, ' '); if(e) { m = e; d = strqsep(&cf, ' '); y = strqsep(&cf, ' '); ftp_trace("parsing time: m:%s d:%s y:%s\n", m, d, y); rfile_parse_time(rf, m, d, y); } } rf->date = time_to_string(rf->mtime); rf->size = dir[i]->a.size; rfile_parse_colors(rf); rf->link = 0; if(rislink(rf) && ftp->ssh_version > 2) rf->link = ssh_readlink(rf->path); list_additem(rdir->files, (void *)rf); } ftp_trace("*** end parsing directory listing ***\n"); ssh_free_dirents(dir); rdir->path = p; ftp_trace("added directory '%s' to cache\n", p); list_additem(ftp->cache, rdir); return rdir; }
static int rfile_parse_mlsd(rfile *f, char *str, const char *dirpath) { char *e; bool isdir = false; if(!str) return -1; e = strchr(str, ' '); if(e) { if (asprintf(&f->path, "%s/%s", strcmp(dirpath, "/") ? dirpath : "", base_name_ptr(e+1)) == -1) { f->path = NULL; return -1; } *e = 0; } else return -1; f->perm = 0; f->size = 0L; f->mtime = 0; f->link = 0; f->nhl = 0; f->owner = xstrdup("owner"); f->group = xstrdup("group"); f->date = xstrdup("Jan 0 1900"); while((e = strqsep(&str, ';')) != 0) { char *factname, *value; factname = strqsep(&e, '='); value = e; if(!factname || !value) { return -1; } if(strcasecmp(factname, "size") == 0 || strcasecmp(factname, "sizd") == 0) /* the "sizd" fact is not standardized in "Extension to * FTP" Internet draft, but PureFTPd uses it for some * reason for size of directories */ f->size = strtoull(value,NULL,10); else if(strcasecmp(factname, "type") == 0) { if(strcasecmp(value, "file") == 0) isdir = false; else if(strcasecmp(value, "dir") == 0 || strcasecmp(value, "cdir") == 0 || strcasecmp(value, "pdir") == 0) isdir = true; } else if(strcasecmp(factname, "modify") == 0) { struct tm ts; sscanf(value, "%04d%02d%02d%02d%02d%02d", &ts.tm_year, &ts.tm_mon, &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec); ts.tm_year -= 1900; ts.tm_mon--; f->mtime = gmt_mktime(&ts); free(f->date); f->date = time_to_string(f->mtime); } else if(strcasecmp(factname, "UNIX.mode") == 0) { free(f->perm); f->perm = perm2string(strtoul(value, 0, 8)); } else if(strcasecmp(factname, "UNIX.gid") == 0) { free(f->group); f->group = xstrdup(value); } else if(strcasecmp(factname, "UNIX.uid") == 0) { free(f->owner); f->owner = xstrdup(value); } } if(!f->perm) f->perm = xstrdup("-rw-r--r--"); if(isdir) f->perm[0] = 'd'; rfile_parse_colors(f); return 0; }