Permalink
Browse files

tar: prevent few prefix edge cases and simplify logic.

  • Loading branch information...
ctalkington committed Jul 7, 2013
1 parent e7f9a8b commit ae494cde8b74a9678c4800e49784bd0214d540c5
Showing with 11 additions and 18 deletions.
  1. +11 −18 lib/headers/tar.js
View
@@ -126,11 +126,9 @@ HeaderTarFile.prototype._normalize = function(data) {
data.date = util.convertDateTimeEpoch(data.mtime);
}
- if (data.name.length > 100) {
- var pathParts = this._splitFilePath(data.name);
- data.name = pathParts[0];
- data.prefix = pathParts[1];
- }
+ var pathParts = this._splitFilePath(data.name);
+ data.name = pathParts[0];
+ data.prefix = pathParts[1];
return data;
};
@@ -185,23 +183,18 @@ HeaderTarFile.prototype._parseNumeric = function(str) {
};
HeaderTarFile.prototype._splitFilePath = function(filepath) {
- var baseName = path.basename(filepath);
+ var fileName = filepath;
var filePrefix = '';
- var fileName = '';
+ var sepIndex;
- var prefixParts = path.dirname(filepath).split('/');
- var nameAvail = 100 - baseName.length;
+ if (filepath.length > 100 && filepath.length <= 255) {
+ sepIndex = filepath.substring(0, 155).lastIndexOf('/');
- prefixParts.forEach(function(pre) {
- if ((filePrefix.length + pre.length) < 155) {
- filePrefix += pre + '/';
- } else if ((fileName.length + pre.length) < nameAvail) {
- fileName += pre + '/';
+ if (sepIndex !== -1) {
+ filePrefix = filepath.substring(0, sepIndex);
+ fileName = filepath.substring(sepIndex + 1);
}
- });
-
- fileName += baseName;
- filePrefix = filePrefix.substring(0, filePrefix.length - 1);
+ }
return [fileName, filePrefix];
};

0 comments on commit ae494cd

Please sign in to comment.