}
struct stat sbuf;
- (void)fstat(fd, &sbuf);
+ if (fstat(fd, &sbuf) == -1)
+ {
+ std::cerr << "memcp " << argv[optind] << " " << strerror(errno) << std::endl;
+ optind++;
+ exit_code= EXIT_FAILURE;
+ continue;
+ }
char *ptr= rindex(argv[optind], '/');
if (ptr)
ptr, opt_flags, (unsigned long)opt_expires);
}
- char *file_buffer_ptr;
- if ((file_buffer_ptr= (char *)malloc(sizeof(char) * (size_t)sbuf.st_size)) == NULL)
+ // The file may be empty
+ char *file_buffer_ptr= NULL;
+ if (sbuf.st_size > 0)
{
- std::cerr << "Error allocating file buffer(" << strerror(errno) << ")" << std::endl;
- close(fd);
- exit(EXIT_FAILURE);
- }
+ if ((file_buffer_ptr= (char *)malloc(sizeof(char) * (size_t)sbuf.st_size)) == NULL)
+ {
+ std::cerr << "Error allocating file buffer(" << strerror(errno) << ")" << std::endl;
+ close(fd);
+ exit(EXIT_FAILURE);
+ }
- ssize_t read_length;
- if ((read_length= ::read(fd, file_buffer_ptr, (size_t)sbuf.st_size)) == -1)
- {
- std::cerr << "Error while reading file " << file_buffer_ptr << " (" << strerror(errno) << ")" << std::endl;
- close(fd);
- exit(EXIT_FAILURE);
- }
+ ssize_t read_length;
+ if ((read_length= ::read(fd, file_buffer_ptr, (size_t)sbuf.st_size)) == -1)
+ {
+ std::cerr << "Error while reading file " << file_buffer_ptr << " (" << strerror(errno) << ")" << std::endl;
+ close(fd);
+ free(file_buffer_ptr);
+ exit(EXIT_FAILURE);
+ }
- if (read_length != sbuf.st_size)
- {
- std::cerr << "Failure while reading file. Read length was not equal to stat() length" << std::endl;
- close(fd);
- exit(EXIT_FAILURE);
+ if (read_length != sbuf.st_size)
+ {
+ std::cerr << "Failure while reading file. Read length was not equal to stat() length" << std::endl;
+ close(fd);
+ free(file_buffer_ptr);
+ exit(EXIT_FAILURE);
+ }
}
memcached_return_t rc;
if (memcached_failed(rc))
{
std::cerr << "Error occrrured during memcached_set(): " << memcached_last_error_message(memc) << std::endl;
- ::close(fd);
exit_code= EXIT_FAILURE;
}
Application::error_t Application::join()
{
- pid_t waited_pid= waitpid(_pid, &_status, 0);
+ pid_t waited_pid= waitpid(_pid, &_status, WUNTRACED);
slurp();
if (waited_pid == _pid and WIFEXITED(_status) == false)
{
namespace libtest {
-std::string create_tmpfile(const std::string& name)
+std::string create_tmpfile(const std::string& name, int& fd)
{
libtest::vchar_t file_buffer;
file_buffer.resize(FILENAME_MAX);
int length= snprintf(&file_buffer[0], file_buffer.size(), "var/tmp/%s.XXXXXX", name.c_str());
fatal_assert(length > 0);
- int fd;
if ((fd= mkstemp(&file_buffer[0])) == -1)
{
throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "mkstemp() failed on %s with %s", &file_buffer[0], strerror(errno));
}
- close(fd);
- unlink(&file_buffer[0]);
return &file_buffer[0];
}
+std::string create_tmpfile(const std::string& name)
+{
+ int fd;
+ std::string ret_file= create_tmpfile(name, fd);
+ close(fd);
+ unlink(ret_file.c_str());
+
+ return ret_file.c_str();
+}
+
} // namespace libtest
namespace libtest {
+std::string create_tmpfile(const std::string&, int&);
std::string create_tmpfile(const std::string&);
} // namespace libtest
check_PROGRAMS+= tests/memdump
noinst_PROGRAMS+= tests/memdump
+test-memcp: tests/memcp
+ tests/memcp
+
+gdb-memcp: tests/memcp
+ @$(GDB_COMMAND) tests/memcp
+
test-memstat: tests/memstat
tests/memstat
#include <libtest/test.hpp>
#include <libmemcached-1.0/memcached.h>
+#include <sys/stat.h>
+
using namespace libtest;
#ifndef __INTEL_COMPILER
static test_return_t server_test(void *)
{
+ int fd;
+ std::string tmp_file= create_tmpfile("memcp", fd);
+ ASSERT_TRUE(tmp_file.c_str());
+ struct stat buf;
+ ASSERT_EQ(fstat(fd, &buf), 0);
+ ASSERT_EQ(buf.st_size, 0);
+
char buffer[1024];
snprintf(buffer, sizeof(buffer), "--servers=localhost:%d", int(default_port()));
- const char *args[]= { buffer, 0 };
+ const char *args[]= { buffer, tmp_file.c_str(), 0 };
test_compare(EXIT_SUCCESS, exec_cmdline(executable, args, true));
+ close(fd);
+ unlink(tmp_file.c_str());
return TEST_SUCCESS;
}