public void copy()

in clearcase-common/src/jetbrains/buildServer/vcs/clearcase/Util.java [292:439]


      public void copy(CopyJob... copyJobs) throws IOException {
        byteCount = 0;
        copiedBytes = 0;

        // feed our property change listeners
        State previousState = state;
        state = State.CHECKING_SOURCE;
        propertyChangeSupport.firePropertyChange(STATE_PROPERTY, previousState, state);

        // scan all sources of all copyJobs and store the directoryInfos
        int fileCount = 0;
        for (CopyJob copyJob : copyJobs) {
          if (copyJob == null) {
            continue;
          }
          Source[] sources = copyJob.getSources();
          List<DirectoryInfo> directoryInfos = new ArrayList<DirectoryInfo>();
          for (Source source : sources) {
            File baseDirectory = source.getBaseDirectory();
            int baseDirectoryPathLength = 0;
            String baseDirectoryPath = baseDirectory.getPath();
            if (baseDirectoryPath.endsWith(File.separator)) {
              // baseDirectory is a file system root, e.g.
              // "/" or "C:\"
              baseDirectoryPathLength = baseDirectoryPath.length();
            } else {
              // baseDirectory is a normal directory, e.g.
              // "/etc" or "C:\test"
              baseDirectoryPathLength = baseDirectoryPath.length() + 1;
            }
            DirectoryInfo tmpInfo = expand(baseDirectoryPathLength, baseDirectory, source.getPattern(), source.isRecursive());
            if (tmpInfo != null) {
              directoryInfos.add(tmpInfo);
              byteCount += tmpInfo.getByteCount();
              fileCount += tmpInfo.getFiles().size();
            }
          }
          copyJob.setDirectoryInfos(directoryInfos);
          if (LOGGER.isDebugEnabled()) {
            StringBuilder stringBuilder = new StringBuilder("source files:\n");
            for (DirectoryInfo directoryInfo : directoryInfos) {
              stringBuilder.append("source files in base directory ");
              stringBuilder.append(directoryInfo.getBaseDirectory());
              stringBuilder.append(":\n");
              for (File sourceFile : directoryInfo.getFiles()) {
                stringBuilder.append(sourceFile.isFile() ? "f " : "d ");
                stringBuilder.append(sourceFile.getPath());
                stringBuilder.append('\n');
              }
            }
            LOGGER.info(stringBuilder.toString());
          }
        }

        if (fileCount == 0) {
          LOGGER.info("there are no files to copy");
          return;
        }

        // do all known sanity checks
        for (CopyJob copyJob : copyJobs) {
          // skip empty jobs
          if (copyJob == null) {
            continue;
          }
          // get number of source files in this job
          List<DirectoryInfo> directoryInfos = copyJob.getDirectoryInfos();
          int sourceCount = 0;
          for (DirectoryInfo directoryInfo : directoryInfos) {
            sourceCount += directoryInfo.getFiles().size();
          }
          // skip empty jobs
          if (sourceCount == 0) {
            continue;
          }

          String[] destinations = copyJob.getDestinations();
          for (String destination : destinations) {
            File destinationFile = new File(destination);
            if (destinationFile.isFile()) {
              if (sourceCount == 1) {
                File sourceFile = directoryInfos.get(0).getFiles().get(0);
                if (sourceFile.isDirectory()) {
                  throw new IOException("can not overwrite file \"" + destinationFile + "\" with directory \"" + sourceFile + "\"");
                }
              } else {
                StringBuilder errorMessage = new StringBuilder("can not copy several files to another file\n" + " sources:");
                for (DirectoryInfo directoryInfo : directoryInfos) {
                  List<File> files = directoryInfo.getFiles();
                  for (File file : files) {
                    errorMessage.append("  ").append(file.getPath());
                  }
                }
                errorMessage.append(" destination: ").append(destinationFile.getPath());
                throw new IOException(errorMessage.toString());
              }
            }
          }
        }

        // feed our property change listeners
        previousState = state;
        state = State.COPYING;
        propertyChangeSupport.firePropertyChange(STATE_PROPERTY, previousState, state);

        // execute all copy jobs
        for (CopyJob copyJob : copyJobs) {
          // skip empty copy jobs
          if (copyJob == null) {
            continue;
          }

          for (DirectoryInfo directoryInfo : copyJob.getDirectoryInfos()) {
            for (File sourceFile : directoryInfo.getFiles()) {
              File[] destinationFiles = getDestinationFiles(directoryInfo.getBaseDirectory(), sourceFile, copyJob.getDestinations());
              if (sourceFile.isDirectory()) {
                // make target directories (sequentially)
                for (File destinationFile : destinationFiles) {
                  if (destinationFile.exists()) {
                    if (destinationFile.isDirectory()) {
                      LOGGER.info("Directory \"" + destinationFile + "\" already exists");
                    } else {
                      throw new IOException("can not overwrite " + "file \"" + destinationFile + "\" with directory \"" + sourceFile + "\"");
                    }
                  } else {
                    LOGGER.info("Creating directory \"" + destinationFile + "\"");
                    if (!destinationFile.mkdirs()) {
                      throw new IOException("Could not create directory \"" + destinationFile + "\"");
                    }
                  }
                }
              } else {
                // create target files in parrallel
                copyFile(sourceFile, destinationFiles);
              }
            }
          }
        }

        if (oldCopiedBytes != copiedBytes) {
          // need to fire one last time...
          // (last slice was not fully used)
          propertyChangeSupport.firePropertyChange(BYTE_COUNTER_PROPERTY, oldCopiedBytes, copiedBytes);
        }
        previousState = state;
        state = State.END;
        propertyChangeSupport.firePropertyChange(STATE_PROPERTY, previousState, state);
      }