Author: leavengood Date: Fri Jan 7 15:51:32 2011 New Revision: 571 URL: http://webpositive.haiku-os.org/changeset/571 Log: Don't clobber existing downloaded files. This will add -<number> to the filename before the extension (if there is one) until a non-existant file name is found. Fixes #71. Modified: webkit/trunk/WebKit/haiku/API/WebDownloadPrivate.cpp webkit/trunk/WebKit/haiku/API/WebDownloadPrivate.h Modified: webkit/trunk/WebKit/haiku/API/WebDownloadPrivate.cpp ============================================================================== --- webkit/trunk/WebKit/haiku/API/WebDownloadPrivate.cpp Fri Jan 7 04:53:01 2011 (r570) +++ webkit/trunk/WebKit/haiku/API/WebDownloadPrivate.cpp Fri Jan 7 15:51:32 2011 (r571) @@ -40,6 +40,7 @@ #include <Message.h> #include <Messenger.h> #include <NodeInfo.h> +#include <stdio.h> namespace BPrivate { @@ -176,7 +177,9 @@ void WebDownloadPrivate::createFile() { - m_path.Append(m_filename.String()); + // Don't overwrite existing files + findAvailableFilename(); + if (m_file.SetTo(m_path.Path(), B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY) == B_OK) { BNodeInfo info(&m_file); info.SetType(m_mimeType.String()); @@ -190,5 +193,35 @@ } } +void WebDownloadPrivate::findAvailableFilename() +{ + BPath filePath = m_path; + BString fileName = m_filename; + filePath.Append(fileName.String()); + + BEntry entry(filePath.Path()); + for (int32 i = 0; entry.InitCheck() == B_OK && entry.Exists(); i++) { + // Use original file name in each iteration + BString baseName = m_filename; + + // Separate extension and base file name + int32 extensionStart = baseName.FindLast('.'); + BString extension; + if (extensionStart > 0) + baseName.MoveInto(extension, extensionStart, baseName.CountChars() - extensionStart); + + // Add i to file name before the extension + char num[10]; + snprintf(num, sizeof(num), "-%d", i); + baseName.Append(num).Append(extension); + fileName = baseName; + filePath = m_path; + filePath.Append(fileName); + entry.SetTo(filePath.Path()); + } + m_filename = fileName; + m_path = filePath; +} + } // namespace BPrivate Modified: webkit/trunk/WebKit/haiku/API/WebDownloadPrivate.h ============================================================================== --- webkit/trunk/WebKit/haiku/API/WebDownloadPrivate.h Fri Jan 7 04:53:01 2011 (r570) +++ webkit/trunk/WebKit/haiku/API/WebDownloadPrivate.h Fri Jan 7 15:51:32 2011 (r571) @@ -80,6 +80,7 @@ private: void handleFinished(WebCore::ResourceHandle* handle, uint32 status); void createFile(); + void findAvailableFilename(); private: BWebDownload* m_webDownload;