bool tr_sys_file_write_fmt (tr_sys_file_t handle, const char * format, tr_error ** error, ...) { bool ret = false; char * buffer; va_list args; assert (handle != TR_BAD_SYS_FILE); assert (format != NULL); va_start (args, error); buffer = tr_strdup_vprintf (format, args); va_end (args); if (buffer != NULL) { ret = tr_sys_file_write (handle, buffer, strlen (buffer), NULL, error); tr_free (buffer); } else { tr_error_set_literal (error, 0, "Unable to format message."); } return ret; }
void tr_logAddDeep (const char * file, int line, const char * name, const char * fmt, ...) { const tr_sys_file_t fp = tr_logGetFile (); if (fp != TR_BAD_SYS_FILE || IsDebuggerPresent ()) { va_list args; char timestr[64]; char * message; size_t message_len; struct evbuffer * buf = evbuffer_new (); char * base = tr_sys_path_basename (file, NULL); evbuffer_add_printf (buf, "[%s] ", tr_logGetTimeStr (timestr, sizeof (timestr))); if (name) evbuffer_add_printf (buf, "%s ", name); va_start (args, fmt); evbuffer_add_vprintf (buf, fmt, args); va_end (args); evbuffer_add_printf (buf, " (%s:%d)" TR_NATIVE_EOL_STR, base, line); /* FIXME (libevent2) ifdef this out for nonwindows platforms */ message = evbuffer_free_to_str (buf, &message_len); OutputDebugStringA (message); if (fp != TR_BAD_SYS_FILE) tr_sys_file_write (fp, message, message_len, NULL, NULL); tr_free (message); tr_free (base); } }
bool tr_sys_file_write_line (tr_sys_file_t handle, const char * buffer, tr_error ** error) { bool ret; assert (handle != TR_BAD_SYS_FILE); assert (buffer != NULL); ret = tr_sys_file_write (handle, buffer, strlen (buffer), NULL, error); if (ret) ret = tr_sys_file_write (handle, TR_NATIVE_EOL_STR, TR_NATIVE_EOL_STR_SIZE, NULL, error); return ret; }
void libttest_zero_torrent_populate (tr_torrent * tor, bool complete) { tr_file_index_t i; for (i=0; i<tor->info.fileCount; ++i) { int err; uint64_t j; tr_sys_file_t fd; char * path; char * dirname; const tr_file * file = &tor->info.files[i]; if (!complete && (i==0)) path = tr_strdup_printf ("%s%c%s.part", tor->currentDir, TR_PATH_DELIMITER, file->name); else path = tr_strdup_printf ("%s%c%s", tor->currentDir, TR_PATH_DELIMITER, file->name); dirname = tr_sys_path_dirname (path, NULL); tr_sys_dir_create (dirname, TR_SYS_DIR_CREATE_PARENTS, 0700, NULL); fd = tr_sys_file_open (path, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0600, NULL); for (j=0; j<file->length; ++j) tr_sys_file_write (fd, ((!complete) && (i==0) && (j<tor->info.pieceSize)) ? "\1" : "\0", 1, NULL, NULL); tr_sys_file_close (fd, NULL); tr_free (dirname); tr_free (path); path = tr_torrentFindFile (tor, i); assert (path != NULL); err = errno; assert (tr_sys_path_exists (path, NULL)); errno = err; tr_free (path); } libttest_sync (); libttest_blockingTorrentVerify (tor); if (complete) assert (tr_torrentStat(tor)->leftUntilDone == 0); else assert (tr_torrentStat(tor)->leftUntilDone == tor->info.pieceSize); }
static bool preallocate_file_full (tr_sys_file_t fd, uint64_t length, tr_error ** error) { tr_error * my_error = NULL; if (length == 0) return true; if (tr_sys_file_preallocate (fd, length, 0, &my_error)) return true; dbgmsg ("Preallocating (full, normal) failed (%d): %s", my_error->code, my_error->message); if (!TR_ERROR_IS_ENOSPC (my_error->code)) { uint8_t buf[4096]; bool success = true; memset (buf, 0, sizeof (buf)); tr_error_clear (&my_error); /* fallback: the old-fashioned way */ while (success && length > 0) { const uint64_t thisPass = MIN (length, sizeof (buf)); uint64_t bytes_written; success = tr_sys_file_write (fd, buf, thisPass, &bytes_written, &my_error); length -= bytes_written; } if (success) return true; dbgmsg ("Preallocating (full, fallback) failed (%d): %s", my_error->code, my_error->message); } tr_error_propagate (error, &my_error); return false; }