Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.1209 diff -u -p -r1.1209 common.inc --- includes/common.inc 20 Aug 2010 01:17:51 -0000 1.1209 +++ includes/common.inc 20 Aug 2010 22:46:04 -0000 @@ -3210,42 +3210,52 @@ function drupal_build_css_cache($css) { $uri = $map[$key]; } - if (empty($uri) || !file_exists($uri)) { - // Build aggregate CSS file. - foreach ($css as $stylesheet) { - // Only 'file' stylesheets can be aggregated. - if ($stylesheet['type'] == 'file') { - $contents = drupal_load_stylesheet($stylesheet['data'], TRUE); - // Return the path to where this CSS file originated from. - $base = base_path() . dirname($stylesheet['data']) . '/'; - _drupal_build_css_path(NULL, $base); - // Prefix all paths within this CSS file, ignoring external and absolute paths. - $data .= preg_replace_callback('/url\(\s*[\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\s*\)/i', '_drupal_build_css_path', $contents); + $lock_acquire_attempts = 0; + do { + if (empty($uri) || !file_exists($uri)) { + if ($lock_acquired = lock_acquire('css_files')) { + // Build aggregate CSS file. + foreach ($css as $stylesheet) { + // Only 'file' stylesheets can be aggregated. + if ($stylesheet['type'] == 'file') { + $contents = drupal_load_stylesheet($stylesheet['data'], TRUE); + // Return the path to where this CSS file originated from. + $base = base_path() . dirname($stylesheet['data']) . '/'; + _drupal_build_css_path(NULL, $base); + // Prefix all paths within this CSS file, ignoring external and absolute paths. + $data .= preg_replace_callback('/url\([\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\)/i', '_drupal_build_css_path', $contents); + } + } + + // Per the W3C specification at http://www.w3.org/TR/REC-CSS2/cascade.html#at-import, + // @import rules must proceed any other style, so we move those to the top. + $regexp = '/@import[^;]+;/i'; + preg_match_all($regexp, $data, $matches); + $data = preg_replace($regexp, '', $data); + $data = implode('', $matches[0]) . $data; + + // Prefix filename to prevent blocking by firewalls which reject files + // starting with "ad*". + $filename = 'css_' . drupal_hash_base64($data) . '.css'; + // Create the css/ within the files folder. + $csspath = 'public://css'; + $uri = $csspath . '/' . $filename; + // Create the CSS file. + file_prepare_directory($csspath, FILE_CREATE_DIRECTORY); + if (!file_exists($uri) && !file_unmanaged_save_data($data, $uri, FILE_EXISTS_REPLACE)) { + return FALSE; + } + // Save the updated map. + $map[$key] = $uri; + variable_set('drupal_css_cache_files', $map); + lock_release('css_files'); + } + else { + lock_wait(); } } - - // Per the W3C specification at http://www.w3.org/TR/REC-CSS2/cascade.html#at-import, - // @import rules must proceed any other style, so we move those to the top. - $regexp = '/@import[^;]+;/i'; - preg_match_all($regexp, $data, $matches); - $data = preg_replace($regexp, '', $data); - $data = implode('', $matches[0]) . $data; - - // Prefix filename to prevent blocking by firewalls which reject files - // starting with "ad*". - $filename = 'css_' . drupal_hash_base64($data) . '.css'; - // Create the css/ within the files folder. - $csspath = 'public://css'; - $uri = $csspath . '/' . $filename; - // Create the CSS file. - file_prepare_directory($csspath, FILE_CREATE_DIRECTORY); - if (!file_exists($uri) && !file_unmanaged_save_data($data, $uri, FILE_EXISTS_REPLACE)) { - return FALSE; - } - // Save the updated map. - $map[$key] = $uri; - variable_set('drupal_css_cache_files', $map); } + while (!file_exists($uri) && $lock_acquired === FALSE && ++$lock_acquire_attempts < 5); return $uri; } @@ -4421,28 +4431,38 @@ function drupal_build_js_cache($files) { $uri = $map[$key]; } - if (empty($uri) || !file_exists($uri)) { - // Build aggregate JS file. - foreach ($files as $path => $info) { - if ($info['preprocess']) { - // Append a ';' and a newline after each JS file to prevent them from running together. - $contents .= file_get_contents($path) . ";\n"; - } - } - // Prefix filename to prevent blocking by firewalls which reject files - // starting with "ad*". - $filename = 'js_' . drupal_hash_base64($contents) . '.js'; - // Create the js/ within the files folder. - $jspath = 'public://js'; - $uri = $jspath . '/' . $filename; - // Create the JS file. - file_prepare_directory($jspath, FILE_CREATE_DIRECTORY); - if (!file_unmanaged_save_data($contents, $uri, FILE_EXISTS_REPLACE)) { - return FALSE; + $lock_acquire_attempts = 0; + do { + if (empty($uri) || !file_exists($uri)) { + if ($lock_acquired = lock_acquire('js_files')) { + // Build aggregate JS file. + foreach ($files as $path => $info) { + if ($info['preprocess']) { + // Append a ';' and a newline after each JS file to prevent them from running together. + $contents .= file_get_contents($path) . ";\n"; + } + } + // Prefix filename to prevent blocking by firewalls which reject files + // starting with "ad*". + $filename = 'js_' . drupal_hash_base64($contents) . '.js'; + // Create the js/ within the files folder. + $jspath = 'public://js'; + $uri = $jspath . '/' . $filename; + // Create the JS file. + file_prepare_directory($jspath, FILE_CREATE_DIRECTORY); + if (!file_unmanaged_save_data($contents, $uri, FILE_EXISTS_REPLACE)) { + return FALSE; + } + $map[$key] = $uri; + variable_set('drupal_js_cache_files', $map); + lock_release('js_files'); + } + else { + lock_wait(); + } } - $map[$key] = $uri; - variable_set('drupal_js_cache_files', $map); } + while (!file_exists($uri) && $lock_acquired === FALSE && ++$lock_acquire_attempts < 5); return $uri; }