| ... | @@ -15,22 +15,65 @@ | ... | @@ -15,22 +15,65 @@ |
| 15 | //! GNU tar reference: https://www.gnu.org/software/tar/manual/html_node/Standard.html | 15 | //! GNU tar reference: https://www.gnu.org/software/tar/manual/html_node/Standard.html |
| 16 | //! pax reference: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html#tag_20_92_13 | 16 | //! pax reference: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html#tag_20_92_13 |
| 17 | | 17 | |
| 18 | const std = @import("std.zig"); | 18 | const std = @import("std"); |
| 19 | const assert = std.debug.assert; | 19 | const assert = std.debug.assert; |
| | 20 | const testing = std.testing; |
| 20 | | 21 | |
| 21 | pub const output = @import("tar/output.zig"); | 22 | pub const output = @import("tar/output.zig"); |
| 22 | | 23 | |
| 23 | pub const Options = struct { | 24 | /// Provide this to receive detailed error messages. |
| | 25 | /// When this is provided, some errors which would otherwise be returned |
| | 26 | /// immediately will instead be added to this structure. The API user must check |
| | 27 | /// the errors in diagnostics to know whether the operation succeeded or failed. |
| | 28 | pub const Diagnostics = struct { |
| | 29 | allocator: std.mem.Allocator, |
| | 30 | errors: std.ArrayListUnmanaged(Error) = .{}, |
| | 31 | |
| | 32 | pub const Error = union(enum) { |
| | 33 | unable_to_create_sym_link: struct { |
| | 34 | code: anyerror, |
| | 35 | file_name: []const u8, |
| | 36 | link_name: []const u8, |
| | 37 | }, |
| | 38 | unable_to_create_file: struct { |
| | 39 | code: anyerror, |
| | 40 | file_name: []const u8, |
| | 41 | }, |
| | 42 | unsupported_file_type: struct { |
| | 43 | file_name: []const u8, |
| | 44 | file_type: Header.Kind, |
| | 45 | }, |
| | 46 | }; |
| | 47 | |
| | 48 | pub fn deinit(d: *Diagnostics) void { |
| | 49 | for (d.errors.items) |item| { |
| | 50 | switch (item) { |
| | 51 | .unable_to_create_sym_link => |info| { |
| | 52 | d.allocator.free(info.file_name); |
| | 53 | d.allocator.free(info.link_name); |
| | 54 | }, |
| | 55 | .unable_to_create_file => |info| { |
| | 56 | d.allocator.free(info.file_name); |
| | 57 | }, |
| | 58 | .unsupported_file_type => |info| { |
| | 59 | d.allocator.free(info.file_name); |
| | 60 | }, |
| | 61 | } |
| | 62 | } |
| | 63 | d.errors.deinit(d.allocator); |
| | 64 | d.* = undefined; |
| | 65 | } |
| | 66 | }; |
| | 67 | |
| | 68 | /// pipeToFileSystem options |
| | 69 | pub const PipeOptions = struct { |
| 24 | /// Number of directory levels to skip when extracting files. | 70 | /// Number of directory levels to skip when extracting files. |
| 25 | strip_components: u32 = 0, | 71 | strip_components: u32 = 0, |
| 26 | /// How to handle the "mode" property of files from within the tar file. | 72 | /// How to handle the "mode" property of files from within the tar file. |
| 27 | mode_mode: ModeMode = .executable_bit_only, | 73 | mode_mode: ModeMode = .executable_bit_only, |
| 28 | /// Prevents creation of empty directories. | 74 | /// Prevents creation of empty directories. |
| 29 | exclude_empty_directories: bool = false, | 75 | exclude_empty_directories: bool = false, |
| 30 | /// Provide this to receive detailed error messages. | 76 | /// Collects error messages during unpacking |
| 31 | /// When this is provided, some errors which would otherwise be returned immediately | | |
| 32 | /// will instead be added to this structure. The API user must check the errors | | |
| 33 | /// in diagnostics to know whether the operation succeeded or failed. | | |
| 34 | diagnostics: ?*Diagnostics = null, | 77 | diagnostics: ?*Diagnostics = null, |
| 35 | | 78 | |
| 36 | pub const ModeMode = enum { | 79 | pub const ModeMode = enum { |
| ... | @@ -42,56 +85,16 @@ pub const Options = struct { | ... | @@ -42,56 +85,16 @@ pub const Options = struct { |
| 42 | /// Other bits of the mode are left as the default when creating files. | 85 | /// Other bits of the mode are left as the default when creating files. |
| 43 | executable_bit_only, | 86 | executable_bit_only, |
| 44 | }; | 87 | }; |
| 45 | | | |
| 46 | pub const Diagnostics = struct { | | |
| 47 | allocator: std.mem.Allocator, | | |
| 48 | errors: std.ArrayListUnmanaged(Error) = .{}, | | |
| 49 | | | |
| 50 | pub const Error = union(enum) { | | |
| 51 | unable_to_create_sym_link: struct { | | |
| 52 | code: anyerror, | | |
| 53 | file_name: []const u8, | | |
| 54 | link_name: []const u8, | | |
| 55 | }, | | |
| 56 | unable_to_create_file: struct { | | |
| 57 | code: anyerror, | | |
| 58 | file_name: []const u8, | | |
| 59 | }, | | |
| 60 | unsupported_file_type: struct { | | |
| 61 | file_name: []const u8, | | |
| 62 | file_type: Header.Kind, | | |
| 63 | }, | | |
| 64 | }; | | |
| 65 | | | |
| 66 | pub fn deinit(d: *Diagnostics) void { | | |
| 67 | for (d.errors.items) |item| { | | |
| 68 | switch (item) { | | |
| 69 | .unable_to_create_sym_link => |info| { | | |
| 70 | d.allocator.free(info.file_name); | | |
| 71 | d.allocator.free(info.link_name); | | |
| 72 | }, | | |
| 73 | .unable_to_create_file => |info| { | | |
| 74 | d.allocator.free(info.file_name); | | |
| 75 | }, | | |
| 76 | .unsupported_file_type => |info| { | | |
| 77 | d.allocator.free(info.file_name); | | |
| 78 | }, | | |
| 79 | } | | |
| 80 | } | | |
| 81 | d.errors.deinit(d.allocator); | | |
| 82 | d.* = undefined; | | |
| 83 | } | | |
| 84 | }; | | |
| 85 | }; | 88 | }; |
| 86 | | 89 | |
| 87 | pub const Header = struct { | 90 | const Header = struct { |
| 88 | const SIZE = 512; | 91 | const SIZE = 512; |
| 89 | const MAX_NAME_SIZE = 100 + 1 + 155; // name(100) + separator(1) + prefix(155) | 92 | const MAX_NAME_SIZE = 100 + 1 + 155; // name(100) + separator(1) + prefix(155) |
| 90 | const LINK_NAME_SIZE = 100; | 93 | const LINK_NAME_SIZE = 100; |
| 91 | | 94 | |
| 92 | bytes: *const [SIZE]u8, | 95 | bytes: *const [SIZE]u8, |
| 93 | | 96 | |
| 94 | pub const Kind = enum(u8) { | 97 | const Kind = enum(u8) { |
| 95 | normal_alias = 0, | 98 | normal_alias = 0, |
| 96 | normal = '0', | 99 | normal = '0', |
| 97 | hard_link = '1', | 100 | hard_link = '1', |
| ... | @@ -114,9 +117,10 @@ pub const Header = struct { | ... | @@ -114,9 +117,10 @@ pub const Header = struct { |
| 114 | | 117 | |
| 115 | /// Includes prefix concatenated, if any. | 118 | /// Includes prefix concatenated, if any. |
| 116 | /// TODO: check against "../" and other nefarious things | 119 | /// TODO: check against "../" and other nefarious things |
| 117 | pub fn fullName(header: Header, buffer: *[MAX_NAME_SIZE]u8) ![]const u8 { | 120 | pub fn fullName(header: Header, buffer: []u8) ![]const u8 { |
| 118 | const n = name(header); | 121 | const n = name(header); |
| 119 | const p = prefix(header); | 122 | const p = prefix(header); |
| | 123 | if (buffer.len < n.len + p.len + 1) return error.TarInsufficientBuffer; |
| 120 | if (!is_ustar(header) or p.len == 0) { | 124 | if (!is_ustar(header) or p.len == 0) { |
| 121 | @memcpy(buffer[0..n.len], n); | 125 | @memcpy(buffer[0..n.len], n); |
| 122 | return buffer[0..n.len]; | 126 | return buffer[0..n.len]; |
| ... | @@ -127,11 +131,14 @@ pub const Header = struct { | ... | @@ -127,11 +131,14 @@ pub const Header = struct { |
| 127 | return buffer[0 .. p.len + 1 + n.len]; | 131 | return buffer[0 .. p.len + 1 + n.len]; |
| 128 | } | 132 | } |
| 129 | | 133 | |
| 130 | pub fn linkName(header: Header, buffer: *[LINK_NAME_SIZE]u8) []const u8 { | 134 | /// When kind is symbolic_link linked-to name (target_path) is specified in |
| | 135 | /// the linkname field. |
| | 136 | pub fn linkName(header: Header, buffer: []u8) ![]const u8 { |
| 131 | const link_name = header.str(157, 100); | 137 | const link_name = header.str(157, 100); |
| 132 | if (link_name.len == 0) { | 138 | if (link_name.len == 0) { |
| 133 | return buffer[0..0]; | 139 | return buffer[0..0]; |
| 134 | } | 140 | } |
| | 141 | if (buffer.len < link_name.len) return error.TarInsufficientBuffer; |
| 135 | const buf = buffer[0..link_name.len]; | 142 | const buf = buffer[0..link_name.len]; |
| 136 | @memcpy(buf, link_name); | 143 | @memcpy(buf, link_name); |
| 137 | return buf; | 144 | return buf; |
| ... | @@ -233,71 +240,85 @@ fn nullStr(str: []const u8) []const u8 { | ... | @@ -233,71 +240,85 @@ fn nullStr(str: []const u8) []const u8 { |
| 233 | return str; | 240 | return str; |
| 234 | } | 241 | } |
| 235 | | 242 | |
| | 243 | /// Options for iterator. |
| | 244 | /// Buffers should be provided by the caller. |
| 236 | pub const IteratorOptions = struct { | 245 | pub const IteratorOptions = struct { |
| 237 | /// Use a buffer with length `std.fs.MAX_PATH_BYTES` to match file system capabilities. | 246 | /// Use a buffer with length `std.fs.MAX_PATH_BYTES` to match file system capabilities. |
| 238 | file_name_buffer: []u8, | 247 | file_name_buffer: []u8, |
| 239 | /// Use a buffer with length `std.fs.MAX_PATH_BYTES` to match file system capabilities. | 248 | /// Use a buffer with length `std.fs.MAX_PATH_BYTES` to match file system capabilities. |
| 240 | link_name_buffer: []u8, | 249 | link_name_buffer: []u8, |
| | 250 | /// Collects error messages during unpacking |
| 241 | diagnostics: ?*Diagnostics = null, | 251 | diagnostics: ?*Diagnostics = null, |
| 242 | | | |
| 243 | pub const Diagnostics = Options.Diagnostics; | | |
| 244 | }; | 252 | }; |
| 245 | | 253 | |
| 246 | /// Iterates over files in tar archive. | 254 | /// Iterates over files in tar archive. |
| 247 | /// `next` returns each file in `reader` tar archive. | 255 | /// `next` returns each file in tar archive. |
| 248 | pub fn iterator(reader: anytype, options: IteratorOptions) Iterator(@TypeOf(reader)) { | 256 | pub fn iterator(reader: anytype, options: IteratorOptions) Iterator(@TypeOf(reader)) { |
| 249 | return .{ | 257 | return .{ |
| 250 | .reader = reader, | 258 | .reader = reader, |
| 251 | .diagnostics = options.diagnostics, | 259 | .diagnostics = options.diagnostics, |
| 252 | .header_buffer = undefined, | | |
| 253 | .file_name_buffer = options.file_name_buffer, | 260 | .file_name_buffer = options.file_name_buffer, |
| 254 | .link_name_buffer = options.link_name_buffer, | 261 | .link_name_buffer = options.link_name_buffer, |
| 255 | .padding = 0, | | |
| 256 | .file = undefined, | | |
| 257 | }; | 262 | }; |
| 258 | } | 263 | } |
| 259 | | 264 | |
| 260 | fn Iterator(comptime ReaderType: type) type { | 265 | /// Type of the file returned by iterator `next` method. |
| | 266 | pub const FileKind = enum { |
| | 267 | directory, |
| | 268 | sym_link, |
| | 269 | file, |
| | 270 | }; |
| | 271 | |
| | 272 | /// Iteartor over entries in the tar file represented by reader. |
| | 273 | pub fn Iterator(comptime ReaderType: type) type { |
| 261 | return struct { | 274 | return struct { |
| 262 | reader: ReaderType, | 275 | reader: ReaderType, |
| 263 | diagnostics: ?*Options.Diagnostics, | 276 | diagnostics: ?*Diagnostics = null, |
| 264 | | 277 | |
| 265 | // buffers for heeader and file attributes | 278 | // buffers for heeader and file attributes |
| 266 | header_buffer: [Header.SIZE]u8, | 279 | header_buffer: [Header.SIZE]u8 = undefined, |
| 267 | file_name_buffer: []u8, | 280 | file_name_buffer: []u8, |
| 268 | link_name_buffer: []u8, | 281 | link_name_buffer: []u8, |
| 269 | | 282 | |
| 270 | // bytes of padding to the end of the block | 283 | // bytes of padding to the end of the block |
| 271 | padding: usize, | 284 | padding: usize = 0, |
| 272 | // current tar file | 285 | // not consumed bytes of file from last next iteration |
| 273 | file: File, | 286 | unread_file_bytes: u64 = 0, |
| 274 | | 287 | |
| 275 | pub const File = struct { | 288 | pub const File = struct { |
| 276 | name: []const u8, // name of file, symlink or directory | 289 | name: []const u8, // name of file, symlink or directory |
| 277 | link_name: []const u8, // target name of symlink | 290 | link_name: []const u8, // target name of symlink |
| 278 | size: u64, // size of the file in bytes | 291 | size: u64 = 0, // size of the file in bytes |
| 279 | mode: u32, | 292 | mode: u32 = 0, |
| 280 | kind: Header.Kind, | 293 | kind: FileKind = .file, |
| | 294 | |
| | 295 | unread_bytes: *u64, |
| | 296 | parent_reader: ReaderType, |
| | 297 | |
| | 298 | pub const Reader = std.io.Reader(File, ReaderType.Error, File.read); |
| 281 | | 299 | |
| 282 | reader: ReaderType, | 300 | pub fn reader(self: File) Reader { |
| | 301 | return .{ .context = self }; |
| | 302 | } |
| | 303 | |
| | 304 | pub fn read(self: File, dest: []u8) ReaderType.Error!usize { |
| | 305 | const buf = dest[0..@min(dest.len, self.unread_bytes.*)]; |
| | 306 | const n = try self.parent_reader.read(buf); |
| | 307 | self.unread_bytes.* -= n; |
| | 308 | return n; |
| | 309 | } |
| 283 | | 310 | |
| 284 | // Writes file content to writer. | 311 | // Writes file content to writer. |
| 285 | pub fn write(self: File, writer: anytype) !void { | 312 | pub fn writeAll(self: File, writer: anytype) !void { |
| 286 | var buffer: [4096]u8 = undefined; | 313 | var buffer: [4096]u8 = undefined; |
| 287 | | 314 | |
| 288 | var n: u64 = 0; | 315 | while (self.unread_bytes.* > 0) { |
| 289 | while (n < self.size) { | 316 | const buf = buffer[0..@min(buffer.len, self.unread_bytes.*)]; |
| 290 | const buf = buffer[0..@min(buffer.len, self.size - n)]; | 317 | try self.parent_reader.readNoEof(buf); |
| 291 | try self.reader.readNoEof(buf); | | |
| 292 | try writer.writeAll(buf); | 318 | try writer.writeAll(buf); |
| 293 | n += buf.len; | 319 | self.unread_bytes.* -= buf.len; |
| 294 | } | 320 | } |
| 295 | } | 321 | } |
| 296 | | | |
| 297 | // Skips file content. Advances reader. | | |
| 298 | pub fn skip(self: File) !void { | | |
| 299 | try self.reader.skipBytes(self.size, .{}); | | |
| 300 | } | | |
| 301 | }; | 322 | }; |
| 302 | | 323 | |
| 303 | const Self = @This(); | 324 | const Self = @This(); |
| ... | @@ -315,20 +336,18 @@ fn Iterator(comptime ReaderType: type) type { | ... | @@ -315,20 +336,18 @@ fn Iterator(comptime ReaderType: type) type { |
| 315 | } | 336 | } |
| 316 | | 337 | |
| 317 | fn readString(self: *Self, size: usize, buffer: []u8) ![]const u8 { | 338 | fn readString(self: *Self, size: usize, buffer: []u8) ![]const u8 { |
| 318 | if (size > buffer.len) return error.TarCorruptInput; | 339 | if (size > buffer.len) return error.TarInsufficientBuffer; |
| 319 | const buf = buffer[0..size]; | 340 | const buf = buffer[0..size]; |
| 320 | try self.reader.readNoEof(buf); | 341 | try self.reader.readNoEof(buf); |
| 321 | return nullStr(buf); | 342 | return nullStr(buf); |
| 322 | } | 343 | } |
| 323 | | 344 | |
| 324 | fn initFile(self: *Self) void { | 345 | fn newFile(self: *Self) File { |
| 325 | self.file = .{ | 346 | return .{ |
| 326 | .name = self.file_name_buffer[0..0], | 347 | .name = self.file_name_buffer[0..0], |
| 327 | .link_name = self.link_name_buffer[0..0], | 348 | .link_name = self.link_name_buffer[0..0], |
| 328 | .size = 0, | 349 | .parent_reader = self.reader, |
| 329 | .kind = .normal, | 350 | .unread_bytes = &self.unread_file_bytes, |
| 330 | .mode = 0, | | |
| 331 | .reader = self.reader, | | |
| 332 | }; | 351 | }; |
| 333 | } | 352 | } |
| 334 | | 353 | |
| ... | @@ -345,7 +364,12 @@ fn Iterator(comptime ReaderType: type) type { | ... | @@ -345,7 +364,12 @@ fn Iterator(comptime ReaderType: type) type { |
| 345 | /// loop iterates through one or more entries until it collects a all | 364 | /// loop iterates through one or more entries until it collects a all |
| 346 | /// file attributes. | 365 | /// file attributes. |
| 347 | pub fn next(self: *Self) !?File { | 366 | pub fn next(self: *Self) !?File { |
| 348 | self.initFile(); | 367 | if (self.unread_file_bytes > 0) { |
| | 368 | // If file content was not consumed by caller |
| | 369 | try self.reader.skipBytes(self.unread_file_bytes, .{}); |
| | 370 | self.unread_file_bytes = 0; |
| | 371 | } |
| | 372 | var file: File = self.newFile(); |
| 349 | | 373 | |
| 350 | while (try self.readHeader()) |header| { | 374 | while (try self.readHeader()) |header| { |
| 351 | const kind = header.kind(); | 375 | const kind = header.kind(); |
| ... | @@ -355,46 +379,52 @@ fn Iterator(comptime ReaderType: type) type { | ... | @@ -355,46 +379,52 @@ fn Iterator(comptime ReaderType: type) type { |
| 355 | switch (kind) { | 379 | switch (kind) { |
| 356 | // File types to retrun upstream | 380 | // File types to retrun upstream |
| 357 | .directory, .normal, .symbolic_link => { | 381 | .directory, .normal, .symbolic_link => { |
| 358 | self.file.kind = kind; | 382 | file.kind = switch (kind) { |
| 359 | self.file.mode = try header.mode(); | 383 | .directory => .directory, |
| | 384 | .normal => .file, |
| | 385 | .symbolic_link => .sym_link, |
| | 386 | else => unreachable, |
| | 387 | }; |
| | 388 | file.mode = try header.mode(); |
| 360 | | 389 | |
| 361 | // set file attributes if not already set by prefix/extended headers | 390 | // set file attributes if not already set by prefix/extended headers |
| 362 | if (self.file.size == 0) { | 391 | if (file.size == 0) { |
| 363 | self.file.size = size; | 392 | file.size = size; |
| 364 | } | 393 | } |
| 365 | if (self.file.link_name.len == 0) { | 394 | if (file.link_name.len == 0) { |
| 366 | self.file.link_name = header.linkName(self.link_name_buffer[0..Header.LINK_NAME_SIZE]); | 395 | file.link_name = try header.linkName(self.link_name_buffer); |
| 367 | } | 396 | } |
| 368 | if (self.file.name.len == 0) { | 397 | if (file.name.len == 0) { |
| 369 | self.file.name = try header.fullName(self.file_name_buffer[0..Header.MAX_NAME_SIZE]); | 398 | file.name = try header.fullName(self.file_name_buffer); |
| 370 | } | 399 | } |
| 371 | | 400 | |
| 372 | self.padding = blockPadding(self.file.size); | 401 | self.padding = blockPadding(file.size); |
| 373 | return self.file; | 402 | self.unread_file_bytes = file.size; |
| | 403 | return file; |
| 374 | }, | 404 | }, |
| 375 | // Prefix header types | 405 | // Prefix header types |
| 376 | .gnu_long_name => { | 406 | .gnu_long_name => { |
| 377 | self.file.name = try self.readString(@intCast(size), self.file_name_buffer); | 407 | file.name = try self.readString(@intCast(size), self.file_name_buffer); |
| 378 | }, | 408 | }, |
| 379 | .gnu_long_link => { | 409 | .gnu_long_link => { |
| 380 | self.file.link_name = try self.readString(@intCast(size), self.link_name_buffer); | 410 | file.link_name = try self.readString(@intCast(size), self.link_name_buffer); |
| 381 | }, | 411 | }, |
| 382 | .extended_header => { | 412 | .extended_header => { |
| 383 | // Use just attributes from last extended header. | 413 | // Use just attributes from last extended header. |
| 384 | self.initFile(); | 414 | file = self.newFile(); |
| 385 | | 415 | |
| 386 | var rdr = paxIterator(self.reader, @intCast(size)); | 416 | var rdr = paxIterator(self.reader, @intCast(size)); |
| 387 | while (try rdr.next()) |attr| { | 417 | while (try rdr.next()) |attr| { |
| 388 | switch (attr.kind) { | 418 | switch (attr.kind) { |
| 389 | .path => { | 419 | .path => { |
| 390 | self.file.name = try attr.value(self.file_name_buffer); | 420 | file.name = try attr.value(self.file_name_buffer); |
| 391 | }, | 421 | }, |
| 392 | .linkpath => { | 422 | .linkpath => { |
| 393 | self.file.link_name = try attr.value(self.link_name_buffer); | 423 | file.link_name = try attr.value(self.link_name_buffer); |
| 394 | }, | 424 | }, |
| 395 | .size => { | 425 | .size => { |
| 396 | var buf: [pax_max_size_attr_len]u8 = undefined; | 426 | var buf: [pax_max_size_attr_len]u8 = undefined; |
| 397 | self.file.size = try std.fmt.parseInt(u64, try attr.value(&buf), 10); | 427 | file.size = try std.fmt.parseInt(u64, try attr.value(&buf), 10); |
| 398 | }, | 428 | }, |
| 399 | } | 429 | } |
| 400 | } | 430 | } |
| ... | @@ -467,7 +497,8 @@ fn PaxIterator(comptime ReaderType: type) type { | ... | @@ -467,7 +497,8 @@ fn PaxIterator(comptime ReaderType: type) type { |
| 467 | // Copies pax attribute value into destination buffer. | 497 | // Copies pax attribute value into destination buffer. |
| 468 | // Must be called with destination buffer of size at least Attribute.len. | 498 | // Must be called with destination buffer of size at least Attribute.len. |
| 469 | pub fn value(self: Attribute, dst: []u8) ![]const u8 { | 499 | pub fn value(self: Attribute, dst: []u8) ![]const u8 { |
| 470 | assert(self.len <= dst.len); | 500 | if (self.len > dst.len) return error.TarInsufficientBuffer; |
| | 501 | // assert(self.len <= dst.len); |
| 471 | const buf = dst[0..self.len]; | 502 | const buf = dst[0..self.len]; |
| 472 | const n = try self.reader.readAll(buf); | 503 | const n = try self.reader.readAll(buf); |
| 473 | if (n < self.len) return error.UnexpectedEndOfStream; | 504 | if (n < self.len) return error.UnexpectedEndOfStream; |
| ... | @@ -540,7 +571,8 @@ fn PaxIterator(comptime ReaderType: type) type { | ... | @@ -540,7 +571,8 @@ fn PaxIterator(comptime ReaderType: type) type { |
| 540 | }; | 571 | }; |
| 541 | } | 572 | } |
| 542 | | 573 | |
| 543 | pub fn pipeToFileSystem(dir: std.fs.Dir, reader: anytype, options: Options) !void { | 574 | /// Saves tar file content to the file systems. |
| | 575 | pub fn pipeToFileSystem(dir: std.fs.Dir, reader: anytype, options: PipeOptions) !void { |
| 544 | switch (options.mode_mode) { | 576 | switch (options.mode_mode) { |
| 545 | .ignore => {}, | 577 | .ignore => {}, |
| 546 | .executable_bit_only => { | 578 | .executable_bit_only => { |
| ... | @@ -568,24 +600,23 @@ pub fn pipeToFileSystem(dir: std.fs.Dir, reader: anytype, options: Options) !voi | ... | @@ -568,24 +600,23 @@ pub fn pipeToFileSystem(dir: std.fs.Dir, reader: anytype, options: Options) !voi |
| 568 | try dir.makePath(file_name); | 600 | try dir.makePath(file_name); |
| 569 | } | 601 | } |
| 570 | }, | 602 | }, |
| 571 | .normal => { | 603 | .file => { |
| 572 | if (file.size == 0 and file.name.len == 0) return; | 604 | if (file.size == 0 and file.name.len == 0) return; |
| 573 | const file_name = stripComponents(file.name, options.strip_components); | 605 | const file_name = stripComponents(file.name, options.strip_components); |
| 574 | if (file_name.len == 0) return error.BadFileName; | 606 | if (file_name.len == 0) return error.BadFileName; |
| 575 | | 607 | |
| 576 | if (createDirAndFile(dir, file_name)) |fs_file| { | 608 | if (createDirAndFile(dir, file_name)) |fs_file| { |
| 577 | defer fs_file.close(); | 609 | defer fs_file.close(); |
| 578 | try file.write(fs_file); | 610 | try file.writeAll(fs_file); |
| 579 | } else |err| { | 611 | } else |err| { |
| 580 | const d = options.diagnostics orelse return err; | 612 | const d = options.diagnostics orelse return err; |
| 581 | try d.errors.append(d.allocator, .{ .unable_to_create_file = .{ | 613 | try d.errors.append(d.allocator, .{ .unable_to_create_file = .{ |
| 582 | .code = err, | 614 | .code = err, |
| 583 | .file_name = try d.allocator.dupe(u8, file_name), | 615 | .file_name = try d.allocator.dupe(u8, file_name), |
| 584 | } }); | 616 | } }); |
| 585 | try file.skip(); | | |
| 586 | } | 617 | } |
| 587 | }, | 618 | }, |
| 588 | .symbolic_link => { | 619 | .sym_link => { |
| 589 | // The file system path of the symbolic link. | 620 | // The file system path of the symbolic link. |
| 590 | const file_name = stripComponents(file.name, options.strip_components); | 621 | const file_name = stripComponents(file.name, options.strip_components); |
| 591 | if (file_name.len == 0) return error.BadFileName; | 622 | if (file_name.len == 0) return error.BadFileName; |
| ... | @@ -601,7 +632,6 @@ pub fn pipeToFileSystem(dir: std.fs.Dir, reader: anytype, options: Options) !voi | ... | @@ -601,7 +632,6 @@ pub fn pipeToFileSystem(dir: std.fs.Dir, reader: anytype, options: Options) !voi |
| 601 | } }); | 632 | } }); |
| 602 | }; | 633 | }; |
| 603 | }, | 634 | }, |
| 604 | else => unreachable, | | |
| 605 | } | 635 | } |
| 606 | } | 636 | } |
| 607 | } | 637 | } |
| ... | @@ -619,6 +649,7 @@ fn createDirAndFile(dir: std.fs.Dir, file_name: []const u8) !std.fs.File { | ... | @@ -619,6 +649,7 @@ fn createDirAndFile(dir: std.fs.Dir, file_name: []const u8) !std.fs.File { |
| 619 | return fs_file; | 649 | return fs_file; |
| 620 | } | 650 | } |
| 621 | | 651 | |
| | 652 | // Creates a symbolic link at path `file_name` which points to `link_name`. |
| 622 | fn createDirAndSymlink(dir: std.fs.Dir, link_name: []const u8, file_name: []const u8) !void { | 653 | fn createDirAndSymlink(dir: std.fs.Dir, link_name: []const u8, file_name: []const u8) !void { |
| 623 | dir.symLink(link_name, file_name, .{}) catch |err| { | 654 | dir.symLink(link_name, file_name, .{}) catch |err| { |
| 624 | if (err == error.FileNotFound) { | 655 | if (err == error.FileNotFound) { |
| ... | @@ -645,8 +676,8 @@ fn stripComponents(path: []const u8, count: u32) []const u8 { | ... | @@ -645,8 +676,8 @@ fn stripComponents(path: []const u8, count: u32) []const u8 { |
| 645 | return path[i..]; | 676 | return path[i..]; |
| 646 | } | 677 | } |
| 647 | | 678 | |
| 648 | test "tar stripComponents" { | 679 | test "stripComponents" { |
| 649 | const expectEqualStrings = std.testing.expectEqualStrings; | 680 | const expectEqualStrings = testing.expectEqualStrings; |
| 650 | try expectEqualStrings("a/b/c", stripComponents("a/b/c", 0)); | 681 | try expectEqualStrings("a/b/c", stripComponents("a/b/c", 0)); |
| 651 | try expectEqualStrings("b/c", stripComponents("a/b/c", 1)); | 682 | try expectEqualStrings("b/c", stripComponents("a/b/c", 1)); |
| 652 | try expectEqualStrings("c", stripComponents("a/b/c", 2)); | 683 | try expectEqualStrings("c", stripComponents("a/b/c", 2)); |
| ... | @@ -654,7 +685,7 @@ test "tar stripComponents" { | ... | @@ -654,7 +685,7 @@ test "tar stripComponents" { |
| 654 | try expectEqualStrings("", stripComponents("a/b/c", 4)); | 685 | try expectEqualStrings("", stripComponents("a/b/c", 4)); |
| 655 | } | 686 | } |
| 656 | | 687 | |
| 657 | test "tar PaxIterator" { | 688 | test "PaxIterator" { |
| 658 | const Attr = struct { | 689 | const Attr = struct { |
| 659 | kind: PaxAttributeKind, | 690 | kind: PaxAttributeKind, |
| 660 | value: []const u8 = undefined, | 691 | value: []const u8 = undefined, |
| ... | @@ -757,24 +788,24 @@ test "tar PaxIterator" { | ... | @@ -757,24 +788,24 @@ test "tar PaxIterator" { |
| 757 | var i: usize = 0; | 788 | var i: usize = 0; |
| 758 | while (iter.next() catch |err| { | 789 | while (iter.next() catch |err| { |
| 759 | if (case.err) |e| { | 790 | if (case.err) |e| { |
| 760 | try std.testing.expectEqual(e, err); | 791 | try testing.expectEqual(e, err); |
| 761 | continue; | 792 | continue; |
| 762 | } | 793 | } |
| 763 | return err; | 794 | return err; |
| 764 | }) |attr| : (i += 1) { | 795 | }) |attr| : (i += 1) { |
| 765 | const exp = case.attrs[i]; | 796 | const exp = case.attrs[i]; |
| 766 | try std.testing.expectEqual(exp.kind, attr.kind); | 797 | try testing.expectEqual(exp.kind, attr.kind); |
| 767 | const value = attr.value(&buffer) catch |err| { | 798 | const value = attr.value(&buffer) catch |err| { |
| 768 | if (exp.err) |e| { | 799 | if (exp.err) |e| { |
| 769 | try std.testing.expectEqual(e, err); | 800 | try testing.expectEqual(e, err); |
| 770 | break :outer; | 801 | break :outer; |
| 771 | } | 802 | } |
| 772 | return err; | 803 | return err; |
| 773 | }; | 804 | }; |
| 774 | try std.testing.expectEqualStrings(exp.value, value); | 805 | try testing.expectEqualStrings(exp.value, value); |
| 775 | } | 806 | } |
| 776 | try std.testing.expectEqual(case.attrs.len, i); | 807 | try testing.expectEqual(case.attrs.len, i); |
| 777 | try std.testing.expect(case.err == null); | 808 | try testing.expect(case.err == null); |
| 778 | } | 809 | } |
| 779 | } | 810 | } |
| 780 | | 811 | |
| ... | @@ -782,7 +813,7 @@ test { | ... | @@ -782,7 +813,7 @@ test { |
| 782 | _ = @import("tar/test.zig"); | 813 | _ = @import("tar/test.zig"); |
| 783 | } | 814 | } |
| 784 | | 815 | |
| 785 | test "tar header parse size" { | 816 | test "header parse size" { |
| 786 | const cases = [_]struct { | 817 | const cases = [_]struct { |
| 787 | in: []const u8, | 818 | in: []const u8, |
| 788 | want: u64 = 0, | 819 | want: u64 = 0, |
| ... | @@ -810,14 +841,14 @@ test "tar header parse size" { | ... | @@ -810,14 +841,14 @@ test "tar header parse size" { |
| 810 | @memcpy(bytes[124 .. 124 + case.in.len], case.in); | 841 | @memcpy(bytes[124 .. 124 + case.in.len], case.in); |
| 811 | var header = Header{ .bytes = &bytes }; | 842 | var header = Header{ .bytes = &bytes }; |
| 812 | if (case.err) |err| { | 843 | if (case.err) |err| { |
| 813 | try std.testing.expectError(err, header.size()); | 844 | try testing.expectError(err, header.size()); |
| 814 | } else { | 845 | } else { |
| 815 | try std.testing.expectEqual(case.want, try header.size()); | 846 | try testing.expectEqual(case.want, try header.size()); |
| 816 | } | 847 | } |
| 817 | } | 848 | } |
| 818 | } | 849 | } |
| 819 | | 850 | |
| 820 | test "tar header parse mode" { | 851 | test "header parse mode" { |
| 821 | const cases = [_]struct { | 852 | const cases = [_]struct { |
| 822 | in: []const u8, | 853 | in: []const u8, |
| 823 | want: u64 = 0, | 854 | want: u64 = 0, |
| ... | @@ -835,9 +866,148 @@ test "tar header parse mode" { | ... | @@ -835,9 +866,148 @@ test "tar header parse mode" { |
| 835 | @memcpy(bytes[100 .. 100 + case.in.len], case.in); | 866 | @memcpy(bytes[100 .. 100 + case.in.len], case.in); |
| 836 | var header = Header{ .bytes = &bytes }; | 867 | var header = Header{ .bytes = &bytes }; |
| 837 | if (case.err) |err| { | 868 | if (case.err) |err| { |
| 838 | try std.testing.expectError(err, header.mode()); | 869 | try testing.expectError(err, header.mode()); |
| 839 | } else { | 870 | } else { |
| 840 | try std.testing.expectEqual(case.want, try header.mode()); | 871 | try testing.expectEqual(case.want, try header.mode()); |
| | 872 | } |
| | 873 | } |
| | 874 | } |
| | 875 | |
| | 876 | test "create file and symlink" { |
| | 877 | var root = testing.tmpDir(.{}); |
| | 878 | defer root.cleanup(); |
| | 879 | |
| | 880 | var file = try createDirAndFile(root.dir, "file1"); |
| | 881 | file.close(); |
| | 882 | file = try createDirAndFile(root.dir, "a/b/c/file2"); |
| | 883 | file.close(); |
| | 884 | |
| | 885 | createDirAndSymlink(root.dir, "a/b/c/file2", "symlink1") catch |err| { |
| | 886 | // On Windows when developer mode is not enabled |
| | 887 | if (err == error.AccessDenied) return error.SkipZigTest; |
| | 888 | return err; |
| | 889 | }; |
| | 890 | try createDirAndSymlink(root.dir, "../../../file1", "d/e/f/symlink2"); |
| | 891 | |
| | 892 | // Danglink symlnik, file created later |
| | 893 | try createDirAndSymlink(root.dir, "../../../g/h/i/file4", "j/k/l/symlink3"); |
| | 894 | file = try createDirAndFile(root.dir, "g/h/i/file4"); |
| | 895 | file.close(); |
| | 896 | } |
| | 897 | |
| | 898 | test iterator { |
| | 899 | // Example tar file is created from this tree structure: |
| | 900 | // $ tree example |
| | 901 | // example |
| | 902 | // ├── a |
| | 903 | // │   └── file |
| | 904 | // ├── b |
| | 905 | // │   └── symlink -> ../a/file |
| | 906 | // └── empty |
| | 907 | // $ cat example/a/file |
| | 908 | // content |
| | 909 | // $ tar -cf example.tar example |
| | 910 | // $ tar -tvf example.tar |
| | 911 | // example/ |
| | 912 | // example/b/ |
| | 913 | // example/b/symlink -> ../a/file |
| | 914 | // example/a/ |
| | 915 | // example/a/file |
| | 916 | // example/empty/ |
| | 917 | |
| | 918 | const data = @embedFile("tar/testdata/example.tar"); |
| | 919 | var fbs = std.io.fixedBufferStream(data); |
| | 920 | |
| | 921 | // User provided buffers to the iterator |
| | 922 | var file_name_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined; |
| | 923 | var link_name_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined; |
| | 924 | // Create iterator |
| | 925 | var iter = iterator(fbs.reader(), .{ |
| | 926 | .file_name_buffer = &file_name_buffer, |
| | 927 | .link_name_buffer = &link_name_buffer, |
| | 928 | }); |
| | 929 | // Iterate over files in example.tar |
| | 930 | var file_no: usize = 0; |
| | 931 | while (try iter.next()) |file| : (file_no += 1) { |
| | 932 | switch (file.kind) { |
| | 933 | .directory => { |
| | 934 | switch (file_no) { |
| | 935 | 0 => try testing.expectEqualStrings("example/", file.name), |
| | 936 | 1 => try testing.expectEqualStrings("example/b/", file.name), |
| | 937 | 3 => try testing.expectEqualStrings("example/a/", file.name), |
| | 938 | 5 => try testing.expectEqualStrings("example/empty/", file.name), |
| | 939 | else => unreachable, |
| | 940 | } |
| | 941 | }, |
| | 942 | .file => { |
| | 943 | try testing.expectEqualStrings("example/a/file", file.name); |
| | 944 | // Read file content |
| | 945 | var buf: [16]u8 = undefined; |
| | 946 | const n = try file.reader().readAll(&buf); |
| | 947 | try testing.expectEqualStrings("content\n", buf[0..n]); |
| | 948 | }, |
| | 949 | .sym_link => { |
| | 950 | try testing.expectEqualStrings("example/b/symlink", file.name); |
| | 951 | try testing.expectEqualStrings("../a/file", file.link_name); |
| | 952 | }, |
| 841 | } | 953 | } |
| 842 | } | 954 | } |
| 843 | } | 955 | } |
| | 956 | |
| | 957 | test pipeToFileSystem { |
| | 958 | // Example tar file is created from this tree structure: |
| | 959 | // $ tree example |
| | 960 | // example |
| | 961 | // ├── a |
| | 962 | // │   └── file |
| | 963 | // ├── b |
| | 964 | // │   └── symlink -> ../a/file |
| | 965 | // └── empty |
| | 966 | // $ cat example/a/file |
| | 967 | // content |
| | 968 | // $ tar -cf example.tar example |
| | 969 | // $ tar -tvf example.tar |
| | 970 | // example/ |
| | 971 | // example/b/ |
| | 972 | // example/b/symlink -> ../a/file |
| | 973 | // example/a/ |
| | 974 | // example/a/file |
| | 975 | // example/empty/ |
| | 976 | |
| | 977 | const data = @embedFile("tar/testdata/example.tar"); |
| | 978 | var fbs = std.io.fixedBufferStream(data); |
| | 979 | const reader = fbs.reader(); |
| | 980 | |
| | 981 | var tmp = testing.tmpDir(.{ .no_follow = true }); |
| | 982 | defer tmp.cleanup(); |
| | 983 | const dir = tmp.dir; |
| | 984 | |
| | 985 | // Save tar from `reader` to the file system `dir` |
| | 986 | pipeToFileSystem(dir, reader, .{ |
| | 987 | .mode_mode = .ignore, |
| | 988 | .strip_components = 1, |
| | 989 | .exclude_empty_directories = true, |
| | 990 | }) catch |err| { |
| | 991 | // Skip on platform which don't support symlinks |
| | 992 | if (err == error.UnableToCreateSymLink) return error.SkipZigTest; |
| | 993 | return err; |
| | 994 | }; |
| | 995 | |
| | 996 | try testing.expectError(error.FileNotFound, dir.statFile("empty")); |
| | 997 | try testing.expect((try dir.statFile("a/file")).kind == .file); |
| | 998 | try testing.expect((try dir.statFile("b/symlink")).kind == .file); // statFile follows symlink |
| | 999 | |
| | 1000 | var buf: [32]u8 = undefined; |
| | 1001 | try testing.expectEqualSlices( |
| | 1002 | u8, |
| | 1003 | "../a/file", |
| | 1004 | normalizePath(try dir.readLink("b/symlink", &buf)), |
| | 1005 | ); |
| | 1006 | } |
| | 1007 | |
| | 1008 | fn normalizePath(bytes: []u8) []u8 { |
| | 1009 | const canonical_sep = std.fs.path.sep_posix; |
| | 1010 | if (std.fs.path.sep == canonical_sep) return bytes; |
| | 1011 | std.mem.replaceScalar(u8, bytes, std.fs.path.sep, canonical_sep); |
| | 1012 | return bytes; |
| | 1013 | } |