ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From EXT / FOCAL MALAPRADE Roland <ext.focal.malapr...@sncf.fr>
Subject FTP Optional task - try to change it to support "last modified da te" preservation
Date Thu, 12 Sep 2002 11:36:31 GMT
Hi,

I was using the <ftp> task to retrieve files from a server, and I noticed
that files' last modified date was not being preserved. I originally posted
this problem to ant-user, but since I am trying to change the Ant code, I
thought it would be more appropriate to post here.

I added this code to FTP.java:

/** Code added **/

private boolean preserveLastModified = true;

public void setPreserveLastModified(boolean preserve) {
    this.preserveLastModified = preserve;
}

/**
 * Returns the timestamp of a remote file.
 * The timestamp is usually the file's last modified date.
 * @returns the timestamp as a long.
 */
protected long getFileTimestamp(FTPClient ftp, String remoteFile)
     throws IOException, BuildException {
    log("getting date for " + remoteFile, Project.MSG_VERBOSE);

    FTPFile[] files = ftp.listFiles(remoteFile);

    // For Microsoft's Ftp-Service an Array with length 0 is
    // returned if configured to return listings in "MS-DOS"-Format
    if (files == null || files.length == 0) {
        // If we are sending files, then assume out of date.
        // If we are getting files, then throw an error

        return 0L;
    }
    return files[0].getTimestamp().getTime().getTime();
}
/** End code added **/



And I changed the getFile method to this (changes indicated) :

/** Code changed **/

protected void getFile(FTPClient ftp, String dir, String filename)
     throws IOException, BuildException {
    OutputStream outstream = null;

    // getTimestamp()

    try {
        File file = project.resolveFile(new File(dir, filename).getPath());

        if (newerOnly && isUpToDate(ftp, file, resolveFile(filename))) {
            return;
        }

        if (verbose) {
            log("transferring " + filename + " to "
                 + file.getAbsolutePath());
        }

        File pdir = fileUtils.getParentFile(file);

        if (!pdir.exists()) {
            pdir.mkdirs();
        }
        outstream = new BufferedOutputStream(new FileOutputStream(file));
        ftp.retrieveFile(resolveFile(filename), outstream);

        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            String s = "could not get file: " + ftp.getReplyString();

            if (skipFailedTransfers == true) {
                log(s, Project.MSG_WARN);
                skipped++;
            } else {
                throw new BuildException(s);
            }

        } else {
            log("File " + file.getAbsolutePath() + " copied from "
                 + server, Project.MSG_VERBOSE);
            transferred++;
        }
/** My changes **/

        if (preserveLastModified) {
            long l = getFileTimestamp(ftp,resolveFile(filename));
            log("File's last modified date : " + new Date(l).toString() , 
                Project.MSG_VERBOSE);
            
            if (l != 0L) {
                if (!file.setLastModified(l)) {
                    log("local file's last modified date could not be set :
" + file.getAbsolutePath() , 
                        Project.MSG_WARN);
                }
            } else {
                log("File's last modified date could not be preserved : " +
file.getAbsolutePath() , 
                    Project.MSG_WARN);
            }
        }
/** End my changes **/

    } finally {
        if (outstream != null) {
            try {
                outstream.close();
            } catch (IOException ex) {
                // ignore it
            }
        }
    }
}

/** End code changed **/


Now the problem is that the files' last modified date is not being changed.
Is it something I am doing wrong? Is the file being modified after the call
to the getFile() method ? (I looked but I couldn't find anything).

Is it possible that you can't set the "last modified date" to a date earlier
than the creation time ? (which is what I'm trying to do. I've looked on the
net, but I haven't found anything stating that you can't.)

BTW I am using:
- Ant 1.5 (9 July 2002)
- NetComponents 1.3.8a
- JDK 1.3.1-b24

Any help would be great.

Roland.

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message