Changeset 11079


Ignore:
Timestamp:
2006-01-23T11:30:45+13:00 (18 years ago)
Author:
mdewsnip
Message:

Added an "overwrite" parameter to copyFile to cause it to overwrite the target file instead of throwing an exception.

Location:
trunk/gli/src/org/greenstone/gatherer
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/cdm/GeneralManager.java

    r11049 r11079  
    473473            try {
    474474                File collection_image_file = new File(images_folder, file.getName());
    475                 Gatherer.f_man.getQueue().copyFile(file, collection_image_file, null);
     475                Gatherer.f_man.getQueue().copyFile(file, collection_image_file, false, null);
    476476
    477477                // If we're using a remote Greenstone server, upload the image
  • trunk/gli/src/org/greenstone/gatherer/collection/CollectionManager.java

    r10726 r11079  
    742742    if (!metadata_set_file.exists()) {
    743743        try {
    744         Gatherer.f_man.getQueue().copyFile(external_metadata_set_file, metadata_set_file, null);
     744        Gatherer.f_man.getQueue().copyFile(external_metadata_set_file, metadata_set_file, false, null);
    745745
    746746        // If we're using a remote Greenstone server, upload the metadata file
  • trunk/gli/src/org/greenstone/gatherer/collection/LegacyCollectionImporter.java

    r11053 r11079  
    5353        try {
    5454        backup_dir.mkdirs();
    55         Gatherer.f_man.getQueue().copyFile(metadata_xml_file_file, new_metadata_xml_file_file, null);
     55        Gatherer.f_man.getQueue().copyFile(metadata_xml_file_file, new_metadata_xml_file_file, false, null);
    5656        if (!new_metadata_xml_file_file.exists()) {
    5757            throw new Exception("");
  • trunk/gli/src/org/greenstone/gatherer/file/FileQueue.java

    r11073 r11079  
    336336                    // copy the file. If anything goes wrong the copy file should throw the appropriate exception. No matter what exception is thrown (bar an IOException) we display some message, perhaps take some action, then cancel the remainder of the pending file jobs. No point in being told your out of hard drive space for each one of six thousand files eh?
    337337                    try {
    338                     copyFile(source_file, target_file, progress);
     338                    copyFile(source_file, target_file, false, progress);
    339339                    }
    340340                    // If we can't find the source file, then the most likely reason is that the file system has changed since the last time it was mapped. Warn the user that the requested file can't be found, then force a refresh of the source folder involved.
     
    599599        copyDirectoryContents(f, new_file, progress);
    600600        } else if (f.isFile()) {
    601         copyFile(f, new_file, progress);
     601        copyFile(f, new_file, false, progress);
    602602        }
    603603    }
     
    610610     * @see org.greenstone.gatherer.Gatherer
    611611     */
    612     public void copyFile(File source, File destination, GProgressBar progress)
    613     throws FileAlreadyExistsException, FileNotFoundException, InsufficientSpaceException, IOException, ReadNotPermittedException, UnknownFileErrorException, WriteNotPermittedException {
    614     if(source.isDirectory()) {
     612    public void copyFile(File source, File destination, boolean overwrite, GProgressBar progress)
     613    throws FileAlreadyExistsException, FileNotFoundException, InsufficientSpaceException, IOException, ReadNotPermittedException, UnknownFileErrorException, WriteNotPermittedException
     614    {
     615    if (source.isDirectory()) {
    615616        destination.mkdirs();
    616     }
    617     else {
    618         // Check if the origin file exists.
    619         if (!source.exists()) {
    620         DebugStream.println("Couldn't find the source file.");
    621         throw(new FileNotFoundException());
    622         }
    623 
    624         // Make sure the destination file does not exist.
    625         if (destination.exists()) {
    626         throw(new FileAlreadyExistsException());
    627         }
    628 
    629         // Open an input stream to the source file
    630         FileInputStream f_in = null;
     617        return;
     618    }
     619
     620    // Check if the origin file exists.
     621    if (!source.exists()) {
     622        DebugStream.println("Couldn't find the source file.");
     623        throw new FileNotFoundException();
     624    }
     625
     626    // Make sure the destination file does not exist.
     627    if (destination.exists() && !overwrite) {
     628        throw new FileAlreadyExistsException();
     629    }
     630
     631    // Open an input stream to the source file
     632    FileInputStream f_in = null;
     633    try {
     634        f_in = new FileInputStream(source);
     635    }
     636    catch (FileNotFoundException exception) {
     637        // A FileNotFoundException translates into a ReadNotPermittedException in this case
     638        throw new ReadNotPermittedException(exception.toString());     
     639    }
     640
     641    // Create an necessary directories for the target file
     642    File dirs = destination.getParentFile();
     643    dirs.mkdirs();
     644
     645    // Open an output stream to the target file
     646    FileOutputStream f_out = null;
     647    try {
     648        f_out = new FileOutputStream(destination);
     649    }
     650    catch (FileNotFoundException exception) {
     651        // A FileNotFoundException translates into a WriteNotPermittedException in this case
     652        throw new WriteNotPermittedException(exception.toString());
     653    }
     654
     655    // Copy the file
     656    byte data[] = new byte[Utility.BUFFER_SIZE];
     657    int data_size = 0;
     658    while ((data_size = f_in.read(data, 0, Utility.BUFFER_SIZE)) != -1 && !cancel_action) {
     659        long destination_size = destination.length();
    631660        try {
    632         f_in = new FileInputStream(source);
    633         }
    634         catch (FileNotFoundException exception) {
    635         // A FileNotFoundException translates into a ReadNotPermittedException in this case
    636         throw new ReadNotPermittedException(exception.toString());     
    637         }
    638 
    639         // Create an necessary directories for the target file
    640         File dirs = destination.getParentFile();
    641         dirs.mkdirs();
    642 
    643         // Open an output stream to the target file
    644         FileOutputStream f_out = null;
    645         try {
    646         f_out = new FileOutputStream(destination);
    647         }
    648         catch (FileNotFoundException exception) {
    649         // A FileNotFoundException translates into a WriteNotPermittedException in this case
    650         throw new WriteNotPermittedException(exception.toString());
    651         }
    652 
    653         // Copy the file
    654         byte data[] = new byte[Utility.BUFFER_SIZE];
    655         int data_size = 0;
    656         while((data_size = f_in.read(data, 0, Utility.BUFFER_SIZE)) != -1 && !cancel_action) {
    657         long destination_size = destination.length();
    658         try {
    659             f_out.write(data, 0, data_size);
     661        f_out.write(data, 0, data_size);
     662        }
     663        // If an IO exception occurs, we can do some maths to determine if the number of bytes written to the file was less than expected. If so we assume a InsufficientSpace exception. If not we just throw the exception again.
     664        catch (IOException io_exception) {
     665        if (destination_size + (long) data_size > destination.length()) {
     666            // Determine the difference (which I guess is in bytes).
     667            long difference = (destination_size + (long) data_size) - destination.length();
     668            // Transform that into a human readable string.
     669            String message = Utility.formatFileLength(difference);
     670            throw new InsufficientSpaceException(message);
    660671        }
    661         // If an IO exception occurs, we can do some maths to determine if the number of bytes written to the file was less than expected. If so we assume a InsufficientSpace exception. If not we just throw the exception again.
    662         catch (IOException io_exception) {
    663             if(destination_size + (long) data_size > destination.length()) {
    664             // Determine the difference (which I guess is in bytes).
    665             long difference = (destination_size + (long) data_size) - destination.length();
    666             // Transform that into a human readable string.
    667             String message = Utility.formatFileLength(difference);
    668             throw(new InsufficientSpaceException(message));
    669             }
    670             else {
    671             throw(io_exception);
    672             }
     672        else {
     673            throw(io_exception);
    673674        }
    674         if(progress != null) {
    675             progress.addValue(data_size);
    676         }
    677         }
    678         // Flush and close the streams to ensure all bytes are written.
    679         f_in.close();
    680         f_out.close();
    681         // We have now, in theory, produced an exact copy of the source file. Check this by comparing sizes.
    682         if(!destination.exists() || (!cancel_action && source.length() != destination.length())) {
    683         throw(new UnknownFileErrorException());
    684         }
    685         // If we were cancelled, ensure that none of the destination file exists.
    686         if(cancel_action) {
    687         destination.delete();
    688         }
     675        }
     676        if (progress != null) {
     677        progress.addValue(data_size);
     678        }
     679    }
     680
     681    // Flush and close the streams to ensure all bytes are written.
     682    f_in.close();
     683    f_out.close();
     684
     685    // We have now, in theory, produced an exact copy of the source file. Check this by comparing sizes.
     686    if(!destination.exists() || (!cancel_action && source.length() != destination.length())) {
     687        throw new UnknownFileErrorException();
     688    }
     689
     690    // If we were cancelled, ensure that none of the destination file exists.
     691    if (cancel_action) {
     692        destination.delete();
    689693    }
    690694    }
Note: See TracChangeset for help on using the changeset viewer.