SmbDir(const QUrl &url) { Q_UNUSED(url); m_error = SmbAuth::Unsupported; #if HAVE_SAMBA m_url = url; m_dh = smbc_opendir(url.toString(QUrl::FullyEncoded).toLatin1()); if (m_dh < 0) { switch (errno) { case EACCES: m_error = SmbAuth::NoPermission; break; case EINVAL: m_error = SmbAuth::InvalidUrl; break; case ENOENT: m_error = SmbAuth::NotExistingPath; break; case ENOMEM: m_error = SmbAuth::OutOfMemory; break; case EPERM: case ENODEV: m_error = SmbAuth::NotExistingShare; break; default: break; } return; } smbc_dirent *child = nullptr; while ((child = smbc_readdir(m_dh))) { qDebug() << QByteArray(child->name, child->namelen); } #endif }
void search_smb(char *url) { struct smbc_dirent* dirptr; int dh = 0; dh = smbc_opendir(url); while((dirptr = smbc_readdir(dh)) != NULL){ if(!strcmp(dirptr->name, ".") || !strcmp(dirptr->name, "..")) continue; char buf[MAX_BUFF_SIZE] = {0}; sprintf(buf, "%s%s", url, dirptr->name); struct stat info; smbc_stat(buf, &info); if(dirptr->smbc_type == SMBC_FILE) { printf("FILE-------%s%s\n", url, dirptr->name); printf("-----------mtime:%lu\n", info.st_mtime); printf("-----------size :%lld\n", info.st_size); printf("-----------mode :%d S_IDDIR :%d\n", info.st_mode, S_ISDIR(info.st_mode)); } else if(dirptr->smbc_type == SMBC_DIR) { printf("FOLDER-----%s%s\n", url, dirptr->name); printf("-----------mode :%d S_IDDIR :%d\n", info.st_mode, S_ISDIR(info.st_mode)); char new_url[255] = {0}; sprintf(new_url, "%s%s/", url, dirptr->name); search_smb(new_url); } } smbc_closedir(dh); }
/** First information such as permissions is gathered for each directory entry. * All entries are then sorted. */ static struct directory_entry * get_smb_directory_entries(int dir, struct string *prefix) { struct directory_entry *entries = NULL; int size = 0; struct smbc_dirent *entry; while ((entry = smbc_readdir(dir))) { struct stat st, *stp; struct directory_entry *new_entries; struct string attrib; struct string name; if (!strcmp(entry->name, ".")) continue; new_entries = mem_realloc(entries, (size + 2) * sizeof(*new_entries)); if (!new_entries) continue; entries = new_entries; if (!init_string(&attrib)) { continue; } if (!init_string(&name)) { done_string(&attrib); continue; } add_string_to_string(&name, prefix); add_to_string(&name, entry->name); stp = (smbc_stat(name.source, &st)) ? NULL : &st; stat_type(&attrib, stp); stat_mode(&attrib, stp); stat_links(&attrib, stp); stat_user(&attrib, stp); stat_group(&attrib, stp); stat_size(&attrib, stp); stat_date(&attrib, stp); entries[size].name = stracpy(entry->name); entries[size].attrib = attrib.source; done_string(&name); size++; } smbc_closedir(dir); if (!size) { /* We may have allocated space for entries but added none. */ mem_free_if(entries); return NULL; } qsort(entries, size, sizeof(*entries), compare_dir_entries); memset(&entries[size], 0, sizeof(*entries)); return entries; }
struct smbc_dirent* smbc_wrapper_readdir(connection* con, unsigned int dh) { if(con->mode== SMB_BASIC) return smbc_readdir(dh); else if(con->mode == SMB_NTLM) return smbc_cli_readdir(dh); return NULL; }
/***************************************************************************** * DirRead: *****************************************************************************/ static input_item_t* DirRead (access_t *p_access ) { access_sys_t *p_sys = p_access->p_sys; struct smbc_dirent *p_entry; input_item_t *p_item = NULL; while( !p_item && ( p_entry = smbc_readdir( p_sys->i_smb ) ) ) { char *psz_uri; const char *psz_server = p_sys->url.psz_host; const char *psz_path = p_sys->url.psz_path; const char *psz_name = p_entry->name; int i_type; switch( p_entry->smbc_type ) { case SMBC_SERVER: case SMBC_WORKGROUP: psz_server = p_sys->url.psz_host; psz_path = NULL; psz_name = NULL; case SMBC_FILE_SHARE: case SMBC_DIR: i_type = ITEM_TYPE_DIRECTORY; break; case SMBC_FILE: i_type = ITEM_TYPE_FILE; break; default: case SMBC_PRINTER_SHARE: case SMBC_COMMS_SHARE: case SMBC_IPC_SHARE: case SMBC_LINK: continue; } char *psz_encoded_name = NULL; if( psz_name != NULL && ( psz_encoded_name = vlc_uri_encode( psz_name ) ) == NULL ) return NULL; if( smb_get_uri( p_access, &psz_uri, NULL, NULL, NULL, psz_server, psz_path, psz_encoded_name ) < 0 ) { free(psz_encoded_name); return NULL; } free(psz_encoded_name); p_item = input_item_NewExt( psz_uri, p_entry->name, -1, i_type, ITEM_NET ); free( psz_uri ); if( !p_item ) return NULL; } return p_item; }
int main(int argc, char **argv) { if (argc <= 1) { usage(); } smbc_init(samba_auth_data_fn, 0); if (argc==3 && argv[1][0]=='-' && argv[1][1]=='r') { struct in_addr ip; if (samba_resolve_name(argv[2], &ip, 0x20)) { puts(START_OUTPUT); puts(inet_ntoa(ip)); return 0; } else return E_NAMENOTFOUND; } int i; for (i = 1; i <= argc; i++) { if (argv[i][0]=='-' && argv[i][1]=='u') { i++; g_user = argv[i]; } else if (argv[i][0]=='-' && argv[i][1]=='p') { i++; g_pass = argv[i]; } else { g_path = argv[i]; break; } } int dh = smbc_opendir(g_path); if (dh < 0) return errno == EACCES ? E_ACCESS : E_OPENDIRFAILED; puts(START_OUTPUT); struct smbc_dirent *pdirent; while ((pdirent = smbc_readdir(dh)) != NULL) { if (pdirent->name[strlen(pdirent->name)-1] != '$') puts(pdirent->name); } smbc_closedir(dh); return 0; }
static int smbcfs_readdir(const char* const path, void* const buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info* const fi) { const int fh = fi->fh; while(true) { #ifdef MUTEX_LOCK pthread_mutex_lock(&mutex_lock); #endif const smbc_dirent* const dirent = smbc_readdir(fh); #ifdef MUTEX_LOCK pthread_mutex_unlock(&mutex_lock); #endif #ifdef SMBCFS_DEBUG fprintf(stderr, "[smbc_readdir] %d => %p\n" , fh, dirent); #endif if(dirent==NULL) break; const unsigned int smbc_type = dirent->smbc_type; switch(smbc_type) { case SMBC_DIR: case SMBC_FILE: { const int ret_filler = filler(buf, dirent->name, NULL, 0); #ifdef SMBCFS_DEBUG fprintf(stderr, "[fill_dir] %s %s => %d\n", dirent->name, dirent->comment, ret_filler); #endif if(ret_filler!=0) { return -ENOMEM; } break; } default: #ifdef SMBCFS_DEBUG fprintf(stderr, "[dirent not used] %s%s\n", dirent->name, dirent->comment); #endif break; } } return 0; }
static gboolean xmms_samba_browse (xmms_xform_t *xform, const gchar *url, xmms_error_t *error) { struct smbc_dirent *dir; int handle; G_LOCK (mutex); handle = smbc_opendir (url); G_UNLOCK (mutex); if (handle < 0) { xmms_error_set (error, XMMS_ERROR_GENERIC, "Couldn't browse URL"); xmms_log_error ("Couldn't open directory %s!", url); return FALSE; } while (42) { guint32 flags = 0; G_LOCK (mutex); dir = smbc_readdir (handle); if (!dir) { G_UNLOCK (mutex); break; } if (dir->name[0] == '.') { G_UNLOCK (mutex); continue; } if (dir->smbc_type == SMBC_DIR || dir->smbc_type == SMBC_WORKGROUP || dir->smbc_type == SMBC_SERVER || dir->smbc_type == SMBC_FILE_SHARE) flags |= XMMS_XFORM_BROWSE_FLAG_DIR; xmms_xform_browse_add_entry (xform, dir->name, flags); G_UNLOCK (mutex); } G_LOCK (mutex); smbc_closedir (handle); G_UNLOCK (mutex); return TRUE; }
static csync_vio_file_stat_t *_readdir(csync_vio_method_handle_t *dhandle) { struct smbc_dirent *dirent = NULL; smb_dhandle_t *handle = NULL; csync_vio_file_stat_t *file_stat = NULL; handle = (smb_dhandle_t *) dhandle; errno = 0; dirent = smbc_readdir(handle->dh); if (dirent == NULL) { return NULL; } file_stat = c_malloc(sizeof(csync_vio_file_stat_t)); if (file_stat == NULL) { return NULL; } file_stat->name = c_strndup(dirent->name, dirent->namelen); file_stat->fields = CSYNC_VIO_FILE_STAT_FIELDS_NONE; switch (dirent->smbc_type) { case SMBC_FILE_SHARE: file_stat->fields |= CSYNC_VIO_FILE_STAT_FIELDS_TYPE; file_stat->type = CSYNC_VIO_FILE_TYPE_DIRECTORY; break; case SMBC_WORKGROUP: case SMBC_SERVER: case SMBC_COMMS_SHARE: case SMBC_IPC_SHARE: break; case SMBC_DIR: case SMBC_FILE: file_stat->fields |= CSYNC_VIO_FILE_STAT_FIELDS_TYPE; if (dirent->smbc_type == SMBC_DIR) { file_stat->type = CSYNC_VIO_FILE_TYPE_DIRECTORY; } else { file_stat->type = CSYNC_VIO_FILE_TYPE_REGULAR; } break; default: break; } return file_stat; }
int SMB_del(char *url, int index) { SMB_init(index); int res = 0; int dh = 0; dh = smbc_opendir(url); printf("SMB_del() - dh = %d\n", dh); if(dh < 0){ res = smbc_unlink(url); } else{ struct smbc_dirent* dirptr; if(url[strlen(url) - 1] != '/') strcat(url, "/"); while((dirptr = smbc_readdir(dh)) != NULL){ if(!strcmp(dirptr->name, ".") || !strcmp(dirptr->name, "..")) continue; char new_url[255] = {0}; sprintf(new_url, "%s%s", url, dirptr->name); printf("-------%s\n", new_url); if(dirptr->smbc_type == SMBC_DIR) { if(new_url[strlen(new_url) - 1] != '/') strcat(new_url, "/"); SMB_del(new_url, index); } else if(dirptr->smbc_type == SMBC_FILE) { res = smbc_unlink(new_url); } } res = smbc_rmdir(url);//删除成功返回0,失败返回-1 } smbc_closedir(dh); return res; }
/***************************************************************************** * DirRead: *****************************************************************************/ static int DirRead (stream_t *p_access, input_item_node_t *p_node ) { access_sys_t *p_sys = p_access->p_sys; int i_ret = VLC_SUCCESS; struct vlc_readdir_helper rdh; vlc_readdir_helper_init( &rdh, p_access, p_node ); #ifndef _WIN32 struct smbc_dirent *p_entry; while( i_ret == VLC_SUCCESS && ( p_entry = smbc_readdir( p_sys->i_smb ) ) ) { char *psz_uri; const char *psz_server = p_sys->url.psz_host; const char *psz_path = p_sys->url.psz_path; const char *psz_name = p_entry->name; int i_type; switch( p_entry->smbc_type ) { case SMBC_SERVER: case SMBC_WORKGROUP: psz_server = p_sys->url.psz_host; psz_path = NULL; psz_name = NULL; case SMBC_FILE_SHARE: case SMBC_DIR: i_type = ITEM_TYPE_DIRECTORY; break; case SMBC_FILE: i_type = ITEM_TYPE_FILE; break; default: case SMBC_PRINTER_SHARE: case SMBC_COMMS_SHARE: case SMBC_IPC_SHARE: case SMBC_LINK: continue; } char *psz_encoded_name = NULL; if( psz_name != NULL && ( psz_encoded_name = vlc_uri_encode( psz_name ) ) == NULL ) { i_ret = VLC_ENOMEM; break; } if( smb_get_uri( p_access, &psz_uri, NULL, NULL, NULL, psz_server, psz_path, psz_encoded_name ) < 0 ) { free(psz_encoded_name); i_ret = VLC_ENOMEM; break; } free(psz_encoded_name); i_ret = vlc_readdir_helper_additem( &rdh, psz_uri, NULL, p_entry->name, i_type, ITEM_NET ); free( psz_uri ); } #else // Handle share listing from here. Directory browsing is handled by the // usual filesystem module. SHARE_INFO_1 *p_info; DWORD i_share_enum_res; DWORD i_nb_elem; DWORD i_resume_handle = 0; DWORD i_total_elements; // Unused, but needs to be passed wchar_t *wpsz_host = ToWide( p_sys->url.psz_host ); if( wpsz_host == NULL ) return VLC_ENOMEM; do { i_share_enum_res = NetShareEnum( wpsz_host, 1, (LPBYTE*)&p_info, MAX_PREFERRED_LENGTH, &i_nb_elem, &i_total_elements, &i_resume_handle ); if( i_share_enum_res == ERROR_SUCCESS || i_share_enum_res == ERROR_MORE_DATA ) { for ( DWORD i = 0; i < i_nb_elem; ++i ) { SHARE_INFO_1 *p_current = p_info + i; if( p_current->shi1_type & STYPE_SPECIAL ) continue; char* psz_name = FromWide( p_current->shi1_netname ); if( psz_name == NULL ) { i_ret = VLC_ENOMEM; break; } char* psz_path; if( smb_get_uri( p_access, &psz_path, NULL, NULL, NULL, p_sys->url.psz_host, p_sys->url.psz_path, psz_name ) < 0 ) { free( psz_name ); i_ret = VLC_ENOMEM; break; } free( psz_name ); // We need to concatenate the scheme before, as the window version // of smb_get_uri generates a path (and the other call site needs // a path). The path is already prefixed by "//" so we just need // to add "file:" char* psz_uri; if( asprintf( &psz_uri, "file:%s", psz_path ) < 0 ) { free( psz_path ); i_ret = VLC_ENOMEM; break; } free( psz_path ); i_ret = vlc_readdir_helper_additem( &rdh, psz_uri, NULL, psz_name, ITEM_TYPE_DIRECTORY, ITEM_NET ); free( psz_uri ); } } NetApiBufferFree( p_info ); } while( i_share_enum_res == ERROR_MORE_DATA && i_ret == VLC_SUCCESS ); free( wpsz_host ); #endif vlc_readdir_helper_finish( &rdh, i_ret == VLC_SUCCESS ); return i_ret; }
int browse(int (*scan_found_share)(char share[]),char * path, int scan, int indent) { char * p; char buf[1024]; int dir; struct stat stat; struct smbc_dirent * dirent; if (! scan) { bblog(INFO, "Opening (%s)...", path); } if ((dir = smbc_opendir(path)) < 0) { bblog(ERROR, "Could not open directory [%s] (%d:%s)", path, errno, strerror(errno)); return 0; } while ((dirent = smbc_readdir(dir)) != NULL) { bblog(INFO, "%*.*s%-30s", indent, indent, "", dirent->name); switch(dirent->smbc_type) { case SMBC_WORKGROUP: bblog(INFO, "WORKGROUP"); break; case SMBC_SERVER: bblog(INFO, "SERVER"); break; case SMBC_FILE_SHARE: bblog(INFO, "FILE_SHARE"); (*scan_found_share)(dirent->name); break; case SMBC_PRINTER_SHARE: bblog(INFO, "PRINTER_SHARE"); break; case SMBC_COMMS_SHARE: bblog(INFO, "COMMS_SHARE"); break; case SMBC_IPC_SHARE: bblog(INFO, "IPC_SHARE"); break; case SMBC_DIR: bblog(INFO, "DIR"); break; case SMBC_FILE: bblog(INFO, "FILE"); p = path + strlen(path); strcat(p, "/"); strcat(p+1, dirent->name); if (smbc_stat(path, &stat) < 0) { bblog(WARN, " unknown size (reason %d: %s)", errno, strerror(errno)); } else { bblog(INFO, " size %lu", (unsigned long) stat.st_size); } *p = '\0'; break; case SMBC_LINK: bblog(INFO, "LINK"); break; } if (scan && (dirent->smbc_type == SMBC_WORKGROUP || dirent->smbc_type == SMBC_SERVER)) { /* * don't append server name to workgroup; what we want is: * * smb://workgroup_name * or * smb://server_name * */ snprintf(buf, sizeof(buf), "smb://%s", dirent->name); browse(scan_found_share,buf, scan, indent + 2); } } smbc_closedir(dir); return 1; }
int smb_download_dir(const char *base, const char *name, int resume) { char path[SMB_MAXPATHLEN]; int dirhandle; struct smbc_dirent *dirent; const char *relname = name; char *tmpname; struct stat remotestat; snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (base[0] && name[0] && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name); /* List files in directory and call smb_download_file on them */ dirhandle = smbc_opendir(path); if(dirhandle < 1) { if(errno == ENOTDIR) return smb_download_file(base, name, 1, resume, NULL); fprintf(stderr, "Can't open directory %s: %s\n", path, strerror(errno)); return 0; } while(*relname == '/')relname++; mkdir(relname, 0755); tmpname = strdup(name); while((dirent = smbc_readdir(dirhandle))) { char *newname; if(!strcmp(dirent->name, ".") || !strcmp(dirent->name, ".."))continue; asprintf(&newname, "%s/%s", tmpname, dirent->name); switch(dirent->smbc_type) { case SMBC_DIR: smb_download_dir(base, newname, resume); break; case SMBC_WORKGROUP: smb_download_dir("smb://", dirent->name, resume); break; case SMBC_SERVER: smb_download_dir("smb://", dirent->name, resume); break; case SMBC_FILE: smb_download_file(base, newname, 1, resume, NULL); break; case SMBC_FILE_SHARE: smb_download_dir(base, newname, resume); break; case SMBC_PRINTER_SHARE: if(!quiet)printf("Ignoring printer share %s\n", dirent->name); break; case SMBC_COMMS_SHARE: if(!quiet)printf("Ignoring comms share %s\n", dirent->name); break; case SMBC_IPC_SHARE: if(!quiet)printf("Ignoring ipc$ share %s\n", dirent->name); break; default: fprintf(stderr, "Ignoring file '%s' of type '%d'\n", newname, dirent->smbc_type); break; } free(newname); } free(tmpname); if(keep_permissions) { if(smbc_fstat(dirhandle, &remotestat) < 0) { fprintf(stderr, "Unable to get stats on %s on remote server\n", path); smbc_closedir(dirhandle); return 0; } if(chmod(relname, remotestat.st_mode) < 0) { fprintf(stderr, "Unable to change mode of local dir %s to %o\n", relname, remotestat.st_mode); smbc_closedir(dirhandle); return 0; } } smbc_closedir(dirhandle); return 1; }
int DirGetContents( const char *path, const char *filter, fileInfo_t *fileInfo, int maxItems ) { int c, i; char *ptr; int numRead; int index = 0; if( !path || !fileInfo || !maxItems ) return -1; if( (ptr = strchr( path, '/' )) == NULL ) { printf("DirGetContents : Invalid Path (ptr = NULL)\n"); return -1; } c = ptr - path; // try to read in dir from cd/dvd if( !strncmp( path, "cdfs:/", c ) ) { // just make sure we are initialized CD_Init(); struct TocEntry *tocEntries = (struct TocEntry*) malloc( sizeof(struct TocEntry) * maxItems ); if( !tocEntries ) return -1; CDVD_FlushCache(); numRead = CDVD_GetDir( ptr, NULL, CDVD_GET_FILES_AND_DIRS, tocEntries, maxItems, NULL ); CDVD_Stop(); index = 0; ptr = NULL; if( filter ) ptr = (char*) malloc( strlen(filter) + 1 ); for( i = 0; i < numRead; i++ ) { if( index >= maxItems ) break; if( !strcmp( tocEntries[i].filename, ".." ) || !strcmp( tocEntries[i].filename, "." ) ) continue; // check for filters c = 1; if( filter && !(tocEntries[i].fileProperties & FLAG_DIRECTORY) ) { strcpy( ptr, filter ); c = 0; char *token = strtok( ptr, " " ); while( token ) { // found matching extension if( CmpFileExtension( tocEntries[i].filename, token ) ) { c = 1; break; } token = strtok( NULL, " " ); } } if( c == 1 ) { strncpy( fileInfo[index].name, tocEntries[i].filename, sizeof(fileInfo[index].name) ); fileInfo[index].size = tocEntries[i].fileSize; fileInfo[index].flags = tocEntries[i].fileProperties; index++; } } if( ptr ) free(ptr); free(tocEntries); } else if( !strncmp( path, "pfs", 3 ) ) { // try to read in dir from hdd int hDir = fileXioDopen( path ); int nRet; iox_dirent_t dirEntry; if( hDir < 0 ) return -1; index = 0; ptr = NULL; if( filter ) ptr = (char*) malloc( strlen(filter) + 1 ); do { if( !(nRet = fileXioDread( hDir, &dirEntry )) ) break; if(!strcmp( dirEntry.name, "." ) || !strcmp( dirEntry.name, "..")) continue; if( index >= maxItems ) break; if( FIO_S_ISDIR(dirEntry.stat.mode) ) fileInfo[index].flags = FLAG_DIRECTORY; else fileInfo[index].flags = 0; // check for filters c = 1; if( filter && !(fileInfo[index].flags & FLAG_DIRECTORY) ) { strcpy( ptr, filter ); c = 0; char *token = strtok( ptr, " " ); while( token ) { // found matching extension if( CmpFileExtension( dirEntry.name, token ) ) { c = 1; break; } token = strtok( NULL, " " ); } } if( c == 1 ) { strncpy( fileInfo[index].name, dirEntry.name, sizeof(fileInfo[index].name) ); fileInfo[index].size = dirEntry.stat.size; index++; } } while( nRet > 0 ); if( ptr ) free(ptr); fileXioDclose( hDir ); } else if( !strncmp( path, "mc0:/", c ) || !strncmp( path, "mc1:/", c ) ) { // try to read in dir from memory card int nPort; char mcPath[256]; mcTable mcEntries[MAX_DIR_FILES] __attribute__((aligned(64))); if( !strncmp( path, "mc0:/", c ) ) nPort = 0; else nPort = 1; strcpy( mcPath, ptr ); strcat( mcPath, "*" ); mcGetDir( nPort, 0, mcPath, 0, MAX_DIR_FILES, mcEntries ); mcSync( 0, NULL, &numRead ); index = 0; ptr = NULL; if( filter ) ptr = (char*) malloc( strlen(filter) + 1 ); for( i = 0; i < numRead; i++ ) { if( index >= maxItems ) break; if( !strcmp( mcEntries[i].name, "." ) || !strcmp( mcEntries[i].name, "..") ) continue; if( mcEntries[i].attrFile & MC_ATTR_SUBDIR ) fileInfo[index].flags = FLAG_DIRECTORY; else fileInfo[index].flags = 0; // check for filters c = 1; if( filter && !(mcEntries[i].attrFile & MC_ATTR_SUBDIR) ) { strcpy( ptr, filter ); c = 0; char *token = strtok( ptr, " " ); while( token ) { // found matching extension if( CmpFileExtension( mcEntries[i].name, token ) ) { c = 1; break; } token = strtok( NULL, " " ); } } if( c == 1 ) { strncpy( fileInfo[index].name, mcEntries[i].name, sizeof(fileInfo[index].name) ); fileInfo[index].size = mcEntries[i].fileSizeByte; index++; } } if( ptr ) free(ptr); } else if( !strncmp( path, "mass:/", c ) ) { // try to read in dir from USB device int nRet; fat_dir_record dirEntry; // returns number of entries in directory nRet = usb_mass_getFirstDirentry( ptr, &dirEntry ); index = 0; ptr = NULL; if( filter ) ptr = (char*) malloc( strlen(filter) + 1 ); // loop through all entries in directory while( nRet > 0 ) { if(!strcmp( dirEntry.name, "." ) || !strcmp( dirEntry.name, "..")) { nRet = usb_mass_getNextDirentry(&dirEntry); continue; } // ignore volume label if( dirEntry.attr & USB_VOLUME ) { nRet = usb_mass_getNextDirentry(&dirEntry); continue; } if( index >= maxItems ) break; if( dirEntry.attr & USB_DIRECTORY ) fileInfo[index].flags = FLAG_DIRECTORY; else fileInfo[index].flags = 0; // check for filters c = 1; if( filter && !(fileInfo[index].flags & FLAG_DIRECTORY) ) { strcpy( ptr, filter ); c = 0; char *token = strtok( ptr, " " ); while( token ) { // found matching extension if( CmpFileExtension( dirEntry.name, token ) ) { c = 1; break; } token = strtok( NULL, " " ); } } if( c == 1 ) { strncpy( fileInfo[index].name, dirEntry.name, sizeof(fileInfo[index].name) ); fileInfo[index].size = dirEntry.size; index++; } nRet = usb_mass_getNextDirentry( &dirEntry ); } if( ptr ) free(ptr); } else if( !strncmp( path, "smb", 3 ) ) { // read from a samba share int hDir = smbc_opendir( path ); const struct smbc_dirent *dirEntry; if( hDir < 0 ) return -1; index = 0; ptr = NULL; if( filter ) ptr = (char*) malloc( strlen(filter) + 1 ); while( (dirEntry = smbc_readdir( hDir )) != NULL ) { if(!strcmp( dirEntry->name, "." ) || !strcmp( dirEntry->name, "..")) continue; if( index >= maxItems ) break; if( dirEntry->smbc_type == SMBC_DIR ) fileInfo[index].flags = FLAG_DIRECTORY; else fileInfo[index].flags = 0; // check for filters c = 1; if( filter && !(fileInfo[index].flags & FLAG_DIRECTORY) ) { strcpy( ptr, filter ); c = 0; char *token = strtok( ptr, " " ); while( token ) { // found matching extension if( CmpFileExtension( dirEntry->name, token ) ) { c = 1; break; } token = strtok( NULL, " " ); } } if( c == 1 ) { strncpy( fileInfo[index].name, dirEntry->name, sizeof(fileInfo[index].name) ); fileInfo[index].size = 0; // fixme index++; } }; if( ptr ) free(ptr); smbc_closedir( hDir ); } return index; }
int main(int argc, char** argv) { int err = -1; int fd = 0; int dh = 0; int entry_num = 0; int i = 0; int offset = 0; int dirsize = 0; char *file_name; char *tmp_file_ptr; struct smbc_dirent *dirptr; char buff[MAX_BUFF_SIZE]; char url[MAX_BUFF_SIZE]; char file_url[MAX_BUFF_SIZE]; char dir_url[MAX_BUFF_SIZE]; char dirbuff[MAX_BUFF_SIZE]; memset(g_workgroup, '\0', MAX_BUFF_SIZE); memset(url, '\0', MAX_BUFF_SIZE); memset(file_url, '\0', MAX_BUFF_SIZE); memset(dir_url, '\0', MAX_BUFF_SIZE); memset(buff, '\0', MAX_BUFF_SIZE); if ( argc == 6 ) { dirptr = (struct smbc_dirent *) dirbuff; strncpy(g_workgroup,argv[1],strlen(argv[1])); strncpy(g_username,argv[2],strlen(argv[2])); strncpy(g_password,argv[3],strlen(argv[3])); strncpy(url,argv[4],strlen(argv[4])); smbc_init(auth_fn, 0); strncpy(file_url,"tempfile-",9); tmp_file_ptr = file_url; tmp_file_ptr += 9; smbc_rmdir(url); smbc_mkdir(url,0666); entry_num = atoi(argv[5]); strcat(dir_url,url); strcat(dir_url,"/"); file_name = dir_url; file_name += strlen(dir_url); for ( i = 0; i < entry_num; i++ ) { sprintf(buff,"%d",i); memcpy(tmp_file_ptr,buff,strlen(buff)+4); strncat(tmp_file_ptr,".txt",4); strcpy(file_name,file_url); fd = smbc_open(dir_url,O_RDWR | O_CREAT, 0666); smbc_close(fd); } dh = smbc_opendir(url); err = 0; while ( 1 ) { dirptr = smbc_readdir(dh); if ( dirptr == NULL ) { break; } dirsize += dirptr->dirlen; } offset = smbc_telldir(dh); if ( offset != dirsize ) { /* printf("offset: %i dirsize: %i\n", offset, dirsize); */ err = 1; } } return err; }
int FSSmb::ReadDir( FSList* list, FSPath& _path, int* err, FSCInfo* info ) { FREPARE_SMB_OPER( lock, info, &_param ); if ( info && info->Stopped() ) { return -2; } list->Clear(); FSPath path( _path ); int d = smbc_opendir( pathBuffer1.SetPath( path ) ); if ( d < 0 ) { SetError( err, errno ); return -1; } try { struct smbc_dirent* pEnt; int n = path.Count(); while ( true ) { if ( info && info->Stopped() ) { smbc_closedir( d ); return -2; } pEnt = smbc_readdir( d ); if ( !pEnt ) { //??? break; } //skip . and .. if ( pEnt->name[0] == '.' && ( !pEnt->name[1] || ( pEnt->name[1] == '.' && !pEnt->name[2] ) ) ) { continue; } if ( //ignore it pEnt->smbc_type == SMBC_PRINTER_SHARE || pEnt->smbc_type == SMBC_IPC_SHARE ) { continue; } clPtr<FSNode> pNode = new FSNode(); path.SetItem( n, CS_UTF8, pEnt->name ); switch ( pEnt->smbc_type ) { case SMBC_WORKGROUP: pNode->extType = FSNode::WORKGROUP; pNode->st.mode = S_IFDIR; break; case SMBC_SERVER: pNode->extType = FSNode::SERVER; pNode->st.mode = S_IFDIR; break; case SMBC_FILE_SHARE: pNode->extType = FSNode::FILESHARE; pNode->st.mode = S_IFDIR; break; case SMBC_PRINTER_SHARE: case SMBC_COMMS_SHARE: case SMBC_IPC_SHARE: case SMBC_DIR: pNode->st.mode = S_IFDIR; break; default: InternalStat( path, &pNode->st, info ); } pNode->name.Set( sys_charset_id, pEnt->name ); list->Append( pNode ); }; smbc_closedir( d ); return 0; err: SetError( err, errno ); smbc_closedir( d ); return -1; } catch ( ... ) { smbc_closedir( d ); throw; } }
static void browse(char * path, int scan, int indent) { char * p; char buf[1024]; int dir; struct stat stat; struct smbc_dirent * dirent; if (! scan) { printf("Opening (%s)...\n", path); } if ((dir = smbc_opendir(path)) < 0) { printf("Could not open directory [%s] (%d:%s)\n", path, errno, strerror(errno)); return; } while ((dirent = smbc_readdir(dir)) != NULL) { printf("%*.*s%-30s", indent, indent, "", dirent->name); switch(dirent->smbc_type) { case SMBC_WORKGROUP: printf("WORKGROUP"); break; case SMBC_SERVER: printf("SERVER"); break; case SMBC_FILE_SHARE: printf("FILE_SHARE"); break; case SMBC_PRINTER_SHARE: printf("PRINTER_SHARE"); break; case SMBC_COMMS_SHARE: printf("COMMS_SHARE"); break; case SMBC_IPC_SHARE: printf("IPC_SHARE"); break; case SMBC_DIR: printf("DIR"); break; case SMBC_FILE: printf("FILE"); p = path + strlen(path); strcat(p, "/"); strcat(p+1, dirent->name); if (smbc_stat(path, &stat) < 0) { printf(" unknown size (reason %d: %s)", errno, strerror(errno)); } else { printf(" size %lu", (unsigned int) stat.st_size); } *p = '\0'; break; case SMBC_LINK: printf("LINK"); break; } printf("\n"); if (scan && (dirent->smbc_type == SMBC_WORKGROUP || dirent->smbc_type == SMBC_SERVER)) { /* * don't append server name to workgroup; what we want is: * * smb://workgroup_name * or * smb://server_name * */ snprintf(buf, sizeof(buf), "smb://%s", dirent->name); browse(buf, scan, indent + 2); } } smbc_closedir(dir); }
int main(int argc, char** argv) { int err = -1; int fd = 0; int dh = 0; int entry_num = 0; int i = 0; int j = 0; char *file_name; char *tmp_file_ptr; struct smbc_dirent *dirptr; char buff[MAX_BUFF_SIZE]; char url[MAX_BUFF_SIZE]; char file_url[MAX_BUFF_SIZE]; char dir_url[MAX_BUFF_SIZE]; char dirbuff[MAX_BUFF_SIZE]; bzero(g_workgroup,MAX_BUFF_SIZE); bzero(url,MAX_BUFF_SIZE); bzero(file_url,MAX_BUFF_SIZE); bzero(dir_url,MAX_BUFF_SIZE); bzero(buff,MAX_BUFF_SIZE); if ( argc == 6 ) { dirptr = (struct smbc_dirent *) dirbuff; strncpy(g_workgroup,argv[1],strlen(argv[1])); strncpy(g_username,argv[2],strlen(argv[2])); strncpy(g_password,argv[3],strlen(argv[3])); strncpy(url,argv[4],strlen(argv[4])); smbc_init(auth_fn, 0); strncpy(file_url,"tempfile-",9); tmp_file_ptr = file_url; tmp_file_ptr += 9; smbc_rmdir(url); smbc_mkdir(url,0666); entry_num = atoi(argv[5]); strcat(dir_url,url); strcat(dir_url,"/"); file_name = dir_url; file_name += strlen(dir_url); for ( i = 0; i < entry_num; i++ ) { sprintf(buff,"%d",i); memcpy(tmp_file_ptr,buff,strlen(buff)+4); strncat(tmp_file_ptr,".txt",4); strcpy(file_name,file_url); fd = smbc_open(dir_url,O_RDWR | O_CREAT, 0666); smbc_close(fd); } dh = smbc_opendir(url); err = 0; i = 0; bzero(buff,MAX_BUFF_SIZE); bzero(tmp_file_ptr,MAX_BUFF_SIZE-9); while ( 1 ) { dirptr = smbc_readdir( dh ); if ( dirptr == NULL ) { break; } /* printf("Name: %s\n",dirptr->name); */ if ( j == 0 ) { if ( !(( strncmp(dirptr->name,".",1) == 0 )) ) { break; err = 1; } } else if ( j == 1 ) { if ( !(( strncmp(dirptr->name,"..",2) == 0 )) ) { break; err = 1; } } else if ( j > 1 ) { sprintf(buff,"%d",i); memcpy(tmp_file_ptr,buff,strlen(buff)+4); strncat(tmp_file_ptr,".txt",4); if ( !(( strcmp(dirptr->name,file_url) == 0 )) ) /* make sure entries match */ { err = 1; break; } i++; } j++; } if ( ! err ) { if ( (j - 2) != entry_num ) /* Make sure that all entries created are counted and returned - minus . and .. */ err = 1; } } return err; }
vector<string> FileBrowser::GetFolderList() { vector<string> retVal; if(m_CurrentFolder.size()==0) { //List Drives retVal.push_back("Game:"); if (SETTINGS::getInstance().getSambaClientOn()) retVal.push_back("smb:"); std::vector<Drive* const> mountedDrives; DrivesManager::getInstance().getMountedDrives(&mountedDrives); for(unsigned int x=0;x<mountedDrives.size();x++) { retVal.push_back(mountedDrives[x]->GetCleanDriveName() + ":"); } return retVal; } else { retVal.push_back(".."); string path = GetCurrentPath(); if (path.substr(0,4).compare("smb:") == 0) { map<string, unsigned int> m_SambaTypes; int dir; if (path.length() == 5) { if (SambaClient::getInstance().m_SambaTopLevelTypes.size() > 0) { for (map<string, unsigned int>::const_iterator it = SambaClient::getInstance().m_SambaTopLevelTypes.begin(); it != SambaClient::getInstance().m_SambaTopLevelTypes.end(); ++it) { retVal.push_back(it->first); } return retVal; } } path.append("/"); if ((dir = smbc_opendir(path.c_str())) < 0) { DebugMsg("FileBrowser", "Could not open directory [%s] (%d:%s)\n", path.c_str(), errno, strerror(errno)); if (errno == 13) { // permission denied string user; string password; string share; string rawpath = GetRawSmbPath(user, password, share); XUIMessage xuiMsg; XuiMessage(&xuiMsg, XM_SMB_PERMISSION_DENIED); Credentials * cred = new Credentials(); _XuiMessageExtra(&xuiMsg,(XUIMessageData*) cred, sizeof(*cred)); cred->smbPath = rawpath; cred->user = user; cred->password = password; cred->share = share; XuiSendMessage( parentScene , &xuiMsg ); } return retVal; } struct smbc_dirent * dirent; while ((dirent = smbc_readdir(dir)) != NULL) { if (dirent->smbc_type != SMBC_WORKGROUP && dirent->smbc_type != SMBC_SERVER && dirent->smbc_type != SMBC_FILE_SHARE && dirent->smbc_type != SMBC_DIR) continue; if (strcmp(dirent->name, ".") == 0 || strcmp(dirent->name, "..") == 0) continue; if (hasEnding(dirent->name, "$")) continue; if (m_CurrentFolder.size() == 1) { SambaClient::getInstance().m_SambaTopLevelTypes[dirent->name] = dirent->smbc_type; } // filter ourselves out of the list of servers if (SETTINGS::getInstance().getSambaServerOn() && CFreestyleApp::getInstance().hasInternetConnection()) { if (m_CurrentFolder.size() == 2) { string wrkgroup = SETTINGS::getInstance().getSambaClientWorkgroup(); if (stricmp(wrkgroup.c_str(), m_CurrentFolder[1].c_str()) == 0) { string hostname = SETTINGS::getInstance().getHostname(); if (stricmp(hostname.c_str(), dirent->name) == 0) continue; } } } retVal.push_back(dirent->name); } // if (m_CurrentFolder.size() == 1) { // m_SambaTopLevelTypes = m_SambaTypes; // //m_CurrentFolderSambaTypes.push_back(m_SambaTypes); // } smbc_closedir(dir); return retVal; } WIN32_FIND_DATA findFileData; memset(&findFileData,0,sizeof(WIN32_FIND_DATA)); string searchcmd = GetCurrentPath() + "\\*"; HANDLE hFind = FindFirstFile(searchcmd.c_str(), &findFileData); if (hFind == INVALID_HANDLE_VALUE) return retVal; do { string s = findFileData.cFileName; if(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { retVal.push_back(s); } } while (FindNextFile(hFind, &findFileData)); FindClose(hFind); return retVal; } }
vector<string> FileBrowser::GetFileList() { DebugMsg("FileBrowser", "In Get FileList"); vector<string> retVal; if(m_CurrentFolder.size()>0) { string path = GetCurrentPath(); if (path.substr(0,4).compare("smb:") == 0) { map<string, unsigned int> m_SambaTypes; if (path.length() == 5) return retVal; // there are no "FILES" at the top level path.append("/"); int dir; DebugMsg("FileBrowser", "calling opendir on %s", path.c_str()); if ((dir = smbc_opendir(path.c_str())) < 0) { DebugMsg("FileBrowser", "Could not open directory [%s] (%d:%s)\n", path.c_str(), errno, strerror(errno)); if (errno == 13) { retVal.push_back("*Permission Denied*"); } return retVal; } struct smbc_dirent *dirent; DebugMsg("FileBrowser", "calling smbc_readdir on %s", path.c_str()); while ((dirent = smbc_readdir(dir)) != NULL) { if (dirent->smbc_type != SMBC_FILE) continue; if (strcmp(dirent->name, ".") == 0 || strcmp(dirent->name, "..") == 0) continue; // DebugMsg("FileBrowser","Inserting %s as type %d", dirent->name, dirent->smbc_type); // m_SambaTypes[dirent->name] = dirent->smbc_type; retVal.push_back(dirent->name); } smbc_closedir(dir); // if (m_CurrentFolderSambaTypes.size() < m_CurrentFolder.size()) { // m_CurrentFolderSambaTypes.push_back(m_SambaTypes); // } return retVal; } WIN32_FIND_DATA findFileData; memset(&findFileData,0,sizeof(WIN32_FIND_DATA)); string searchcmd = GetCurrentPath() + "\\*"; HANDLE hFind = FindFirstFile(searchcmd.c_str(), &findFileData); if (hFind == INVALID_HANDLE_VALUE) return retVal; do { string s = findFileData.cFileName; if(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { } else { if(strcmp(s.c_str(),"..")!=0) { retVal.push_back(s); } } } while (FindNextFile(hFind, &findFileData)); FindClose(hFind); } return retVal; }
int main(int argc, char** argv) { int err = -1; int fd = 0; int dh = 0; int entry_num = 0; int i = 0; char *file_name; char *tmp_file_ptr; struct smbc_dirent *dirptr; char buff[MAX_BUFF_SIZE]; char url[MAX_BUFF_SIZE]; char file_url[MAX_BUFF_SIZE]; char dir_url[MAX_BUFF_SIZE]; char dirbuff[MAX_BUFF_SIZE]; bzero(g_workgroup,MAX_BUFF_SIZE); bzero(url,MAX_BUFF_SIZE); bzero(file_url,MAX_BUFF_SIZE); bzero(dir_url,MAX_BUFF_SIZE); bzero(buff,MAX_BUFF_SIZE); if ( argc == 6 ) { dirptr = (struct smbc_dirent *) dirbuff; strncpy(g_workgroup,argv[1],strlen(argv[1])); strncpy(g_username,argv[2],strlen(argv[2])); strncpy(g_password,argv[3],strlen(argv[3])); strncpy(url,argv[4],strlen(argv[4])); smbc_init(auth_fn, 0); strncpy(file_url,"tempfile-",9); tmp_file_ptr = file_url; tmp_file_ptr += 9; smbc_rmdir(url); smbc_mkdir(url,0666); entry_num = atoi(argv[5]); strcat(dir_url,url); strcat(dir_url,"/"); file_name = dir_url; file_name += strlen(dir_url); for ( i = 0; i < entry_num; i++ ) { sprintf(buff,"%d",i); memcpy(tmp_file_ptr,buff,strlen(buff)+4); strncat(tmp_file_ptr,".txt",4); strcpy(file_name,file_url); fd = smbc_open(dir_url,O_RDWR | O_CREAT, 0666); smbc_close(fd); } dh = smbc_opendir(url); dirptr = smbc_readdir( dh ); if ( dirptr == NULL ) err = 1; else err = 0; } return err; }