Downloading a file from a webserver error (C++) - c++

As the title says, I want to download a file from a webserver (eg. localhost/file.txt), but there is a problem. When I use "URLDownloadToFileW", "URLDownloadToFile" and "URLDownloadToFileA", the function return true, but the file doesn't download.
Here is the function that downloads the file:
void MyFunction() {
const char* url_to_download = "localhost/file.txt";
const char* file_dest = "C:\\fileDownloaded.txt";
if (S_OK != URLDownloadToFile(NULL, url_to_download, file_dest, 0, NULL)) {
system("cls");
std::cout << "Failed to get dll!";
system("pause");
exit(1);
}
}
Again, my problem is that the file doesn't download (i can't find the file where I told the URLDownload function to save), but URLDownloadToFile function return true (it returns S_OK but for the sake of this post, let's say that it returns true)
Thanks!

Related

C++ fopen() returns NULL pointer on some Windows

I'm developing a little updater for my framework. In particular the file is written in C++ and when i try to download a file using the following code the fopen function returns NULL. But the thing is that i tested this software on different machines with the same OS (Windows 10) and on few of them they returns NULL, the others just download and write the file correctly. Do you have any ideas? I've also tried to TRIM the filename to avoid invisible characters. Here's the code to download the file i used:
std::vector<unsigned char> resp = http_request(url, "GET", NULL, NULL, "", user_agent);
if (resp.empty()) {
send_output("ERROR: No response while downloading: " + url);
return;
}
string filename = url.substr(url.rfind("/") + 1);
filename = trim(filename);
if (filename.empty()) {
filename = "downloaded";
}
FILE* f = fopen(filename.c_str(), "wb");
if (f == NULL) {
log("ERROR: Could not open file for writing: " + filename);
return;
}
fwrite(&resp[0], 1, resp.size(), f);
fclose(f);
Thanks guys for the help!

c++ - fopen() internally changes my filename?

I use fopen() in my c++ program and I tried to open a .aff file.
I want to parse a file named car_wheel.aff and after if(ifp=fopen(path,"r")) has executed, it seems the fopen() function changes my path variable???
I add some detail to my question to the comment.
code (since variable path is constructed by my code, I put the whole piece of code here, which may seem a bit redundant.)
char* dir = "../kitchen/";
char filename[100];
char* path;
FILE *ifp;
int detail_level;
if(fscanf(fp,"%d %s",&detail_level,filename)!=2)
{
printf("Error: could not parse include.\n");
exit(0);
}
path = (char*)malloc(strlen(dir)+strlen(filename));
strcpy(path, dir);
strcat(path, filename); // path is "../kitchen/car_wheel.aff"
if(detail_level<=gDetailLevel)
{
if(ifp=fopen(path,"r"))
{
viParseFile(ifp);
fclose(ifp);
}
else
{
// jumped here and path became "../kitchen/car_wheel.aff1\002"
if (ifp == NULL) {
perror(path);
exit(EXIT_FAILURE);
}
printf("Error: could not open include file: <%s>.\n",filename);
exit(1);
}
}
I debugged the code in my ide, and it gave the filename char array is
and there is no '1\002' behind my filename variable. What happened??
The problem is here:
path = (char*)malloc(strlen(dir)+strlen(filename));
You don't allocate space for the terminating zero character. Change it to this:
path = (char*)malloc(strlen(dir)+strlen(filename)+1);

How to handle Symbolic Links and Junction while deleting or coping a folder tree

I had task to copy and delete a huge folder using win32 api (C++), I am using the Code Guru recurisive directory deletion code, which works well, but there arises certain question.
RemoveDirectory
Million thanks to Lerooooy Jenkins for pointing it.
The link to CodeGuru for recursive deletes doesn't correctly handle
symbolic links/junctions. Given that a reparse point could point
anywhere (even network drives), you need to be careful when deleting
recursively and only delete the symbolic link/junction and not what it
points at. The correct way to handle this situation is to detect
reparse points (via GetFileAttributes()) and NOT traverse it as a
subdirectory.
So my question is how to actually handle Symbolic Links and Junction while deleting or coping a folder tree.
For the shake of question here is the source code of CodeGuru Directory Deletion
#include <string>
#include <iostream>
#include <windows.h>
#include <conio.h>
int DeleteDirectory(const std::string &refcstrRootDirectory,
bool bDeleteSubdirectories = true)
{
bool bSubdirectory = false; // Flag, indicating whether
// subdirectories have been found
HANDLE hFile; // Handle to directory
std::string strFilePath; // Filepath
std::string strPattern; // Pattern
WIN32_FIND_DATA FileInformation; // File information
strPattern = refcstrRootDirectory + "\\*.*";
hFile = ::FindFirstFile(strPattern.c_str(), &FileInformation);
if(hFile != INVALID_HANDLE_VALUE)
{
do
{
if(FileInformation.cFileName[0] != '.')
{
strFilePath.erase();
strFilePath = refcstrRootDirectory + "\\" + FileInformation.cFileName;
if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(bDeleteSubdirectories)
{
// Delete subdirectory
int iRC = DeleteDirectory(strFilePath, bDeleteSubdirectories);
if(iRC)
return iRC;
}
else
bSubdirectory = true;
}
else
{
// Set file attributes
if(::SetFileAttributes(strFilePath.c_str(),
FILE_ATTRIBUTE_NORMAL) == FALSE)
return ::GetLastError();
// Delete file
if(::DeleteFile(strFilePath.c_str()) == FALSE)
return ::GetLastError();
}
}
} while(::FindNextFile(hFile, &FileInformation) == TRUE);
// Close handle
::FindClose(hFile);
DWORD dwError = ::GetLastError();
if(dwError != ERROR_NO_MORE_FILES)
return dwError;
else
{
if(!bSubdirectory)
{
// Set directory attributes
if(::SetFileAttributes(refcstrRootDirectory.c_str(),
FILE_ATTRIBUTE_NORMAL) == FALSE)
return ::GetLastError();
// Delete directory
if(::RemoveDirectory(refcstrRootDirectory.c_str()) == FALSE)
return ::GetLastError();
}
}
}
return 0;
}
int main()
{
int iRC = 0;
std::string strDirectoryToDelete = "c:\\mydir";
// Delete 'c:\mydir' without deleting the subdirectories
iRC = DeleteDirectory(strDirectoryToDelete, false);
if(iRC)
{
std::cout << "Error " << iRC << std::endl;
return -1;
}
// Delete 'c:\mydir' and its subdirectories
iRC = DeleteDirectory(strDirectoryToDelete);
if(iRC)
{
std::cout << "Error " << iRC << std::endl;
return -1;
}
// Wait for keystroke
_getch();
return 0;
}
Use DeleteFile to delete file symbolic links.
Use RemoveDirectory to delete directory symbolic links and junctions.
In other words, you treat them just like any other file or directory except that you don't recurse into directories that have the FILE_ATTRIBUTE_REPARSE_POINT attribute.
The simplest way to achieve your goal, and the recommended way to do it, is to get the system to do the work.
If you need to support XP then you use SHFileOperation with the FO_DELETE flag.
Otherwise, for Vista and later, use IFileOperation.
These APIs handle all the details for you, and use the same code paths as does the shell. You can even show the standard shell progress UI if you desire.

Libssh SFTP download inside a thread crashes on sftp_read

I'm using the libssh library in MFC C++, specifically the SFTP wrapper. I have code working when not using threading, but I want to use AfxBeginThread to allow user action to continue.
I have confirmed I'm passing the exact same filenames and target save paths using the thread or not - I've tested this by hardcoding the file to save and the destination to save directly in BeginDownload().
Is there a specific way the SFTP wrapper for the libssh must be used to let the download perform within a thread? I would like to allow users to download multiple files at the same time from the same instance of the ssh objects, and just launch a new thread each time a file needs to be downloaded.
Here is what I'm using for the actual downloading. m_sftp_sesssion is my sftp_session object created upon logging in.
bool CSFtpManager::BeginDownload(CString filename, CString savefilename)
{
sftp_dir dir = sftp_opendir(m_sftp_session, "../../../directory");
sftp_attributes attrs = sftp_readdir(m_sftp_session, dir);
int access_type = O_RDONLY;
sftp_file file;
file = sftp_open(m_sftp_session, "../../../directory/myfile.zip", access_type, 0);
const char* x2 = ssh_get_error(m_ssh_session);
if(file == NULL)
{
int a;
}
int nbytes;
char buffer[1024];
FILE * pFile;
pFile = fopen(CStringA(savefilename), "wb");
nbytes = sftp_read(file, buffer, sizeof(buffer)); //this is crashing inside a thread
while(nbytes > 0)
{
fwrite(buffer, sizeof(char), sizeof(buffer), pFile);
nbytes = sftp_read(file, buffer, sizeof(buffer));
}
fclose(pFile);
sftp_close(file);
return true;
}
Here is my login method where my libssh objects are being set in member variables for later use. Included is a commented line calling my BeginDownload. Calling it outside a thread (i.e. clicking a login button) does work.
bool CSFtpManager::login()
{
int verbosity = SSH_LOG_PROTOCOL;
m_ssh_session = ssh_new();
ssh_options_set(m_ssh_session, SSH_OPTIONS_HOST, "ftp3 host..");
//setting username and password...
ssh_options_set(m_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
ssh_options_set(m_ssh_session, SSH_OPTIONS_PORT, &m_iPort);
int x = ssh_connect(m_ssh_session);
const char* x2 = ssh_get_error(m_ssh_session);
if(rc != SSH_AUTH_SUCCESS)
{
return false;
}
m_sftp_session;
m_sftp_session = sftp_new(m_ssh_session);
//Calling BeginDownload here does work
//BeginDownload(L"file.txt", L"T:\\file.txt");
... }
Here is what I'm using to call the exact same function via a thread
CWinThread* pThread2 = AfxBeginThread(BeginSFTPDownload, bundle);
And the method being ran via the above:
UINT CMainFrame::BeginSFTPDownload( LPVOID pParam)
{
pSft->BeginDownload(L"test.txt", L"T:\\test.txt");
return 0;
}

Creating text file into C++ addon of node.js

I want to know how i can create file and append data inside it in c++ addon (.cc) file of node.js ??
I have used below code to do same, but not able to find file "data.txt" in my ubuntu machine(reason behind it may be below code is not correct way to create file, but strange i haven't received any error/warning at compile time).
FILE * pFileTXT;
pFileTXT = fopen ("data.txt","a+");
const char * c = localReq->strResponse.c_str();
fprintf(pFileTXT,c);
fclose (pFileTXT);
Node.js relies on libuv, a C library to handle the I/O (asynchronous or not). This allows you to use the event loop.
You'd be interested in this free online book/introduction to libuv: http://nikhilm.github.com/uvbook/index.html
Specifically, there is a chapter dedicated to reading/writing files.
int main(int argc, char **argv) {
// Open the file in write-only and execute the "on_open" callback when it's ready
uv_fs_open(uv_default_loop(), &open_req, argv[1], O_WRONLY, 0, on_open);
// Run the event loop.
uv_run(uv_default_loop());
return 0;
}
// on_open callback called when the file is opened
void on_open(uv_fs_t *req) {
if (req->result != -1) {
// Specify the on_write callback "on_write" as last argument
uv_fs_write(uv_default_loop(), &write_req, 1, buffer, req->result, -1, on_write);
}
else {
fprintf(stderr, "error opening file: %d\n", req->errorno);
}
// Don't forget to cleanup
uv_fs_req_cleanup(req);
}
void on_write(uv_fs_t *req) {
uv_fs_req_cleanup(req);
if (req->result < 0) {
fprintf(stderr, "Write error: %s\n", uv_strerror(uv_last_error(uv_default_loop())));
}
else {
// Close the handle once you're done with it
uv_fs_close(uv_default_loop(), &close_req, open_req.result, NULL);
}
}
Spend some time reading the book if you want to write C++ for node.js. It's worth it.