lock xft, plugin index builder lock, clear clip thumbnails, clear vwin clock, setpoin...
[goodguy/history.git] / cinelerra-5.1 / cinelerra / mwindow.C
index 2e590c668f376c244c2d46fd6af4f7693962888c..e81b4574387016109e21385ac5fd73b8b2a9b796 100644 (file)
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/mman.h>
+#include <sys/file.h>
 #include <limits.h>
 #include <errno.h>
 
@@ -523,12 +524,11 @@ void MWindow::get_plugin_path(char *path, const char *plug_dir, const char *fs_p
        delete [] base_path;
 }
 
-int MWindow::load_plugin_index(MWindow *mwindow, const char *index_path, const char *plugin_dir)
+int MWindow::load_plugin_index(MWindow *mwindow, FILE *fp, const char *plugin_dir)
 {
-// load index
-       FILE *fp = fopen(index_path, "r");
        if( !fp ) return -1;
-
+// load index
+       fseek(fp, 0, SEEK_SET);
        int ret = 0;
        char index_line[BCTEXTLEN];
        int index_version = -1, len = strlen(plugin_dir);
@@ -576,7 +576,6 @@ int MWindow::load_plugin_index(MWindow *mwindow, const char *index_path, const c
                        ret = 1;  continue;
                }
        }
-       fclose(fp);
 
        if( !ret )
                ret = check_plugin_index(plugins, plugin_dir, ".");
@@ -623,21 +622,36 @@ int MWindow::init_plugins(MWindow *mwindow, Preferences *preferences)
        create_defaults_path(index_path, PLUGIN_FILE);
        char *plugin_dir = FileSystem::basepath(preferences->plugin_dir);
        strcpy(plugin_path, plugin_dir);  delete [] plugin_dir;
-       if( !load_plugin_index(mwindow, index_path, plugin_path) ) return 1;
-       printf("init plugin index: %s\n", plugin_path);
-       FILE *fp = fopen(index_path,"w");
+       FILE *fp = fopen(index_path,"a+");
        if( !fp ) {
                fprintf(stderr,_("MWindow::init_plugins: "
-                       "can't create plugin index: %s\n"), index_path);
-               return 1;
+                       "can't open plugin index: %s\n"), index_path);
+               return -1;
+       }
+       int fd = fileno(fp), ret = -1;
+       if( !flock(fd, LOCK_EX) ) {
+               fseek(fp, 0, SEEK_SET);
+               ret = load_plugin_index(mwindow, fp, plugin_path);
+       }
+       if( ret > 0 ) {
+               ftruncate(fd, 0);
+               fseek(fp, 0, SEEK_SET);
+               printf("init plugin index: %s\n", plugin_path);
+               fprintf(fp, "%d\n", PLUGIN_FILE_VERSION);
+               fprintf(fp, "%s\n", plugin_path);
+               init_plugin_index(mwindow, preferences, fp, plugin_path);
+               init_ffmpeg_index(mwindow, preferences, fp);
+               init_lv2_index(mwindow, preferences, fp);
+               fseek(fp, 0, SEEK_SET);
+               ret = load_plugin_index(mwindow, fp, plugin_path);
+       }
+       if( ret ) {
+               fprintf(stderr,_("MWindow::init_plugins: "
+                       "can't %s plugin index: %s\n"),
+                       ret>0 ? _("create") : _("lock"), index_path);
        }
-       fprintf(fp, "%d\n", PLUGIN_FILE_VERSION);
-       fprintf(fp, "%s\n", plugin_path);
-       init_plugin_index(mwindow, preferences, fp, plugin_path);
-       init_ffmpeg_index(mwindow, preferences, fp);
-       init_lv2_index(mwindow, preferences, fp);
        fclose(fp);
-       return load_plugin_index(mwindow, index_path, plugin_path);
+       return ret;
 }
 
 int MWindow::init_ladspa_plugins(MWindow *mwindow, Preferences *preferences)
@@ -661,9 +675,30 @@ int MWindow::init_ladspa_plugins(MWindow *mwindow, Preferences *preferences)
                for( char *bp=plugin_path; *bp!=0; ++bp )
                        *cp++ = *bp=='/' ? '_' : *bp;
                *cp = 0;
-               if( !load_plugin_index(mwindow, index_path, plugin_path) ) continue;
-               if( init_ladspa_index(mwindow, preferences, index_path, plugin_path) ) continue;
-               load_plugin_index(mwindow, index_path, plugin_path);
+               FILE *fp = fopen(index_path,"a+");
+               if( !fp ) {
+                       fprintf(stderr,_("MWindow::init_ladspa_plugins: "
+                               "can't open ladspa plugin index: %s\n"), index_path);
+                       continue;
+               }
+               int fd = fileno(fp), ret = -1;
+               if( !flock(fd, LOCK_EX) ) {
+                       fseek(fp, 0, SEEK_SET);
+                       ret = load_plugin_index(mwindow, fp, plugin_path);
+               }
+               if( ret > 0 ) {
+                       ftruncate(fd, 0);
+                       fseek(fp, 0, SEEK_SET);
+                       init_ladspa_index(mwindow, preferences, fp, plugin_path);
+                       fseek(fp, 0, SEEK_SET);
+                       ret = load_plugin_index(mwindow, fp, plugin_path);
+               }
+               if( ret ) {
+                       fprintf(stderr,_("MWindow::init_ladspa_plugins: "
+                               "can't %s ladspa plugin index: %s\n"),
+                               ret>0 ? _("create") : _("lock"), index_path);
+               }
+               fclose(fp);
        }
        return 1;
 }