Browse files

Merge branch 'w29_MDL-22584_m21_monsterpaths' of git://github.com/sko…

…dak/moodle into MOODLE_21_STABLE
  • Loading branch information...
2 parents 18c2fcf + 4e64649 commit 38a6a195a4944a36b079e4f4c8a758dba9c801ae Sam Hemelryk committed Jul 25, 2011
Showing with 29 additions and 4 deletions.
  1. +29 −4 lib/db/upgradelib.php
View
33 lib/db/upgradelib.php
@@ -314,7 +314,9 @@ function upgrade_migrate_files_course($context, $path, $delete) {
if ($item->isFile()) {
if (!$item->isReadable()) {
- echo $OUTPUT->notification(" File not readable, skipping: ".$fullpathname.$item->getFilename());
+ $notification = "File not readable, skipping: ".$fullpathname.$item->getFilename();
+ echo $OUTPUT->notification($notification);
+ upgrade_log(UPGRADE_LOG_NOTICE, null, $notification);
continue;
}
@@ -327,8 +329,31 @@ function upgrade_migrate_files_course($context, $path, $delete) {
}
if ($textlib->strlen($filepath) > 255) {
- echo $OUTPUT->notification(" File path longer than 255 chars, skipping: ".$fullpathname.$item->getFilename());
- continue;
+ // we need something unique and reproducible, sorry no shortening possible
+ $filepath = '/directory_over_255_chars/'.md5($filepath).'/';
+ $oldfile = $fullpathname.$item->getFilename();
+ $newfile = $filepath.$item->getFilename();
+ $notification = "File path longer than 255 chars '$oldfile', file path truncated to '$newfile'";
+ echo $OUTPUT->notification($notification);
+ upgrade_log(UPGRADE_LOG_NOTICE, null, $notification);
+ }
+
+ if ($textlib->strlen($filename) > 255) {
+ //try to shorten, but look for collisions
+ $oldfile = $fullpathname.$item->getFilename();
+ $parts = explode('.', $filename);
+ $ext = array_pop($parts);
+ $name = implode('.', $parts);
+ $name = $textlib->substr($name, 0, 254-$textlib->strlen($ext));
+ $newfilename = $name . '.' . $ext;
+ if (file_exists($fullpathname.$newfilename) or $fs->file_exists($context->id, $component, $filearea, '0', $filepath, $newfilename)) {
+ $filename = 'file_name_over_255_chars'.md5($filename).$ext; // bad luck, file with shortened name exists
+ } else {
+ $filename = $newfilename; // shortened name should not cause collisions
+ }
+ $notification = "File name longer than 255 chars '$oldfile', file name truncated to '$filename'";
+ echo $OUTPUT->notification($notification);
+ upgrade_log(UPGRADE_LOG_NOTICE, null, $notification);
}
if (!$fs->file_exists($context->id, $component, $filearea, '0', $filepath, $filename)) {
@@ -352,7 +377,7 @@ function upgrade_migrate_files_course($context, $path, $delete) {
continue;
}
$filepath = ($filepath.$dirname.'/');
- if ($filepath !== '/backupdata/') {
+ if ($filepath !== '/backupdata/' and $textlib->strlen($filepath) <= 255) {
$fs->create_directory($context->id, $component, $filearea, 0, $filepath);
}

0 comments on commit 38a6a19

Please sign in to comment.