Commit db19221d authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

directory: cleanup, avoid useless allocs

parent 18d1dcb4
...@@ -70,10 +70,10 @@ struct directory_t ...@@ -70,10 +70,10 @@ struct directory_t
directory_t *parent; directory_t *parent;
DIR *handle; DIR *handle;
char *uri; char *uri;
#ifndef WIN32 #ifdef HAVE_OPENAT
struct stat st; dev_t device;
#endif ino_t inode;
#ifndef HAVE_OPENAT #else
char *path; char *path;
#endif #endif
}; };
...@@ -85,7 +85,7 @@ struct access_sys_t ...@@ -85,7 +85,7 @@ struct access_sys_t
char mode; char mode;
bool header; bool header;
int i_item_count; int i_item_count;
char *psz_xspf_extension; char *xspf_ext;
}; };
/***************************************************************************** /*****************************************************************************
...@@ -132,22 +132,26 @@ int DirInit (access_t *p_access, DIR *handle) ...@@ -132,22 +132,26 @@ int DirInit (access_t *p_access, DIR *handle)
root->parent = NULL; root->parent = NULL;
root->handle = handle; root->handle = handle;
root->uri = uri; root->uri = uri;
#ifndef HAVE_OPENAT #ifdef HAVE_OPENAT
root->path = strdup (p_access->psz_filepath); struct stat st;
#endif if (fstat (dirfd (handle), &st))
if (fstat (dirfd (handle), &root->st))
{ {
free (root); free (root);
free (uri); free (uri);
goto error; goto error;
} }
root->device = st.st_dev;
root->inode = st.st_ino;
#else
root->path = strdup (p_access->psz_filepath);
#endif
p_access->p_sys = p_sys; p_access->p_sys = p_sys;
p_sys->current = root; p_sys->current = root;
p_sys->ignored_exts = var_InheritString (p_access, "ignore-filetypes"); p_sys->ignored_exts = var_InheritString (p_access, "ignore-filetypes");
p_sys->header = true; p_sys->header = true;
p_sys->i_item_count = 0; p_sys->i_item_count = 0;
p_sys->psz_xspf_extension = strdup( "" ); p_sys->xspf_ext = strdup ("");
/* Handle mode */ /* Handle mode */
char *psz = var_InheritString (p_access, "recursive"); char *psz = var_InheritString (p_access, "recursive");
...@@ -196,26 +200,20 @@ void DirClose( vlc_object_t * p_this ) ...@@ -196,26 +200,20 @@ void DirClose( vlc_object_t * p_this )
free (current); free (current);
} }
free (p_sys->psz_xspf_extension); free (p_sys->xspf_ext);
free (p_sys->ignored_exts); free (p_sys->ignored_exts);
free (p_sys); free (p_sys);
} }
/* Detect directories that recurse into themselves. */ /* Detect directories that recurse into themselves. */
static bool has_inode_loop (const directory_t *dir) static bool has_inode_loop (const directory_t *dir, dev_t dev, ino_t inode)
{ {
#ifndef WIN32 while (dir != NULL)
dev_t dev = dir->st.st_dev; {
ino_t inode = dir->st.st_ino; if ((dir->device == dev) && (dir->inode == inode))
while ((dir = dir->parent) != NULL)
if ((dir->st.st_dev == dev) && (dir->st.st_ino == inode))
return true; return true;
#else dir = dir->parent;
# undef fstat }
# define fstat( fd, st ) (0)
VLC_UNUSED( dir );
#endif
return false; return false;
} }
...@@ -255,11 +253,12 @@ block_t *DirBlock (access_t *p_access) ...@@ -255,11 +253,12 @@ block_t *DirBlock (access_t *p_access)
if (p_sys->current == NULL) if (p_sys->current == NULL)
{ /* End of XSPF playlist */ { /* End of XSPF playlist */
char *footer; char *footer;
int len = asprintf( &footer, " </trackList>\n" \ int len = asprintf (&footer, " </trackList>\n"
" <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \ " <extension application=\"http://www.videolan.org/"
"%s" \ "vlc/playlist/0\">\n"
" </extension>\n" \ "%s"
"</playlist>\n", p_sys->psz_xspf_extension ); " </extension>\n"
"</playlist>\n", p_sys->xspf_ext ? p_sys->xspf_ext : "");
if (unlikely(len == -1)) if (unlikely(len == -1))
goto fatal; goto fatal;
...@@ -273,14 +272,12 @@ block_t *DirBlock (access_t *p_access) ...@@ -273,14 +272,12 @@ block_t *DirBlock (access_t *p_access)
{ {
/* This was the end of a "subnode" */ /* This was the end of a "subnode" */
/* Write the ID to the extension */ /* Write the ID to the extension */
char *old_xspf_extension = p_sys->psz_xspf_extension; char *old_xspf_ext = p_sys->xspf_ext;
if (old_xspf_extension == NULL) if (old_xspf_ext != NULL
goto fatal; && asprintf (&p_sys->xspf_ext, "%s </vlc:node>\n",
old_xspf_ext ? old_xspf_ext : "") == -1)
int len2 = asprintf( &p_sys->psz_xspf_extension, "%s </vlc:node>\n", old_xspf_extension ); p_sys->xspf_ext = NULL;
if (len2 == -1) free (old_xspf_ext);
goto fatal;
free( old_xspf_extension );
} }
return NULL; return NULL;
} }
...@@ -291,83 +288,86 @@ block_t *DirBlock (access_t *p_access) ...@@ -291,83 +288,86 @@ block_t *DirBlock (access_t *p_access)
free (entry); free (entry);
return NULL; return NULL;
} }
/* Handle recursion */ /* Handle recursion */
if (p_sys->mode != MODE_COLLAPSE) if (p_sys->mode != MODE_COLLAPSE)
{ {
directory_t *sub = malloc (sizeof (*sub));
if (sub == NULL)
{
free (entry);
return NULL;
}
DIR *handle; DIR *handle;
#ifdef HAVE_OPENAT #ifdef HAVE_OPENAT
int fd = vlc_openat (dirfd (current->handle), entry, O_RDONLY); int fd = vlc_openat (dirfd (current->handle), entry, O_RDONLY);
if (fd != -1) if (fd == -1)
goto skip; /* File cannot be opened... forget it */
struct stat st;
if (fstat (fd, &st))
{ {
handle = fdopendir (fd);
if (handle == NULL)
close (fd); close (fd);
goto skip; /* cannot stat?! */
}
if (!S_ISDIR (st.st_mode))
{
close (fd);
goto notdir;
}
if (p_sys->mode == MODE_NONE
|| has_inode_loop (current, st.st_dev, st.st_ino)
|| (handle = fdopendir (fd)) == NULL)
{
close (fd);
goto skip;
} }
else
handle = NULL;
#else #else
if (asprintf (&sub->path, "%s/%s", current->path, entry) != -1) char *path;
handle = vlc_opendir (sub->path); if (asprintf (&path, "%s/%s", current->path, entry) == -1)
else goto skip;
handle = NULL; if ((handle = vlc_opendir (path)) == NULL)
goto notdir;
if (p_sys->mode == MODE_NONE)
goto skip;
#endif #endif
if (handle != NULL) directory_t *sub = malloc (sizeof (*sub));
if (unlikely(sub == NULL))
{ {
closedir (handle);
#ifndef HAVE_OPENAT
free (path);
#endif
goto skip;
}
sub->parent = current; sub->parent = current;
sub->handle = handle; sub->handle = handle;
#ifdef HAVE_OPENAT
sub->device = st.st_dev;
sub->inode = st.st_ino;
#else
sub->path = path;
#endif
p_sys->current = sub;
char *encoded = encode_URI_component (entry); char *encoded = encode_URI_component (entry);
if ((encoded == NULL) if (encoded == NULL
|| (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1)) || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
sub->uri = NULL; sub->uri = NULL;
free (encoded); free (encoded);
if (unlikely(sub->uri == NULL))
if ((p_sys->mode == MODE_NONE)
|| fstat (dirfd (handle), &sub->st)
|| has_inode_loop (sub)
|| (sub->uri == NULL))
{
free (entry);
closedir (handle);
free (sub->uri);
free (sub);
return NULL;
}
p_sys->current = sub;
/* Add node to xspf extension */
char *old_xspf_extension = p_sys->psz_xspf_extension;
if (old_xspf_extension == NULL)
{ {
free (entry); free (entry);
goto fatal; goto fatal;
} }
/* Add node to XSPF extension */
char *old_xspf_ext = p_sys->xspf_ext;
char *title = convert_xml_special_chars (entry); char *title = convert_xml_special_chars (entry);
free (entry); if (old_xspf_ext != NULL && title != NULL
if (title == NULL && asprintf (&p_sys->xspf_ext, "%s <vlc:node title=\"%s\">\n",
|| asprintf (&p_sys->psz_xspf_extension, "%s" old_xspf_ext, title) == -1)
" <vlc:node title=\"%s\">\n", old_xspf_extension, p_sys->xspf_ext = NULL;
title) == -1) free (old_xspf_ext);
{
free (title);
goto fatal;
}
free (title); free (title);
free (old_xspf_extension); goto skip;
return NULL;
}
else
free (sub);
} }
notdir:
/* Skip files with ignored extensions */ /* Skip files with ignored extensions */
if (p_sys->ignored_exts != NULL) if (p_sys->ignored_exts != NULL)
{ {
...@@ -411,15 +411,12 @@ block_t *DirBlock (access_t *p_access) ...@@ -411,15 +411,12 @@ block_t *DirBlock (access_t *p_access)
goto fatal; goto fatal;
/* Write the ID to the extension */ /* Write the ID to the extension */
char *old_xspf_extension = p_sys->psz_xspf_extension; char *old_xspf_ext = p_sys->xspf_ext;
if (old_xspf_extension == NULL) if (old_xspf_ext != NULL
goto fatal; && asprintf (&p_sys->xspf_ext, "%s <vlc:item tid=\"%i\" />\n",
old_xspf_ext, p_sys->i_item_count - 1) == -1)
int len2 = asprintf( &p_sys->psz_xspf_extension, "%s <vlc:item tid=\"%i\" />\n", p_sys->xspf_ext = NULL;
old_xspf_extension, p_sys->i_item_count-1 ); free (old_xspf_ext);
if (len2 == -1)
goto fatal;
free( old_xspf_extension );
block_t *block = block_heap_Alloc (entry, entry, len); block_t *block = block_heap_Alloc (entry, entry, len);
if (unlikely(block == NULL)) if (unlikely(block == NULL))
...@@ -432,6 +429,10 @@ block_t *DirBlock (access_t *p_access) ...@@ -432,6 +429,10 @@ block_t *DirBlock (access_t *p_access)
fatal: fatal:
p_access->info.b_eof = true; p_access->info.b_eof = true;
return NULL; return NULL;
skip:
free (entry);
return NULL;
} }
/***************************************************************************** /*****************************************************************************
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment