Отправляет email-рассылки с помощью сервиса Sendsay
  Все выпуски  

Программирование, просто о сложном


Программирование

Просто о сложном

Начинаем разработку показательного проекта

Здравствуйте, уважаемые читатели.

Давненько мы с вами не общались. А я вот все думал, чтоб такое взять в качестве задания для проекта. Не хотелось нагнетать сложную теорию, но и студенческие лабораторные работы тоже не годились. И тут из задворков памяти пришло воспоминание о тестовом задании в одну из хороших контор. Так что мы будем моделировать с вами файловую систему. Постановку задачи прилагаю в том виде, в котором я ее получил, т.е. на английском языке :)

В дальнейших выпусках рассылки будет описан процесс создания программы. Но я могу выкладывать и работающий код тоже. Тут все зависит от интереса читателей.
С постановкой задачи можете ознакомиться, если хотите подумать, как бы вы реализовывали это задание.

Для того, чтобы понимать новую терминологию настоятельно рекомендую изучить материал по методологии SCRUM потому как в следующем выпуске мы будем планировать наши задания.

С уважением,
Сергей.

Постановка задачи

File Manager Emulator

File Manager Emulator (FME) emulates the details of creating, removing, copying and moving files and directories. It also handles commands related to these file management tasks. FME shall be capable to read and execute a batch file with different kind of commands. After the batch file execution it shall generate and print out formatted directory structure or an error message if something went wrong to standard output. Note that program should do nothing with the real file structure on local hard drives and shall only emulate these activities. Your goal is to write such File Manager Emulator.

FME Commands

  • MD – creates a directory.
    Command format: MD [drive:]path
    Notes: MD should not create any intermediate directories in the path.
    Examples:
    a) MD C:\Test – creates a directory called Test\ in the root directory.
    b) MD Test – creates a subdirectory called Test\ in the current directory.
    c) MD C:\Dir1\Dir2\NewDir – creates a subdirectory “NewDir” if directory “C:\Dir1\Dir2” exists.
  • CD – changes the current directory.
    Command format: CD [drive:][path]
    Note that using CD without parameters is not allowed.
    Examples:
    a) CD C: – set root as the current directory.
    b) CD C:\Dir1 – set “C:\Dir1” as the current directory.
    c) CD Dir1 – set Dir1 subdirectory of the current directory as new current directory.
  • RD – removes a directory if it is empty (doesn’t contain any files or subdirectories).
    Command format: RD [drive:]path
    Notes: It is not allowed to delete the current directory in such way.
    Examples:
    RD Dir2 – remove subdirectory “Dir2”.
  • DELTREE – removes a directory with all its subdirectories.
    Command format: DELTREE [drive:]path
    Note that you can’t remove a directory that contains current directory as one of its subdirectories.
    See notes 3 and 4 also.
  • MF – creates a file.
    Command format: MF [drive:]path
    Notes: If such file already exists with the given path then FME should continue to the next
    command in the batch file without any error rising.
    Examples:
    MF Dir2\Dir3\file.txt – create a file named file.txt in the current directory’s “Dir2\Dir3” subdirectory.
  • MHL – creates a hard link to a file/directory and places it in given location.
    Command format: MHL [drive:]source [drive:]destination
    Notes: Destination should contain only path without any file name. Actual link name will be created as mentioned in note 10. If such link already exists then FME should continue to the next command in the batch file without any error rising.
    Examples: a) MHL C:\Dir2\Dir3\file.txt C:\Dir4 – create a hard link to the file “file.txt” and place it into the directory “C:\Dir4”
    b) MHL C:\Dir1\Dir4 C:\Dir2 – create a hard link to the directory “C:\Dir1\Dir4” and place it into the directory “C:\Dir2”
  • MDL – creates a dynamic link to a file/directory and places it in given location.
    Command format: MDL [drive:]source [drive:]destination
    Notes: Destination should contain only path without any file name. Actual link name will be created as mentioned in note 10. If such link already exists then FME should continue to the next command in the batch file without any error rising.
  • DEL – removes a file or link.
    Command format: DEL [drive:]path
    Notes: See notes 3 and 4.
    Examples:
    DEL C:\Dir2\Dir3\file.txt – removes a file “file.txt” form a directory “C:\Dir2\Dir3”.
  • COPY – copy an existed directory/file/link to another location.
    Command format: COPY [drive:]source [drive:]destination
    Notes: Program should copy directory with all its content. Destination path should not contain any file name otherwise FME should raise error.
    Examples:
    a) COPY Dir2\Dir3\ C:\Dir1 – copy the current directory’s subdirectory Dir2\Dir3\ into C:\Dir1.
    b) COPY Dir2\Dir3\file.txt C:\Dir1 – copy a file “file.txt” form current directory’s subdirectory Dir2\Dir3\ into C:\Dir1.
    c) COPY Dir2\Dir3\file.txt C:\Dir1\newfile.txt – is illegal!
  • MOVE – move an existing directory/file/link to another location
    Command format: MOVE [drive:]source [drive:]destination
    Notes: Program should move directory with all its content. In case when a file or directory which is being moved has a hard link, FME should terminate the MOVE operation and batch file execution.
    In case when any dynamic link(s) found and no hard link exists, then dynamic link(s) should be modified and contain new location information instead of the old one.
    Examples:
    a) Let suppose that we have the following directories structure –
    C:
    |_DIR1
    | |_DIR2
    | | |_DIR3
    | | |_readme.txt
    | |
    | |_EDIR4
    | | |_ hlink[C:\DIR1\DIR2\DIR3\readme.txt]
    | |
    | |_GDIR5
    | | |_ dlink[C:\DIR1\DIR2\DIR3\readme.txt]
    And FME found command MOVE C:\DIR1\DIR2\DIR3 C:\DIR1\EDIR4 in a batch file.
    Following to our requirements FME should terminate command execution because of the hard link hlink[C:\DIR1\DIR2\DIR3\readme.txt] existence.
    b) Let suppose that we have a little bit different directories structure –
    C:
    |_DIR1
    | |_DIR2
    | | |_DIR3
    | | |_readme.txt
    | |
    | |_EDIR4
    | |
    | |_GDIR5
    | | |_ dlink[C:\DIR1\DIR2\DIR3\readme.txt]
    After MOVE C:\DIR1\DIR2\DIR3 C:\DIR1\EDIR4 command execution the directories structure and dlink will be changed by the following way.
    C:
    |_DIR1
    | |_DIR2
    | |
    | |_EDIR4
    | | |_DIR3
    | | |_readme.txt
    | |
    | |_GDIR5
    | | |_ dlink[C:\DIR1\EDIR4\DIR3\readme.txt]

Additional Implementation Notes:

  1. Initially file system contains only the root directory marked as “C:”
  2. Commands, file and directory names are case insensitive. Cd, CD, Cd, cD does mean the same thing.
  3. One can not delete a file which has an attached hard link but can delete a dynamic link.
  4. When a file is deleted its all dynamic links are also should be deleted. If a file has both hard and dynamic links FME should keep them all unchanged.
  5. Files and directories naming conventions shall be the following
    • File or directory names shall not exceed 8 characters length.
    • File extension shall not exceed 3 characters length.
    • Only alphabetical [a…z] and numerical [0…9] characters allowed in the file or directory names and extensions.
  6. Any action beside cd shall not change the current directory.
  7. If a path doesn’t contain “C:\” prefix then FME shall perform actions in the current directory.
  8. In case of any error occurs, the program shall stop and output a descriptive error message.
  9. The output shall be organized by the manner given below. The output should be organized in alphabetical ascending order.
  10. The output format for dynamic and hard links should be the following: hlink[ full path ] for hard links and dlink[ full path ] respectively.
  11. It is not allowed to create links to links.
    C:
    |_DIR1
    | |_DIR2
    | | |_DIR3
    | | |_readme.txt
    | |
    | |_EDIR4
    | | |_temp.dat
    | |
    | |_GDIR5
    | | |_hlink[C:\Dir1\EDir\4temp.dat]

Simple Test Case
Simple input:
MD Dir1
MD Dir1\Dir2
CD Dir1
MD EDir4
MD GDir5
MD Dir2\Dir3
MF Dir2\Dir3\readme.txt
CD EDir4
MF temp.dat
Output:
C:
|_DIR1
| |_DIR2
| | |_DIR3
| | |_readme.txt
| |
| |_EDIR4
| | |_temp.dat
| |
| |_GDIR5

Анонсы будущих рассылок


В избранное