联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> C++C++

日期:2020-10-08 11:44

Tar (tape archive) File Manipulation

Learning Objectives

Upon completion of this assignment, you should be able:

1. To read and understand directory entries and file attributes;

2. To do complex file input/output and file manipulation operations.

The mechanisms you will practice using include:

? Buffered I/O: fopen(), fclose(), fread(), fwrite(), fseek()

? Directory entries: opendir(), readdir(), closedir()

? File metadata: stat(), chmod(), utimes()

? Hard links: link()

Program Specification

NAME

mytar – create and manipulate tape archives

SYNOPSIS

mytar [cxtf:] [file]filename

DESCRIPTION

mytar creates an archive from of a directory tree, extracts files from an archive, or prints

the contents and details of an archive file.

Options followed by a ‘:’ expect a subsequent parameter. The options1 are as follows:

-c

create an archive of the given directory tree. A directory name must be specified.

-x

extract the directory tree contained in the specified archive

-t

print the contents of the specified archive

-f:

the subsequent argument is the name of the archive file (to create, extract or print).

This option must always be specified.

EXIT STATUS

mytar exits 0 on success and -1 on failure.

ERRORS

Upon error, mytar exits after printing to the standard error stream an appropriate message

using one of the format strings below:

"Error: No tarfile specified.\n"

1 Options followed by a ‘:’ expect a subsequent parameter.

2

"Error: Specified target (\"%s\") does not exist.\n"

"Error: Specified target (\"%s\") is not a directory.\n"

"Error: No directory target specified.\n"

"Error: Bad magic number (%d), should be: %d.\n"

Additionally, if a library or system call fails, mytar calls perror() with the name of the

failing routine then exits.

EXIT STATUS

mytar exits with 0 on success and -1 on failure.

EXAMPLES

mytar -c -f a.tar a

create an archive, a.tar, containing all files in the directory tree, a

mytar -x -f a.tar

extract the files in the archive, a.tar

mytar -t -f a.tar

print the details of all files in the archive, a.tar

mytar -c a

Error: No tarfile specified.

Implementation Details

mytar File Format

mytar archives can be read or printed by any other program that observes the proper format2.

The mytar format specification follows: first, a magic number that identifies mytar archives.

Then for each file in the archive, the archive contains in order: inode, filename length, filename,

mode, modification time, and for regular files, file size and file content.

Size Content Notes

4 bytes 0x7261746D magic number, once per archive

8 bytes file inode number

4 bytes file name length

n bytes file name n is file name length

4 bytes file mode

8 bytes file modification time in seconds

8 bytes file size only for regular (not directory) files

n bytes file content n is file size;

only for regular (not directory) files

2 When transferring tar files between heterogeneous platforms, endianness may be an issue.

3

(As discussed below, hard links to files already in the archive only have an inode number, file

name length and file name.)

Printing

In print mode, mytar uses the following formatted print statements:

For directories: "inode: %llu, mode: %d, mtime: %llu, name: %s/\n"

For non-executable files: "inode: %llu, mode: %d, mtime: %llu, size: %llu, name: %s\n"

For executable files: "inode: %llu, mode: %d, mtime: %llu, size: %llu, name: %s*\n"

Requirements and Constraints

1. In the archive, directories should appear before any contained files or directories.

2. Your archive must specify file and directory paths relative to the specified directory: you

should not use absolute paths.

3. Close files immediately when you are finished reading or writing them.

4. Call closedir() immediately when you are finished using a directory stream opened

with opendir().

5. Contents should appear in the archive in the order returned by readdir().

6. Directories should be traversed in a depth-first manner.

7. You must track file inodes to exclude redundant information when encountering hard

links that reference the same inode. For redundant hard links, only include the filename

(and length) and inode number in the archive.

8. mytar must re-establish the original mode and modification times for extracted files.

Notes

1. mytar should ignore symbolic links, ‘.’ and ‘..’ directory entries

2. When you extract files and directories from an archive, there is no (portable) way to

retain the original inode values.


版权所有:留学生程序网 2020 All Rights Reserved 联系方式:QQ:99515681 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。