Code Monkey home page Code Monkey logo

autosavebackup_proto's Introduction

AutosaveBackup_Proto

This is a command-line program that watches a folder for a specified file and backs up that file whenever it is created or modified. Currently, it is designed to work only on a Windows OS.

Usage

Download AutosaveBackup_Proto.jar from the releases page. Then open a command prompt, navigate to the folder you downloaded the jar to and type

java -jar AutosaveBackup.jar [path to file]

Where [path to file] is the absolute path to the file you want backed up, such as c:\save folder\save.sav. Do not surround the file path with quotation marks.

When you want to stop watching for the file, type "quit" in the command prompt and then hit enter. Closing the command prompt will also stop the program.

Classes

AutosaveBackup

This is the main class. It parses the command line arguments and then launches two new program threads: one which uses the Watcher class to monitor the specified folder, and one which uses the ConsoleInputTask to wait for the user to ask the program to end by typing "quit" into the console.

It passes a CountDownLatch instance (with a count of 1) to both of these threads and then waits for the CountDownLatch to be signalled. Whichever thread completes first will call countDown() on the CountDownLatch. This prompts AutosaveBackup to shut down the remaining thread and exit.

ConsoleInputTask

This class opens a BufferedReader to listen for input from the console the program was opened from. It runs in a separate thread to prevent blocking the rest of the program.

It calls the BufferedReader ready() method to check if anything has been typed into the console. If nothing was typed, it sleeps for 1 second before calling ready() again, repeating until something is typed into the console. If there is input to the console matching the string "quit," it will signal the main thread using the CountDownLatch and then return.

Watcher

Starts a new WatchService (package java.nio.file) which watches the folder for ENTRY_MODIFY filesystem events.

ENTRY_MODIFY events occur whenever an entry in the folder is modified. In the case of file creation, the filesystem would send an ENTRY_CREATE event followed by one or more ENTRY_MODIFY events as data is written to the new file. Thus, the Watcher will be notified of the creation of a new file provided something is also written to the file at that time.

When a modification event is detected, the Watcher checks the modified file and confirms whether it has the correct filename. If it does, the Watcher performs a backup using the Backup class.

The Watcher continues to monitor the folder unless an IOException occurs. This could happen, for example, if the folder is deleted. If this occurs, the Watcher catches the resulting IOException, signals the CountDownLatch, then rethrows the exception so it is available to the main thread. The main thread would then log the exception and shut down the ConsoleInputTask thread.

Backup

Contains the static method write() for backing up the file. write() constructs a filename for the backup by appending "_copy" followed by an integer to the original file name. The number is incremented to prevent overwritting previous backups. For example, subsequent backups of a file named "save.sav" would be named:

save_copy1.sav
save_copy2.sav
save_copy3.sav

write() then copies the file once the file modification has completed. It confirms whether file modification is complete by attempting to rename the original file using file.renameTo(file). The renameTo operation would not change the filename of the original file; however, on a Windows system, this rename operation will fail (returning false) if the file is currently opened by another program. (Mac and Linux systems, however, allow renaming of a file even when it is opened).

autosavebackup_proto's People

Contributors

joncros avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.