/// <summary> /// Checks if a file exists on the server. /// </summary> /// <param name="path">The full or relative path to the file</param> /// <returns>True if the file exists</returns> public bool FileExists(string path) { // verify args if (path.IsBlank()) { throw new ArgumentException("Required parameter is null or blank.", "path"); } #if !CORE14 lock (m_lock) { #endif path = path.GetFtpPath(); LogFunc(nameof(FileExists), new object[] { path }); // calc the absolute filepath path = GetAbsolutePath(path); // since FTP does not include a specific command to check if a file exists // here we check if file exists by attempting to get its filesize (SIZE) if (HasFeature(FtpCapability.SIZE)) { // Fix #328: get filesize in ASCII or Binary mode as required by server var sizeReply = new FtpSizeReply(); GetFileSizeInternal(path, sizeReply, -1); // handle known errors to the SIZE command var sizeKnownError = CheckFileExistsBySize(sizeReply); if (sizeKnownError.HasValue) { return(sizeKnownError.Value); } } // check if file exists by attempting to get its date modified (MDTM) if (HasFeature(FtpCapability.MDTM)) { var reply = Execute("MDTM " + path); var ch = reply.Code[0]; if (ch == '2') { return(true); } if (ch == '5' && reply.Message.IsKnownError(FtpServerStrings.fileNotFound)) { return(false); } } // check if file exists by getting a name listing (NLST) var fileList = GetNameListing(path.GetFtpDirectoryName()); return(FileListings.FileExistsInNameListing(fileList, path)); #if !CORE14 } #endif }
/// <summary> /// Checks if a file exists on the server asynchronously. /// </summary> /// <param name="path">The full or relative path to the file</param> /// <param name="token">The token that can be used to cancel the entire process</param> /// <returns>True if the file exists, false otherwise</returns> public async Task <bool> FileExistsAsync(string path, CancellationToken token = default(CancellationToken)) { // verify args if (path.IsBlank()) { throw new ArgumentException("Required parameter is null or blank.", "path"); } path = path.GetFtpPath(); LogFunc(nameof(FileExistsAsync), new object[] { path }); // calc the absolute filepath path = await GetAbsolutePathAsync(path, token); // since FTP does not include a specific command to check if a file exists // here we check if file exists by attempting to get its filesize (SIZE) if (HasFeature(FtpCapability.SIZE)) { // Fix #328: get filesize in ASCII or Binary mode as required by server FtpSizeReply sizeReply = new FtpSizeReply(); await GetFileSizeInternalAsync(path, -1, token, sizeReply); // handle known errors to the SIZE command var sizeKnownError = CheckFileExistsBySize(sizeReply); if (sizeKnownError.HasValue) { return(sizeKnownError.Value); } } // check if file exists by attempting to get its date modified (MDTM) if (HasFeature(FtpCapability.MDTM)) { FtpReply reply = await ExecuteAsync("MDTM " + path, token); var ch = reply.Code[0]; if (ch == '2') { return(true); } if (ch == '5' && reply.Message.IsKnownError(FtpServerStrings.fileNotFound)) { return(false); } } // check if file exists by getting a name listing (NLST) string[] fileList = await GetNameListingAsync(path.GetFtpDirectoryName(), token); return(FileListings.FileExistsInNameListing(fileList, path)); }
/// <summary> /// Check if the file is cleared to be uploaded, taking its existance/filesize and existsMode options into account. /// </summary> private bool CanUploadFile(FtpResult result, FtpListItem[] remoteListing, FtpRemoteExists existsMode, out FtpRemoteExists existsModeToUse) { // check if the file already exists on the server existsModeToUse = existsMode; var fileExists = FileListings.FileExistsInListing(remoteListing, result.RemotePath); // if we want to skip uploaded files and the file already exists, mark its skipped if (existsMode == FtpRemoteExists.Skip && fileExists) { LogStatus(FtpTraceLevel.Info, "Skipped file that already exists: " + result.LocalPath); result.IsSuccess = true; result.IsSkipped = true; return(false); } // in any mode if the file does not exist, mark that exists check is not required if (!fileExists) { existsModeToUse = existsMode == FtpRemoteExists.Resume ? FtpRemoteExists.ResumeNoCheck : FtpRemoteExists.NoCheck; } return(true); }
/// <summary> /// Checks if a file exists on the server. /// </summary> /// <param name="path">The full or relative path to the file</param> /// <returns>True if the file exists</returns> public bool FileExists(string path) { // verify args if (path.IsBlank()) { throw new ArgumentException("Required parameter is null or blank.", "path"); } #if !CORE14 lock (m_lock) { #endif path = path.GetFtpPath(); LogFunc(nameof(FileExists), new object[] { path }); // A check for path.StartsWith("/") tells us, even if it is z/OS, we can use the normal unix logic // If z/OS: Do not GetAbsolutePath(), unless we have a leading slash if (ServerType != FtpServer.IBMzOSFTP || path.StartsWith("/")) { // calc the absolute filepath path = GetAbsolutePath(path); } // since FTP does not include a specific command to check if a file exists // here we check if file exists by attempting to get its filesize (SIZE) // If z/OS: Do not do SIZE, unless we have a leading slash if (HasFeature(FtpCapability.SIZE) && (ServerType != FtpServer.IBMzOSFTP || path.StartsWith("/"))) { // Fix #328: get filesize in ASCII or Binary mode as required by server var sizeReply = new FtpSizeReply(); GetFileSizeInternal(path, sizeReply, -1); // handle known errors to the SIZE command var sizeKnownError = CheckFileExistsBySize(sizeReply); if (sizeKnownError.HasValue) { return(sizeKnownError.Value); } } // check if file exists by attempting to get its date modified (MDTM) // If z/OS: Do not do MDTM, unless we have a leading slash if (HasFeature(FtpCapability.MDTM) && (ServerType != FtpServer.IBMzOSFTP || path.StartsWith("/"))) { var reply = Execute("MDTM " + path); var ch = reply.Code[0]; if (ch == '2') { return(true); } if (ch == '5' && reply.Message.IsKnownError(FtpServerStrings.fileNotFound)) { return(false); } } // If z/OS: different handling, unless we have a leading slash if (ServerType == FtpServer.IBMzOSFTP && !path.StartsWith("/")) { var fileList = GetNameListing(path); return(fileList.Count() > 0); } else // check if file exists by getting a name listing (NLST) { var fileList = GetNameListing(path.GetFtpDirectoryName()); return(FileListings.FileExistsInNameListing(fileList, path)); } #if !CORE14 } #endif }