summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Kolesa <d.kolesa@samsung.com>2014-11-06 10:57:19 +0000
committerDaniel Kolesa <d.kolesa@samsung.com>2014-11-06 10:57:36 +0000
commit6b20fcdc8fdd498956e61aa9534bccccb3480763 (patch)
treeb1c7ef6d0f721ae79fddfcc1cd0e26169b52fff0
parent8ceef8c6ca12b00698c7cd13394fba1de8e4c6b7 (diff)
elua: two-stage bytecode caching
This unbreaks parallel builds where an elua process tries to read a bytecode file that is currently being written. Now, we write into a temporary file first instead and when it's written, rename. @fix
-rw-r--r--src/bin/elua/cache.c40
1 files changed, 37 insertions, 3 deletions
diff --git a/src/bin/elua/cache.c b/src/bin/elua/cache.c
index ae90d30b24..c71fd04e7b 100644
--- a/src/bin/elua/cache.c
+++ b/src/bin/elua/cache.c
@@ -60,14 +60,41 @@ writef(lua_State *L EINA_UNUSED, const void *p, size_t size, void *ud)
60 return ferror(f) || (fwrite(p, 1, size, f) != size); 60 return ferror(f) || (fwrite(p, 1, size, f) != size);
61} 61}
62 62
63static FILE *
64bc_tmp_open(const char *fname, char *buf, size_t buflen)
65{
66 int fd;
67#ifndef _WIN32
68 mode_t old_umask;
69#endif
70 char *fs = strrchr(fname, '/'), *bs = strrchr(fname, '\\');
71 if (!fs && !bs)
72 snprintf(buf, buflen, "./XXXXXX");
73 else
74 {
75 char *ss = (fs > bs) ? fs : bs;
76 snprintf(buf, buflen, "%.*sXXXXXX", (int)(ss - fname + 1), fname);
77 }
78#ifndef _WIN32
79 old_umask = umask(S_IRWXG|S_IRWXO);
80#endif
81 fd = mkstemp(buf);
82#ifndef _WIN32
83 umask(old_umask);
84#endif
85 if (fd < 0)
86 return NULL;
87 return fdopen(fd, "w");
88}
89
63static void 90static void
64write_bc(lua_State *L, const char *fname) 91write_bc(lua_State *L, const char *fname)
65{ 92{
66 FILE *f; 93 FILE *f;
67 char buf[PATH_MAX]; 94 char buf[PATH_MAX];
68 snprintf(buf, sizeof(buf), "%sc", fname); 95 if ((f = bc_tmp_open(fname, buf, sizeof(buf))))
69 if ((f = fopen(buf, "wb")))
70 { 96 {
97 char buf2[PATH_MAX];
71 if (lua_dump(L, writef, f)) 98 if (lua_dump(L, writef, f))
72 { 99 {
73 fclose(f); 100 fclose(f);
@@ -75,6 +102,13 @@ write_bc(lua_State *L, const char *fname)
75 (void)!!remove(buf); 102 (void)!!remove(buf);
76 } 103 }
77 else fclose(f); 104 else fclose(f);
105 snprintf(buf2, sizeof(buf2), "%sc", fname);
106 if (rename(buf, buf2))
107 {
108 /* a futile attempt at cleanup */
109 (void)!!remove(buf);
110 (void)!!remove(buf2);
111 }
78 } 112 }
79} 113}
80 114