src/hg/lib/jsHelper.c 1.30

1.30 2009/08/15 20:04:03 larrym
restore dynamic creation of javascript symlinks which I deleted in version 1.29; rationale: to handle mirrors who don't preserve mtime's when rsync'ing
Index: src/hg/lib/jsHelper.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/lib/jsHelper.c,v
retrieving revision 1.29
retrieving revision 1.30
diff -b -B -U 4 -r1.29 -r1.30
--- src/hg/lib/jsHelper.c	13 Aug 2009 07:32:29 -0000	1.29
+++ src/hg/lib/jsHelper.c	15 Aug 2009 20:04:03 -0000	1.30
@@ -371,15 +371,39 @@
             mtime = fileModTime(dyStringContents(realFileName));
 
             // We add mtime to create a pseudo-version; this forces browsers to reload js file when it changes,
             // which fixes bugs and odd behavior that occurs when the browser caches modified js files.
-            // The versioned files are soft-links created by the js directory makefile
+            // We initially tried the simpler solution of appending the mtime as a query parameter (e.g. "?v=123456789"),
+            // but that reportedly caused problems in some versions of Firefox.
 
             dyStringPrintf(fileNameWithVersion, "%s-%ld.js", baseName, mtime);
             dyStringPrintf(fullNameWithVersion, "%s/%s", dyStringContents(fullDirName), dyStringContents(fileNameWithVersion));
             if(!fileExists(dyStringContents(fullNameWithVersion)))
                 {
-                errAbort("jsIncludeFile: versioned javascript file: %s doesn't exist.\n", dyStringContents(fullNameWithVersion));
+                // The versioned copy should be created by the install process; however, mirrors may fail
+                // to preserve mtime's when copying over the javascript files, in which cased the
+                // versioned softlinks won't match the real file; in that case, we try to create
+                // the versioned links on the fly (which requires write access to the javascript directory).
+
+                struct dyString *pattern = dyStringNew(0);
+                struct slName *files, *file;
+                dyStringPrintf(pattern, "%s-[0-9]+\\.js", baseName);
+                files = listDirRegEx(dyStringContents(fullDirName), dyStringContents(pattern), REG_EXTENDED);
+                for (file = files; file != NULL; file = file->next)
+                    {
+                    struct dyString *tmp = dyStringNew(0);
+                    dyStringPrintf(tmp, "%s/%s", dyStringContents(fullDirName), file->name);
+                    unlink(dyStringContents(tmp));
+                    dyStringFree(&tmp);
+                    }
+                slFreeList(&files);
+                dyStringFree(&pattern);
+                if(symlink(dyStringContents(realFileName), dyStringContents(fullNameWithVersion)))
+                    {
+                    int err = errno;
+                    errAbort("jsIncludeFile: symlink failed: errno: %d (%s); the directory '%s' must be writeable by user '%s'; alternatively, the installation process must create the versioned files\n", 
+                             err, strerror(err), dyStringContents(fullDirName), getUser());
+                    }
                 }
             dyStringFree(&fullNameWithVersion);
             dyStringFree(&fullDirName);
             }