]> git.sesse.net Git - vlc/blobdiff - modules/access/directory.c
Trailing ;
[vlc] / modules / access / directory.c
index 63e4c84709f13c341d9ee6d80d1c6829741245e5..379c1a147522ccf206b162cb0aca65aa05a3a4f3 100644 (file)
 #   include <unistd.h>
 #elif defined( WIN32 ) && !defined( UNDER_CE )
 #   include <io.h>
-static inline int dirfd (void *dir)
-{
-    return -1;
-}
-#elif defined( UNDER_CE )
-#   define strcoll strcmp
 #endif
 
 #ifdef HAVE_DIRENT_H
@@ -84,22 +78,22 @@ static const char *const psz_recursive_list_text[] = {
         "This is useful if you add directories that contain playlist files " \
         "for instance. Use a comma-separated list of extensions." )
 
-vlc_module_begin();
-    set_category( CAT_INPUT );
-    set_shortname( N_("Directory" ) );
-    set_subcategory( SUBCAT_INPUT_ACCESS );
-    set_description( N_("Standard filesystem directory input") );
-    set_capability( "access", 55 );
-    add_shortcut( "directory" );
-    add_shortcut( "dir" );
-    add_shortcut( "file" );
+vlc_module_begin ()
+    set_category( CAT_INPUT )
+    set_shortname( N_("Directory" ) )
+    set_subcategory( SUBCAT_INPUT_ACCESS )
+    set_description( N_("Standard filesystem directory input") )
+    set_capability( "access", 55 )
+    add_shortcut( "directory" )
+    add_shortcut( "dir" )
+    add_shortcut( "file" )
     add_string( "recursive", "expand" , NULL, RECURSIVE_TEXT,
-                RECURSIVE_LONGTEXT, false );
-      change_string_list( psz_recursive_list, psz_recursive_list_text, 0 );
-    add_string( "ignore-filetypes", "m3u,db,nfo,jpg,gif,sfv,txt,sub,idx,srt,cue",
-                NULL, IGNORE_TEXT, IGNORE_LONGTEXT, false );
-    set_callbacks( Open, Close );
-vlc_module_end();
+                RECURSIVE_LONGTEXT, false )
+      change_string_list( psz_recursive_list, psz_recursive_list_text, 0 )
+    add_string( "ignore-filetypes", "m3u,db,nfo,jpg,jpeg,ljpg,gif,png,pgm,pgmyuv,pbm,pam,tga,bmp,pnm,xpm,xcf,pcx,tif,tiff,lbm,sfv,txt,sub,idx,srt,cue,ssa",
+                NULL, IGNORE_TEXT, IGNORE_LONGTEXT, false )
+    set_callbacks( Open, Close )
+vlc_module_end ()
 
 
 /*****************************************************************************
@@ -119,7 +113,9 @@ struct directory_t
     directory_t *parent;
     DIR         *handle;
     char        *uri;
+#ifndef WIN32
     struct stat  st;
+#endif
     char         path[1];
 };
 
@@ -129,6 +125,8 @@ struct access_sys_t
     DIR *handle;
     char *ignored_exts;
     int mode;
+    int i_item_count;
+    char *psz_xspf_extension;
 };
 
 static block_t *Block( access_t * );
@@ -145,18 +143,38 @@ static int Open( vlc_object_t *p_this )
     if( !p_access->psz_path )
         return VLC_EGENERIC;
 
-    DIR *handle = utf8_opendir (p_access->psz_path);
+    DIR *handle;
+    if (strcmp (p_access->psz_path, "-"))
+        handle = utf8_opendir (p_access->psz_path);
+    else
+    {
+#if 0   /* This won't work yet, it generates paths like "-/music.ogg".
+         * We'd need to use openat() here and in the file access... */
+        int fd = dup (0);
+        handle = fdopendir (fd);
+        if (handle == NULL)
+            close (fd);
+#else
+        return VLC_EGENERIC;
+#endif
+    }
+
     if (handle == NULL)
         return VLC_EGENERIC;
 
     p_sys = malloc (sizeof (*p_sys));
     if (!p_sys)
+    {
+        closedir( handle );
         return VLC_ENOMEM;
+    }
 
     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->i_item_count = 0;
+    p_sys->psz_xspf_extension = strdup( "" );
 
     /* Handle mode */
     char *psz = var_CreateGetString( p_access, "recursive" );
@@ -197,6 +215,7 @@ static void Close( vlc_object_t * p_this )
     }
     if (p_sys->handle != NULL)
         closedir (p_sys->handle); /* corner case,:Block() not called ever */
+    free (p_sys->psz_xspf_extension);
     free (p_sys->ignored_exts);
     free (p_sys);
 }
@@ -225,12 +244,16 @@ static char *encode_path (const char *path)
 /* Detect directories that recurse into themselves. */
 static bool has_inode_loop (const directory_t *dir)
 {
+#ifndef WIN32
     dev_t dev = dir->st.st_dev;
     ino_t inode = dir->st.st_ino;
 
     while ((dir = dir->parent) != NULL)
         if ((dir->st.st_dev == dev) && (dir->st.st_ino == inode))
             return true;
+#else
+# define fstat( fd, st ) (0)
+#endif
     return false;
 }
 
@@ -246,7 +269,7 @@ static block_t *Block (access_t *p_access)
     {   /* Startup: send the XSPF header */
         static const char header[] =
             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
-            "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">\n"
+            "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
             " <trackList>\n";
         block_t *block = block_Alloc (sizeof (header) - 1);
         if (!block)
@@ -287,22 +310,41 @@ static block_t *Block (access_t *p_access)
 
         if (p_sys->current == NULL)
         {   /* End of XSPF playlist */
-            static const char footer[] =
-                " </trackList>\n"
-                "</playlist>\n";
+            char *footer;
+            int len = asprintf( &footer, " </trackList>\n" \
+                " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
+                "%s" \
+                " </extension>\n" \
+                "</playlist>\n", p_sys->psz_xspf_extension );
+            if( len < 0 )
+                goto fatal;
 
-            block_t *block = block_Alloc (sizeof (footer) - 1);
+            block_t *block = block_Alloc ( len );
             if (!block)
                 goto fatal;
-            memcpy (block->p_buffer, footer, sizeof (footer) - 1);
+            memcpy (block->p_buffer, footer, len);
+            free( footer );
             p_access->info.b_eof = true;
             return block;
         }
+        else
+        {
+            /* This was the end of a "subnode" */
+            /* Write the ID to the extension */
+            char *old_xspf_extension = p_sys->psz_xspf_extension;
+            if (old_xspf_extension == NULL)
+                goto fatal;
+
+            int len2 = asprintf( &p_sys->psz_xspf_extension, "%s  </vlc:node>\n", old_xspf_extension );
+            if (len2 == -1)
+                goto fatal;
+            free( old_xspf_extension );
+        }
         return NULL;
     }
 
-    /* Skip current and parent directories */
-    if (!strcmp (entry, ".") || !strcmp (entry, ".."))
+    /* Skip current, parent and hidden directories */
+    if (entry[0] == '.')
         return NULL;
     /* Handle recursion */
     if (p_sys->mode != MODE_COLLAPSE)
@@ -318,7 +360,12 @@ static block_t *Block (access_t *p_access)
         {
             sub->parent = current;
             sub->handle = handle;
-            sub->uri = encode_path (sub->path);
+
+            char *encoded = encode_URI_component (entry);
+            if ((encoded == NULL)
+             || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
+                 sub->uri = NULL;
+            free (encoded);
 
             if ((p_sys->mode == MODE_NONE)
              || fstat (dirfd (handle), &sub->st)
@@ -330,6 +377,17 @@ static block_t *Block (access_t *p_access)
                 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)
+                goto fatal;
+
+            int len2 = asprintf( &p_sys->psz_xspf_extension, "%s  <vlc:node title=\"%s\">\n", old_xspf_extension, entry );
+            if (len2 == -1)
+                goto fatal;
+            free( old_xspf_extension );
+
             return NULL;
         }
         else
@@ -353,6 +411,9 @@ static block_t *Block (access_t *p_access)
                 if (type + extlen == end
                  && !strncasecmp (ext, type, extlen))
                     return NULL;
+
+                if (*end == '\0')
+                    break;
             }
         }
     }
@@ -362,12 +423,27 @@ static block_t *Block (access_t *p_access)
     if (encoded == NULL)
         goto fatal;
     int len = asprintf (&entry,
-                        "  <track><location>file://%s/%s</location></track>\n",
-                        current->uri, encoded);
+                        "  <track><location>file://%s/%s</location>\n" \
+                        "   <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
+                        "    <vlc:id>%d</vlc:id>\n" \
+                        "   </extension>\n" \
+                        "  </track>\n",
+                        current->uri, encoded, p_sys->i_item_count++);
     free (encoded);
     if (len == -1)
         goto fatal;
 
+    /* Write the ID to the extension */
+    char *old_xspf_extension = p_sys->psz_xspf_extension;
+    if (old_xspf_extension == NULL)
+        goto fatal;
+
+    int len2 = asprintf( &p_sys->psz_xspf_extension, "%s   <vlc:item tid=\"%i\" />\n",
+                            old_xspf_extension, p_sys->i_item_count-1 );
+    if (len2 == -1)
+        goto fatal;
+    free( old_xspf_extension );
+
     /* TODO: new block allocator for malloc()ated data */
     block_t *block = block_Alloc (len);
     if (!block)
@@ -389,9 +465,8 @@ fatal:
  *****************************************************************************/
 static int Control( access_t *p_access, int i_query, va_list args )
 {
-    bool   *pb_bool;
-    int          *pi_int;
-    int64_t      *pi_64;
+    bool    *pb_bool;
+    int64_t *pi_64;
 
     switch( i_query )
     {
@@ -409,11 +484,6 @@ static int Control( access_t *p_access, int i_query, va_list args )
             break;
 
         /* */
-        case ACCESS_GET_MTU:
-            pi_int = (int*)va_arg( args, int * );
-            *pi_int = 0;
-            break;
-
         case ACCESS_GET_PTS_DELAY:
             pi_64 = (int64_t*)va_arg( args, int64_t * );
             *pi_64 = DEFAULT_PTS_DELAY * 1000;