X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;ds=sidebyside;f=modules%2Faccess%2Fdirectory.c;h=b778cf0e252ff37cac3c064a0a556c3092462a77;hb=f659703fb4033420e0607d34bc7cd880a5802ea7;hp=2e1d75a6453b1211e9f826d0568effbd82efd588;hpb=a6d927b07acefa4eb7cc2b936e1d76d7416aee98;p=vlc diff --git a/modules/access/directory.c b/modules/access/directory.c index 2e1d75a645..b778cf0e25 100644 --- a/modules/access/directory.c +++ b/modules/access/directory.c @@ -41,13 +41,11 @@ #ifdef HAVE_UNISTD_H # include +# include #elif defined( WIN32 ) && !defined( UNDER_CE ) # include #endif -#ifdef HAVE_DIRENT_H -# include -#endif #ifdef __sun__ static inline int dirfd (DIR *dir) { @@ -55,7 +53,7 @@ static inline int dirfd (DIR *dir) } #endif -#include +#include #include #include @@ -75,13 +73,16 @@ struct directory_t #ifndef WIN32 struct stat st; #endif - char path[1]; +#ifndef HAVE_OPENAT + char *path; +#endif }; struct access_sys_t { directory_t *current; DIR *handle; + char *uri; char *ignored_exts; int mode; int i_item_count; @@ -95,10 +96,10 @@ int DirOpen( vlc_object_t *p_this ) { access_t *p_access = (access_t*)p_this; - if( !p_access->psz_path ) + if( !p_access->psz_filepath ) return VLC_EGENERIC; - DIR *handle = utf8_opendir (p_access->psz_path); + DIR *handle = vlc_opendir (p_access->psz_filepath); if (handle == NULL) return VLC_EGENERIC; @@ -108,22 +109,31 @@ int DirOpen( vlc_object_t *p_this ) int DirInit (access_t *p_access, DIR *handle) { access_sys_t *p_sys = malloc (sizeof (*p_sys)); - if (!p_sys) + if (unlikely(p_sys == NULL)) + goto error; + + char *uri; + if (!strcmp (p_access->psz_access, "fd")) { - closedir( handle ); - return VLC_ENOMEM; + if (asprintf (&uri, "fd://%s", p_access->psz_location) == -1) + uri = NULL; } + else + uri = make_URI (p_access->psz_filepath, "file"); + if (unlikely(uri == NULL)) + goto error; p_access->p_sys = p_sys; p_sys->current = NULL; p_sys->handle = handle; - p_sys->ignored_exts = var_CreateGetString (p_access, "ignore-filetypes"); + p_sys->uri = uri; + p_sys->ignored_exts = var_InheritString (p_access, "ignore-filetypes"); p_sys->i_item_count = 0; p_sys->psz_xspf_extension = strdup( "" ); /* Handle mode */ - char *psz = var_CreateGetString( p_access, "recursive" ); - if( *psz == '\0' || !strcasecmp( psz, "none" ) ) + char *psz = var_InheritString (p_access, "recursive"); + if (psz == NULL || !strcasecmp (psz, "none")) p_sys->mode = MODE_NONE; else if( !strcasecmp( psz, "collapse" ) ) p_sys->mode = MODE_COLLAPSE; @@ -140,6 +150,11 @@ int DirInit (access_t *p_access, DIR *handle) p_access->psz_demux = strdup ("xspf-open"); return VLC_SUCCESS; + +error: + closedir (handle); + free (p_sys); + return VLC_EGENERIC; } /***************************************************************************** @@ -157,10 +172,17 @@ void DirClose( vlc_object_t * p_this ) p_sys->current = current->parent; closedir (current->handle); free (current->uri); +#ifndef HAVE_OPENAT + free (current->path); +#endif free (current); } + + /* corner case: Block() not called ever */ if (p_sys->handle != NULL) - closedir (p_sys->handle); /* corner case,:Block() not called ever */ + closedir (p_sys->handle); + free (p_sys->uri); + free (p_sys->psz_xspf_extension); free (p_sys->ignored_exts); free (p_sys); @@ -204,7 +226,7 @@ block_t *DirBlock (access_t *p_access) memcpy (block->p_buffer, header, sizeof (header) - 1); /* "Open" the base directory */ - current = malloc (sizeof (*current) + strlen (p_access->psz_path)); + current = malloc (sizeof (*current)); if (current == NULL) { block_Release (block); @@ -212,28 +234,32 @@ block_t *DirBlock (access_t *p_access) } current->parent = NULL; current->handle = p_sys->handle; - strcpy (current->path, p_access->psz_path); - current->uri = make_URI (current->path); - if ((current->uri == NULL) - || fstat (dirfd (current->handle), ¤t->st)) +#ifndef HAVE_OPENAT + current->path = strdup (p_access->psz_filepath); +#endif + current->uri = p_sys->uri; + if (fstat (dirfd (current->handle), ¤t->st)) { - free (current->uri); free (current); block_Release (block); goto fatal; } p_sys->handle = NULL; + p_sys->uri = NULL; p_sys->current = current; return block; } - char *entry = utf8_readdir (current->handle); + char *entry = vlc_readdir (current->handle); if (entry == NULL) { /* End of directory, go back to parent */ closedir (current->handle); p_sys->current = current->parent; free (current->uri); +#ifndef HAVE_OPENAT + free (current->path); +#endif free (current); if (p_sys->current == NULL) @@ -244,14 +270,12 @@ block_t *DirBlock (access_t *p_access) "%s" \ " \n" \ "\n", p_sys->psz_xspf_extension ); - if( len < 0 ) + if (unlikely(len == -1)) goto fatal; - block_t *block = block_Alloc ( len ); - if (!block) - goto fatal; - memcpy (block->p_buffer, footer, len); - free( footer ); + block_t *block = block_heap_Alloc (footer, footer, len); + if (unlikely(block == NULL)) + free (footer); p_access->info.b_eof = true; return block; } @@ -280,16 +304,30 @@ block_t *DirBlock (access_t *p_access) /* Handle recursion */ if (p_sys->mode != MODE_COLLAPSE) { - directory_t *sub = malloc (sizeof (*sub) + strlen (current->path) + 1 - + strlen (entry)); + directory_t *sub = malloc (sizeof (*sub)); if (sub == NULL) { free (entry); return NULL; } - sprintf (sub->path, "%s/%s", current->path, entry); - DIR *handle = utf8_opendir (sub->path); + DIR *handle; +#ifdef HAVE_OPENAT + int fd = vlc_openat (dirfd (current->handle), entry, O_RDONLY); + if (fd != -1) + { + handle = fdopendir (fd); + if (handle == NULL) + close (fd); + } + else + handle = NULL; +#else + if (asprintf (&sub->path, "%s/%s", current->path, entry) != -1) + handle = vlc_opendir (sub->path); + else + handle = NULL; +#endif if (handle != NULL) { sub->parent = current; @@ -393,15 +431,12 @@ block_t *DirBlock (access_t *p_access) goto fatal; free( old_xspf_extension ); - /* TODO: new block allocator for malloc()ated data */ - block_t *block = block_Alloc (len); - if (!block) + block_t *block = block_heap_Alloc (entry, entry, len); + if (unlikely(block == NULL)) { free (entry); goto fatal; } - memcpy (block->p_buffer, entry, len); - free (entry); return block; fatal: