I'm using libssh to download a file from a Debian Linux server on my local Windows machine, the program is running without error but the downloaded file is corrupted. When I download via pscp it works, I don't know if I'm doing something wrong with regards to the buffer or something else.
The sizes of the generated files are identical, when I download it via other tools like FileZilla or WinSCP everything goes well. The file doesn't corrupt anymore. When I run my code then it gets corrupted
I'm running this code on Windows 10 with Visual Studio 2019. After messing with this code a lot I think the problem is with the write file writing function when I open the generated file in a hex editor I see differences in bytes only the end of the file that stays the same.
I ended up posting the Dart version of the code previously, but the C version is almost identical to the Dart version.
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <stdexcept>
#include <string.h>
#include <exception>
#include <fstream>
#include <libssh/callbacks.h>
#include <libssh/libssh.h>
#include <libssh/sftp.h>
#include "custom_exception.cpp"
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <io.h>
#include <time.h>
#include <chrono>
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::duration;
using std::chrono::milliseconds;
//using namespace std;
using std::string;
using std::cout;
using std::endl;
using std::cin;
// Good chunk size
#define MAX_XFER_BUF_SIZE 16384
int sftpDownloadFileTo(ssh_session session, const char* fullRemotePath, const char* fullLocalPath)
{
int access_type;
sftp_file file;
char buffer[MAX_XFER_BUF_SIZE * 8];//128KB
ssize_t nbytes, nwritten, rc;
int fd;
access_type = O_RDONLY;
sftp_session sftp = sftp_new(session);
if (sftp == NULL)
{
fprintf(stderr, "Error allocating SFTP session: %s\n",
ssh_get_error(session));
return SSH_ERROR;
}
rc = sftp_init(sftp);
if (rc != SSH_OK)
{
fprintf(stderr, "Error initializing SFTP session: %d.\n",
sftp_get_error(sftp));
sftp_free(sftp);
return rc;
}
file = sftp_open(sftp, fullRemotePath, access_type, 0);
if (file == NULL) {
fprintf(stderr, "Can't open file for reading: %s\n", ssh_get_error(session));
return SSH_ERROR;
}
fd = open(fullLocalPath, O_CREAT | O_RDWR, 0777);
if (fd < 0) {
fprintf(stderr, "Can't open file for writing: %s\n",
strerror(errno));
return SSH_ERROR;
}
for (;;) {
nbytes = sftp_read(file, buffer, sizeof(buffer));
if (nbytes == 0) {
break; // EOF
}
else if (nbytes < 0) {
fprintf(stderr, "Error while reading file: %s\n",
ssh_get_error(session));
sftp_close(file);
return SSH_ERROR;
}
nwritten = write(fd, buffer, nbytes);
if (nwritten != nbytes) {
fprintf(stderr, "Error writing: %s\n",
strerror(errno));
sftp_close(file);
return SSH_ERROR;
}
}
rc = sftp_close(file);
if (rc != SSH_OK) {
fprintf(stderr, "Can't close the read file: %s\n",
ssh_get_error(session));
return rc;
}
sftp_free(sftp);
return SSH_OK;
}
int main()
{
ssh_session my_ssh_session;
int rc;
int port = 22;
string password = "Ins257257";
auto host = "192.168.133.13";
auto username = "isaque.neves";
int verbosity = SSH_LOG_PROTOCOL;
// Abra a sessão e defina as opções
my_ssh_session = ssh_new();
if (my_ssh_session == NULL)
exit(-1);
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, host);
//ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
// Conecte-se ao servidor
rc = ssh_connect(my_ssh_session);
if (rc != SSH_OK)
{
fprintf(stderr, "Error connecting to host: %s\n",
ssh_get_error(my_ssh_session));
exit(-1);
}
// Autenticar-se
rc = ssh_userauth_password(my_ssh_session, username, password.c_str());
if (rc != SSH_AUTH_SUCCESS)
{
fprintf(stderr, "Error authenticating with password: %s\n",
ssh_get_error(my_ssh_session));
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
exit(-1);
}
clock_t tic = clock();
auto t1 = high_resolution_clock::now();
sftpDownloadFileTo(my_ssh_session, "/home/isaque.neves/go1.11.4.linux-amd64.tar.gz", "D:/MyDartProjects/fsbackup/libssh_binding/go1.11.4.linux-amd64.tar.gz");
auto t2 = high_resolution_clock::now();
clock_t toc = clock();
duration<double, std::milli> ms_double = t2 - t1;
std::cout << ms_double.count() << " ms\r\n";
printf("Elapsed: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
std::cout << "End\n";
return 0;
}
https://github.com/insinfo/fsbackup/tree/main/libssh_binding
fd = open(fullLocalPath, O_CREAT | O_RDWR, 0777);
Here's your problem. Since you are running on Windows we need to adjust here.
fd = open(fullLocalPath, O_CREAT | O_RDWR | O_BINARY, 0777);
otherwise write() will try to adjust line endings.
Related
I followed the documentation here https://api.libssh.org/master/libssh_tutor_sftp.html
to write a small program in c to download a remote file, the program is working but downloading a 120MB file takes 12-14 seconds to complete, compared to winscp, filezilla or pscp which only takes 2 seconds to complete.
How to improve this speed?
PS D:\MyDartProjects\fsbackup\libssh_binding\libssh_c_wrapper\x64\Release> Measure-Command {pscp -pw Ins257257 isaque.neves#192.168.133.13:/home/isaque.neves/go1.11.4.linux-amd64.tar.gz ./go1.11.4.linux-amd64.tar.gz }
Days : 0
Hours : 0
Minutes : 0
Seconds : 2
Milliseconds : 575
Ticks : 25755922
TotalDays : 2,98100949074074E-05
TotalHours : 0,000715442277777778
TotalMinutes : 0,0429265366666667
TotalSeconds : 2,5755922
TotalMilliseconds : 2575,5922
PS D:\MyDartProjects\fsbackup\libssh_binding\libssh_c_wrapper\x64\Release> Measure-Command {./libssh_wrapper.exe}
Days : 0
Hours : 0
Minutes : 0
Seconds : 14
Milliseconds : 360
Ticks : 143600026
TotalDays : 0,000166203733796296
TotalHours : 0,00398888961111111
TotalMinutes : 0,239333376666667
TotalSeconds : 14,3600026
TotalMilliseconds : 14360,0026
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <stdexcept>
#include <string.h>
#include <exception>
#include <fstream>
#include <libssh/callbacks.h>
#include <libssh/libssh.h>
#include <libssh/sftp.h>
#include "custom_exception.cpp"
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <io.h>
#include <time.h>
#include <chrono>
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::duration;
using std::chrono::milliseconds;
//using namespace std;
using std::string;
using std::cout;
using std::endl;
using std::cin;
// Good chunk size
#define MAX_XFER_BUF_SIZE 16384
int sftpDownloadFileTo(ssh_session session, const char* fullRemotePath, const char* fullLocalPath)
{
int access_type;
sftp_file file;
char buffer[MAX_XFER_BUF_SIZE * 8];//128KB
ssize_t nbytes, nwritten, rc;
int fd;
access_type = O_RDONLY;
sftp_session sftp = sftp_new(session);
if (sftp == NULL)
{
fprintf(stderr, "Error allocating SFTP session: %s\n",
ssh_get_error(session));
return SSH_ERROR;
}
rc = sftp_init(sftp);
if (rc != SSH_OK)
{
fprintf(stderr, "Error initializing SFTP session: %d.\n",
sftp_get_error(sftp));
sftp_free(sftp);
return rc;
}
file = sftp_open(sftp, fullRemotePath, access_type, 0);
if (file == NULL) {
fprintf(stderr, "Can't open file for reading: %s\n", ssh_get_error(session));
return SSH_ERROR;
}
fd = open(fullLocalPath, O_CREAT | O_RDWR, 0777);
if (fd < 0) {
fprintf(stderr, "Can't open file for writing: %s\n",
strerror(errno));
return SSH_ERROR;
}
for (;;) {
nbytes = sftp_read(file, buffer, sizeof(buffer));
if (nbytes == 0) {
break; // EOF
}
else if (nbytes < 0) {
fprintf(stderr, "Error while reading file: %s\n",
ssh_get_error(session));
sftp_close(file);
return SSH_ERROR;
}
nwritten = write(fd, buffer, nbytes);
if (nwritten != nbytes) {
fprintf(stderr, "Error writing: %s\n",
strerror(errno));
sftp_close(file);
return SSH_ERROR;
}
}
rc = sftp_close(file);
if (rc != SSH_OK) {
fprintf(stderr, "Can't close the read file: %s\n",
ssh_get_error(session));
return rc;
}
sftp_free(sftp);
return SSH_OK;
}
int main()
{
ssh_session my_ssh_session;
int rc;
int port = 22;
string password = "Ins257257";
auto host = "192.168.133.13";
auto username = "isaque.neves";
int verbosity = SSH_LOG_PROTOCOL;
// Abra a sessão e defina as opções
my_ssh_session = ssh_new();
if (my_ssh_session == NULL)
exit(-1);
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, host);
//ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
// Conecte-se ao servidor
rc = ssh_connect(my_ssh_session);
if (rc != SSH_OK)
{
fprintf(stderr, "Error connecting to host: %s\n",
ssh_get_error(my_ssh_session));
exit(-1);
}
// Autenticar-se
rc = ssh_userauth_password(my_ssh_session, username, password.c_str());
if (rc != SSH_AUTH_SUCCESS)
{
fprintf(stderr, "Error authenticating with password: %s\n",
ssh_get_error(my_ssh_session));
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
exit(-1);
}
clock_t tic = clock();
auto t1 = high_resolution_clock::now();
sftpDownloadFileTo(my_ssh_session, "/home/isaque.neves/go1.11.4.linux-amd64.tar.gz", "D:/MyDartProjects/fsbackup/libssh_binding/go1.11.4.linux-amd64.tar.gz");
auto t2 = high_resolution_clock::now();
clock_t toc = clock();
duration<double, std::milli> ms_double = t2 - t1;
std::cout << ms_double.count() << " ms\r\n";
printf("Elapsed: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
std::cout << "End\n";
return 0;
}
https://github.com/insinfo/fsbackup/tree/main/libssh_binding/libssh_c_wrapper
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <stddef.h>
#include <process.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <fstream>
#define OK_IMAGE "HTTP/1.0 200 OK\r\nContent-Type:image/gif\r\n\r\n"
#define OK_TEXT "HTTP/1.0 200 OK\r\nContent-Type:text/html\r\n\r\n"
#define NOTOK_404 "HTTP/1.0 404 Not Found\r\nContent-Type:text/html\r\n\r\n"
#define MESS_404 "<html><body><h1>FILE NOT FOUND</h1></body></html>"
#define BUF_SIZE 1024
#define PORT_NUM 80
int Count;
void handle_get(void *in_arg);
void do_end(void *server_s);
void main()
{
unsigned int server_s; // Server socket descriptor
struct sockaddr_in server_addr; // Server Internet address
unsigned int client_s; // Client socket descriptor
struct sockaddr_in client_addr; // Client Internet address
int addr_len; // Internet address length
WSADATA wsaData;
WSAStartup(MAKEWORD(1,1), &wsaData);
server_s = socket(AF_INET, SOCK_STREAM, 0);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT_NUM);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(server_s, (struct sockaddr *)&server_addr, sizeof(server_addr));
printf("HTTP Server Start.(Stop:ESC)\n");
if (_beginthread(do_end, 0, (void*)&server_s) < 0) {
printf("ERROR - Unable to create thread \n");
exit(1);
}
while(1)
{
listen(server_s, 50);
addr_len = sizeof(client_addr);
client_s = accept(server_s, (struct sockaddr*)&client_addr, addr_len);
if (client_s == -1)
{
printf("ERROR - Unable to create a socket \n");
exit(1);
}
Count++;
if (client_s == -1) {
if(server_s) printf("ERROR - Unable to create a socket \n");
exit(1);
}
if (_beginthread(handle_get, 0, (void *)client_s) < 0)
{
printf("ERROR - Unable to create a thread to handle the GET \n");
exit(1);
}
}
}
void handle_get(void *in_arg)
{
unsigned int client_s;
char in_buf[BUF_SIZE];
char out_buf[BUF_SIZE];
int fh;
int buf_len;
char command[BUF_SIZE];
char file_name[BUF_SIZE];
int retcode;
int i;
client_s = (unsigned int) in_arg;
retcode = recv(client_s, in_buf, BUF_SIZE, 0);
if (retcode <= 0)
{
printf("ERROR - Receive failed --- probably due to dropped connection \n");
Count--;
closesocket(client_s);
_endthread();
}
for (i=0; i<retcode; i++)
printf ("%c", in_buf[i]);
sscanf(in_buf, "%s %s \n", command, file_name);
if (strcmp(command, "GET") != 0) {
printf("ERROR - Not a GET --- received command = '%s' \n", command);
Count--;
closesocket(client_s);
_endthread();
}
if(file_name[1]=='\0') {
strcpy(file_name[1], "index.htm");
fh = open(file_name[1], O_RDONLY | O_BINARY, S_IREAD | S_IWRITE);
}
else
fh = open(&file_name[1], O_RDONLY | O_BINARY, S_IREAD | S_IWRITE);
if (fh == -1) {
printf("File '%s' not found --- sending an HTTP 404 \n", &file_name[1]);
strcpy(out_buf, NOTOK_404);
send(client_s, out_buf, strlen(out_buf), 0);
strcpy(out_buf, MESS_404);
send(client_s, out_buf, strlen(out_buf), 0);
Count--;
closesocket(client_s);
_endthread();
}
if (((file_name[1] == '.') && (file_name[2] == '.')) ||
(file_name[1] == '/') || (file_name[1] == '\\') ||
(file_name[2] == ':'))
{
printf("SECURITY VIOLATION --- trying to read '%s' \n", &file_name[1]);
Count--;
close(fh);
closesocket(client_s);
_endthread();
}
printf("Sending file '%s' \n\n\n", &file_name[1]);
if (strstr(file_name, ".gif") != NULL)
strcpy(out_buf, OK_IMAGE);
else
strcpy(out_buf, OK_TEXT);
send(client_s, out_buf, strlen(out_buf), 0);
while(!eof(fh)) {
buf_len = read(fh, out_buf, BUF_SIZE);
send(client_s, out_buf, buf_len, 0);
}
Count--;
close(fh);
closesocket(client_s);
_endthread();
}
void do_end(void *server_s)
{
unsigned int temp;
temp = *((unsigned int*)server_s);
while(getch()!=27);
while(Count);
*((unsigned int*)server_s) = 0;
printf("HTTP Server Stop.\n");
closesocket(temp);
WSACleanup();
exit(1);
This is a routine that allows the user to send the "index.htm" file first if the above if statement does not specify any file names. (If the current server has a file called "index.html" instead of this file, a problem occurs.)
file_name[1] contains the name of the requested file.
However, how can I provide a default page called "index.html" to a web browser if a file named "index.html" currently exists on the server and the user didn't name the file?
if(file_name[1]=='\0') {
strcpy(file_name[1], "index.htm");
fh = open(file_name[1], O_RDONLY | O_BINARY, S_IREAD | S_IWRITE);
}
Considering that it's Windows (given the #include <windows.h>), the easiest solution is to do nothing. index.html is found when opening index.htm, because the latter is the 8.3 name of index.html.
All you need to do is check the return value of sscanf(), which tells you how many tokens were read.
Disclaimer: I'm googling my way through this
Using C++
Both ends are Linux
I can see the SSH connection on the far end via TCPDUMP
No build errors or return codes other than 0 (debug says its all good)
TCPDUMP doesn't look like its transferring enough data (it is a 2MB file*)
Connect / Authenticate / Authorize are happening properly as far as I can tell
When i go to the far end looking for the file that was supposedly copied over, it doesn't exist. I've tried updatedb & locate "file", but it's nowhere to be found.
Thank you in advance for any insight possible!
SSH Functions (auth & connect) cleared out for "Minimal, Complete, and Verifiable example" since they seem to be working fine*
#include <cstdio>
#include <string>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <curl/curl.h>
#include <boost/regex.hpp>
#include <boost/system/error_code.hpp>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <x86_64-linux-gnu/sys/stat.h>
#define LIBSSH_STATIC 1
#include <libssh/libssh.h>
using namespace std;
int sourcePull(void)
{
CURL *curl;
FILE *fp;
CURLcode res;
char *url = "https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/firehol_level1.netset";
char outfilename[FILENAME_MAX] = "fireHOL";
curl = curl_easy_init();
if (curl)
{
fp = fopen(outfilename, "wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
return 0;
}
int sourceParse()
{
//Regex for IP's in sourcePull result
ifstream infile("fireHOL");
string ipAddress;
boost::regex expr1("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
boost::regex expr2("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/([0-9]|[1-2][0-9]|3[0-2]))$");
boost::smatch matches;
while (infile >> ipAddress)
{
if (boost::regex_match(ipAddress, matches, expr1))
{
ofstream checkpoint;
checkpoint.open("samp_batch.txt", ios::out | ios::app);
checkpoint << "add -t 86400 -a d -l r -o icewall ip -s " << ipAddress << endl;
checkpoint << "add -t 86400 -a r -l r -o icewall ip -d " << ipAddress << endl;
}
if (boost::regex_match(ipAddress, matches, expr2))
{
ofstream checkpoint;
checkpoint.open("samp_batch.txt", ios::out | ios::app);
checkpoint << "add -t 86400 -a d -l r -o icewall ip -s " << ipAddress << endl;
checkpoint << "add -t 86400 -a r -l r -o icewall ip -d " << ipAddress << endl;
}
}
ofstream terminate;
terminate.open("samp_batch.txt", ios::out | ios::app);
terminate << "EOF";
return 0;
}
int fileSize()
{
int size;
streampos begin, end;
ifstream inputFile("samp_batch", ios::binary);
begin = inputFile.tellg();
inputFile.seekg(0, ios::end);
end = inputFile.tellg();
inputFile.close();
return size;
}
int ssh()
{
ssh_session my_ssh_session;
ssh_scp scp;
int port = 22;
int rc;
int method;
char password[128] = { 0 };
char *banner;
//open session & set options
my_ssh_session = ssh_new();
if (my_ssh_session == NULL)
exit(-1);
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "XXX.XXX.XXX.XXX");
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "security");
//connect to server
rc = ssh_connect(my_ssh_session);
if (rc != SSH_OK)
{
fprintf(stderr, "Error connecting to host: %s\n", ssh_get_error(my_ssh_session));
ssh_free(my_ssh_session);
exit(-1);
}
//verify the servers identity
if (verify_knownHost(my_ssh_session) < 0)
{
fprintf(stdout, "unkown host\n");
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
exit(-1);
}
// Try to authenticate
rc = ssh_userauth_none(my_ssh_session, NULL);
if (rc == SSH_AUTH_ERROR) {
error(my_ssh_session);
return rc;
}
method = ssh_auth_list(my_ssh_session);
while (rc != SSH_AUTH_SUCCESS) {
// Try to authenticate with public key first
if (method & SSH_AUTH_METHOD_PUBLICKEY) {
rc = ssh_userauth_autopubkey(my_ssh_session, NULL);
if (rc == SSH_AUTH_ERROR) {
error(my_ssh_session);
return rc;
}
else if (rc == SSH_AUTH_SUCCESS) {
break;
}
}
return rc;
//SCP samp_batch file here
const int length = fileSize();
const char *fileName = "samp_batch";
ifstream inputFile(fileName);
constexpr size_t bufferSize = 1024 * 1024 * 1024;
unique_ptr<char[]> buffer(new char[bufferSize]);
const void* cvp = &buffer;
rc = ssh_scp_push_file(scp, "samp_batch", length, 0777);
if (rc != SSH_OK)
{
fprintf(stderr, "Can't open remote file: %s\n", ssh_get_error(my_ssh_session));
ssh_free(my_ssh_session);
exit(-1);
}
while (inputFile)
{
inputFile.read(buffer.get(), 1024 * 1024 * 1024);
rc = ssh_scp_write(scp, cvp, bufferSize);
if (rc != SSH_OK)
{
fprintf(stderr, "Cant write to remote file: %s\n", ssh_get_error(my_ssh_session));
ssh_free(my_ssh_session);
exit(-1);
}
}
return SSH_OK;
////execute remote command here
//ssh_free(my_ssh_session);
}
int main()
{
sourcePull();
sourceParse();
ssh();
return 0;
}
Solved this issue w/ of course, more reading...
I was not actually initializing the SCP subsystem "within" the SSH system.
By adding in;
scp = ssh_scp_new(my_ssh_session, SSH_SCP_WRITE, "/");
if (scp == NULL)
{
fprintf(stderr, "Error allocating scp session: %s\n", ssh_get_error(my_ssh_session));
return SSH_ERROR;
}
rc = ssh_scp_init(scp);
if (rc != SSH_OK)
{
fprintf(stderr, "Error initializing scp session: %s\n", ssh_get_error(my_ssh_session));
ssh_scp_free(scp);
return rc;
}
above my push/write functions, I was able to get the SCP subsystem to "start". Haven't gotten her functioning yet (SCP Status Code 1d not valid) error now, but i at least know scp is "awake" at this point...
I need to use sendfile64 to copy about 16GB of files. What I have achieved so far is
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
int main (int argc, char** argv)
{
long long src;
long long dest;
struct stat64 stat_buf;
off64_t offset = 0LL;
long long rc;
if (argc != 3) {
fprintf(stderr, "usage: %s <source> <destination>\n", argv[0]);
exit(1);
}
src = open64(argv[1], O_RDONLY);
if (src == -1) {
fprintf(stderr, "unable to open '%s': %s\n", argv[1], strerror(errno));
exit(1);
}
fstat64(src, &stat_buf);
dest = open64(argv[2], O_WRONLY|O_CREAT, stat_buf.st_mode);
if (dest == -1) {
fprintf(stderr, "unable to open '%s': %s\n", argv[2], strerror(errno));
exit(1);
}
/* copy file using sendfile */
rc = sendfile64 (dest, src, &offset, stat_buf.st_size);
if (rc == -1) {
fprintf(stderr, "error from sendfile: %s\n", strerror(errno));
exit(1);
}
if (rc != stat_buf.st_size) {
fprintf(stderr, "incomplete transfer from sendfile: %lld of %lld bytes\n",
rc,
(long long)stat_buf.st_size);
exit(1);
}
/* clean up and exit */
close(dest);
close(src);
return 0;
}
I have compiled using
g++ BigCopy2.cpp -o BigCopy2 -D_FILE_OFFSET_BITS=64 -DLARGEFILE64_SOURCE
The problem is I still can't copy more than 2GB of file.
Can someone point me where is my mistake?
You should use a loop to copy it all, sendfile() might, for various reasons, not copy all the data
with one call. As janneb points out, the return value of sendfile64 is a ssize_t, so we should not pass in more than SSIZE_MAX to sendfile, moreover the last argument to sendfile is a size_t which would be 32 bit on 32 bit platforms.
/* copy file using sendfile */
while (offset < stat_buf.st_size) {
size_t count;
off64_t remaining = stat_buf.st_size- offset;
if (remaining > SSIZE_MAX)
count = SSIZE_MAX;
else
count = remaining;
rc = sendfile64 (dest, src, &offset, count);
if (rc == 0) {
break;
}
if (rc == -1) {
fprintf(stderr, "error from sendfile: %s\n", strerror(errno));
exit(1);
}
}
if (offset != stat_buf.st_size) {
fprintf(stderr, "incomplete transfer from sendfile: %lld of %lld bytes\n",
rc,
(long long)stat_buf.st_size);
exit(1);
}
Note that you can replace all your 64 bit variants, off64_t, stat64, sendfile64, with off_t, stat, sendfile. As long as you have the -D_FILE_OFFSET_BITS=64 flag, that define will do the right thing and transform off_t to off64_t, sendfile to sendfile64 and so on if those types and functions are not already 64 bits (such as on 32 bit architectures).
I have used the simple C code of pulse audio for playback and record and it worked fine. But when I converted it to C++ it doesn't work. I am pasting both the codes. Please help. The C++ code doesn't show any error but doesn't playback any sound. But C++ code plays the recorded sound.
N.B: I am using 64 bit CentOS 6.2
C++ Code:
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include "pulse/simple.h"
#include "pulse/error.h"
using namespace std;
#define BUFSIZE 32
int error;
/* The Sample format to use */
class AudioCapt
{
public:
AudioCapt();
void RecordSound(int argc,char *argv[],pa_simple *s_in,pa_sample_spec &ss,pa_simple *s_out,uint8_t buf[],ssize_t r);
void PlaybackSound(int argc, char*argv[],pa_simple *s_out,pa_sample_spec &ss,uint8_t buf[],ssize_t r);
};
void AudioCapt::RecordSound(int argc, char*argv[],pa_simple *s_in,pa_sample_spec &ss,pa_simple *s_out,uint8_t buf[],ssize_t r)
{
printf("Audio Capturing \n");
if (!(s_in = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) {
fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
}
if (pa_simple_read(s_in, buf, sizeof(buf), &error) < 0) {
fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));
}
printf("Buffer :::: %d\n",buf[0]);
}
void AudioCapt::PlaybackSound(int argc, char*argv[],pa_simple *s_out,pa_sample_spec &ss,uint8_t buf[],ssize_t r)
{
printf("Audio PlayBack \n");
printf("Play Buffer::: %d\n",buf[0]);
if (!(s_out = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) {
fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
}
/* ... and play it (Modified) */
if (pa_simple_write(s_out, buf, sizeof(buf), &error) < 0) {
fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error));
}
/* Make sure that every single sample was played */
if (pa_simple_drain(s_out, &error) < 0) {
fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error));
}
}
int main(int argc, char * argv[])
{
pa_sample_spec ss;
ss.format = PA_SAMPLE_S16LE;
ss.rate = 44100;
ss.channels = 2;
pa_simple *s_in, *s_out = NULL;
AudioCapt *m_pMyObject;
for(;;)
{
uint8_t buf[BUFSIZE];
ssize_t r;
m_pMyObject->RecordSound(argc,argv,s_in,ss,s_out,buf,r);
m_pMyObject->PlaybackSound(argc,argv,s_out,ss,buf,r);
}
return 0;
}
C Code:
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#define BUFSIZE 32
int main(int argc, char*argv[]) {
/* The Sample format to use */
static const pa_sample_spec ss = {
.format = PA_SAMPLE_S16LE,
.rate = 44100,
.channels = 2
};
pa_simple *s_in, *s_out = NULL;
int ret = 1;
int error;
/* Create a new playback stream */
if (!(s_out = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) {
fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
goto finish;
}
if (!(s_in = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) {
fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
goto finish;
}
for (;;) {
uint8_t buf[BUFSIZE];
ssize_t r;
#if 1
pa_usec_t latency;
if ((latency = pa_simple_get_latency(s_in, &error)) == (pa_usec_t) -1) {
fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error));
goto finish;
}
fprintf(stderr, "In: %0.0f usec \r\n", (float)latency);
if ((latency = pa_simple_get_latency(s_out, &error)) == (pa_usec_t) -1) {
fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error));
goto finish;
}
fprintf(stderr, "Out: %0.0f usec \r\n", (float)latency);
#endif
if (pa_simple_read(s_in, buf, sizeof(buf), &error) < 0) {
fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));
goto finish;
}
printf("Buffer :::: %d\n",buf[0]);
/* ... and play it */
if (pa_simple_write(s_out, buf, sizeof(buf), &error) < 0) {
fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error));
goto finish;
}
}
/* Make sure that every single sample was played */
if (pa_simple_drain(s_out, &error) < 0) {
fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error));
goto finish;
}
ret = 0;
finish:
if (s_in)
pa_simple_free(s_in);
if (s_out)
pa_simple_free(s_out);
return ret;
}
In RecordSound and PlaybackSound you're initializing temporary variables with pa_simple_new. This value is lost once function returns, and you passing NULL to next one.
I would suggest turning on compiler checks and fixing all errors and warnings issued by your compiler on this code.
To start, m_pMyObject is never initialized, so using it in the call to RecordSound will mean passing an uninitialized value as "this" to the method. This is generally a bad thing to do.
In RecordSound and PlaybackSound, you use size(buf) to tell the library how many bytes to read/write. The parameter buf is a pointer to uint8_t. So the compiler fills in the size of the pointer (probably 8 on a 64-bit machine). In these methods you should use the parameter you have for the size. In the calls, pass sizeof(buf) to that parameter.
I don't know how many streams the library can create before running out of memory/resources. Each call to RecordSound creates a recording stream, and the PlaybackSound creates a playback stream. These streams are never freed.
So to summarize, If using the uninitialized value to call the RecordSound method does not cause the program to crash, it will create a recording stream and record a couple samples, then it will create a playback stream and play back those two samples. Then it will try doing all this again.