Пример #1
0
void
blockstore_write_headers(struct blockstore *bs)
{
   btc_block_header *buf;
   struct blockentry *e;
   size_t numWritten;
   uint32 count;
   uint32 numhdr;
   int res;

   e = bs->best_chain;
   count = 0;
   while (e && e->written == 0) {
      e = e->prev;
      count++;
   }
   ASSERT(count < 2048);
   numhdr = count;
   if (count == 0) {
      return;
   }

   buf = safe_malloc(count * sizeof *buf);

   e = bs->best_chain;
   while (e && e->written == 0) {
      count--;
      memcpy(buf + count, &e->header, sizeof e->header);
      e->written = 1;
      e = e->prev;
   }

   ASSERT(count == 0);
   ASSERT(bs->blockSet);
   ASSERT_ON_COMPILE(sizeof *buf == 80);

   res = file_pwrite(bs->blockSet->desc, bs->blockSet->filesize,
                     buf, numhdr * sizeof *buf, &numWritten);
   free(buf);

   if (res != 0 || numWritten != numhdr * sizeof *buf) {
      Warning(LGPFX" failed to write %u block entries.\n", numhdr);
      return;
   }

   bs->blockSet->filesize += numWritten;
}
Пример #2
0
int main(int argc, char *argv[])
{
	int ret;
	struct stat st;
	struct file_context *src;
	struct file_context *dst;
	uint64_t off;
	int64_t count;
	
#ifdef WIN32
	if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
		printf("Failed to start Winsock2\n");
		return 10;
	}
#endif

#ifdef AROS
	aros_init_socket();
#endif

	if (argc != 3) {
		usage();
	}

	src = open_file(argv[1], O_RDONLY);
	if (src == NULL) {
		fprintf(stderr, "Failed to open %s\n", argv[1]);
		return 10;
	}

	dst = open_file(argv[2], O_WRONLY|O_CREAT|O_TRUNC);
	if (dst == NULL) {
		fprintf(stderr, "Failed to open %s\n", argv[2]);
		free_file_context(src);
		return 10;
	}

	if (fstat_file(src, &st) != 0) {
		fprintf(stderr, "Failed to fstat source file\n");
		free_file_context(src);
		free_file_context(dst);
		return 10;
	}

	off = 0;
	while (off < st.st_size) {
		count = st.st_size - off;
		if (count > BUFSIZE) {
			count = BUFSIZE;
		}
		count = file_pread(src, buf, count, off);
		if (count < 0) {
			fprintf(stderr, "Failed to read from source file\n");
			free_file_context(src);
			free_file_context(dst);
			return 10;
		}
		count = file_pwrite(dst, buf, count, off);
		if (count < 0) {
			fprintf(stderr, "Failed to write to dest file\n");
			free_file_context(src);
			free_file_context(dst);
			return 10;
		}

		off += count;
	}
	printf("copied %d bytes\n", (int)off);

	free_file_context(src);
	free_file_context(dst);

	return 0;
}
Пример #3
0
static void aio_write_worker(FAR void *arg)
{
  FAR struct aio_container_s *aioc = (FAR struct aio_container_s *)arg;
  FAR struct aiocb *aiocbp;
  pid_t pid;
#ifdef CONFIG_PRIORITY_INHERITANCE
  uint8_t prio;
#endif
  ssize_t nwritten = 0;
#ifdef AIO_HAVE_FILEP
  int oflags;
#endif

  /* Get the information from the container, decant the AIO control block,
   * and free the container before starting any I/O.  That will minimize
   * the delays by any other threads waiting for a pre-allocated container.
   */

  DEBUGASSERT(aioc && aioc->aioc_aiocbp);
  pid    = aioc->aioc_pid;
#ifdef CONFIG_PRIORITY_INHERITANCE
  prio   = aioc->aioc_prio;
#endif
  aiocbp = aioc_decant(aioc);

#if defined(AIO_HAVE_FILEP) && defined(AIO_HAVE_PSOCK)
  if (aiocbp->aio_fildes < CONFIG_NFILE_DESCRIPTORS)
#endif
#ifdef AIO_HAVE_FILEP
    {
      /* Call fcntl(F_GETFL) to get the file open mode. */

      oflags = file_fcntl(aioc->u.aioc_filep, F_GETFL);
      if (oflags < 0)
        {
          int errcode = get_errno();
          fdbg("ERROR: fcntl failed: %d\n", errcode);
          aiocbp->aio_result = -errcode;
          goto errout;
        }

      /* Perform the write using:
       *
       *   u.aioc_filep - File structure pointer
       *   aio_buf      - Location of buffer
       *   aio_nbytes   - Length of transfer
       *   aio_offset   - File offset
       */

      /* Check if O_APPEND is set in the file open flags */

      if ((oflags & O_APPEND) != 0)
        {
          /* Append to the current file position */

          nwritten = file_write(aioc->u.aioc_filep,
                                (FAR const void *)aiocbp->aio_buf,
                                aiocbp->aio_nbytes);
        }
      else
        {
          nwritten = file_pwrite(aioc->u.aioc_filep,
                                 (FAR const void *)aiocbp->aio_buf,
                                 aiocbp->aio_nbytes,
                                 aiocbp->aio_offset);
        }
    }
#endif
#if defined(AIO_HAVE_FILEP) && defined(AIO_HAVE_PSOCK)
  else
#endif
#ifdef AIO_HAVE_PSOCK
    {
      /* Perform the send using:
       *
       *   u.aioc_psock - Socket structure pointer
       *   aio_buf      - Location of buffer
       *   aio_nbytes   - Length of transfer
       */

      nwritten = psock_send(aioc->u.aioc_psock,
                            (FAR const void *)aiocbp->aio_buf,
                            aiocbp->aio_nbytes, 0);
    }
#endif

  /* Check the result of the write */

  if (nwritten < 0)
    {
      int errcode = get_errno();
      fdbg("ERROR: write/pwrite failed: %d\n", errcode);
      DEBUGASSERT(errcode > 0);
      aiocbp->aio_result = -errcode;
    }
  else
    {
      aiocbp->aio_result = nwritten;
    }

#ifdef AIO_HAVE_FILEP
errout:
#endif

  /* Signal the client */

  (void)aio_signal(pid, aiocbp);

#ifdef CONFIG_PRIORITY_INHERITANCE
  /* Restore the low priority worker thread default priority */

  lpwork_restorepriority(prio);
#endif
}
Пример #4
0
int file_execute_request(sb_request_t *sb_req, int thread_id)
{
  FILE_DESCRIPTOR    fd;
  sb_file_request_t *file_req = &sb_req->u.file_request;
  log_msg_t          msg;
  log_msg_oper_t     op_msg;
  
  if (sb_globals.debug)
  {
    log_text(LOG_DEBUG,
             "Executing request, operation: %d, file_id: %d, pos: %d, "
             "size: %d",
             file_req->operation,
             file_req->file_id,
             (int)file_req->pos,
             (int)file_req->size);
  }
  
  /* Check request parameters */
  if (file_req->file_id > num_files)
  {
    log_text(LOG_FATAL, "Incorrect file discovered in request");
    return 1;
  }
  if (file_req->pos + file_req->size > file_size)
  {
    log_text(LOG_FATAL, "Too large position discovered in request!");
    return 1;
  }
  
  fd = files[file_req->file_id];

  /* Prepare log message */
  msg.type = LOG_MSG_TYPE_OPER;
  msg.data = &op_msg;

  switch (file_req->operation) {
    case FILE_OP_TYPE_NULL:
      log_text(LOG_FATAL, "Execute of NULL request called !, aborting");
      return 1;
    case FILE_OP_TYPE_WRITE:

      /* Store checksum and offset in a buffer when in validation mode */
      if (sb_globals.validate)
        file_fill_buffer(buffer, file_req->size, file_req->pos);
                         
      LOG_EVENT_START(msg, thread_id);
      if(file_pwrite(file_req->file_id, buffer, file_req->size, file_req->pos,
                     thread_id)
         != (ssize_t)file_req->size)
      {
        log_errno(LOG_FATAL, "Failed to write file! file: " FD_FMT " pos: %lld", 
                  fd, (long long)file_req->pos);
        return 1;
      }
      /* Check if we have to fsync each write operation */
      if (file_fsync_all)
      {
        if (file_fsync(file_req->file_id, thread_id))
        {
          log_errno(LOG_FATAL, "Failed to fsync file! file: " FD_FMT, fd);
          return 1;
        }
      }
      LOG_EVENT_STOP(msg, thread_id);

      SB_THREAD_MUTEX_LOCK();
      write_ops++;
      real_write_ops++;
      bytes_written += file_req->size;
      if (file_fsync_all)
        other_ops++;
      SB_THREAD_MUTEX_UNLOCK();

      break;
    case FILE_OP_TYPE_READ:
      LOG_EVENT_START(msg, thread_id);
      if(file_pread(file_req->file_id, buffer, file_req->size, file_req->pos,
                    thread_id)
         != (ssize_t)file_req->size)
      {
        log_errno(LOG_FATAL, "Failed to read file! file: " FD_FMT " pos: %lld",
                  fd, (long long)file_req->pos);
        return 1;
      }
      LOG_EVENT_STOP(msg, thread_id);

      /* Validate block if run with validation enabled */
      if (sb_globals.validate &&
          file_validate_buffer(buffer, file_req->size, file_req->pos))
      {
        log_text(LOG_FATAL,
          "Validation failed on file " FD_FMT ", block offset 0x%x, exiting...",
           file_req->file_id, file_req->pos);
        return 1;
      }
      
      SB_THREAD_MUTEX_LOCK();
      read_ops++;
      real_read_ops++;
      bytes_read += file_req->size;

      SB_THREAD_MUTEX_UNLOCK();

      break;
    case FILE_OP_TYPE_FSYNC:
      /* Ignore fsync requests if we are already fsync'ing each operation */
      if (file_fsync_all)
        break;
      if(file_fsync(file_req->file_id, thread_id))
      {
        log_errno(LOG_FATAL, "Failed to fsync file! file: " FD_FMT, fd);
        return 1;
      }
    
      SB_THREAD_MUTEX_LOCK();
      other_ops++;
      SB_THREAD_MUTEX_UNLOCK();
    
      break;         
    default:
      log_text(LOG_FATAL, "Execute of UNKNOWN file request type called (%d)!, "
               "aborting", file_req->operation);
      return 1;
  }
  return 0;

}
Пример #5
0
int
config_write(struct config *conf,
             const char    *filename)
{
   struct file_descriptor *fd;
   struct KeyValuePair *e;
   uint64 offset;
   int res;

   res = 0;
   fd = NULL;
   ASSERT(conf);
   if (filename == NULL) {
      ASSERT(conf->fileName);
   } else {
      free(conf->fileName);
      conf->fileName = safe_strdup(filename);
   }
   res = file_open(conf->fileName, FALSE, FALSE, &fd);
   if (res != 0) {
      Log(LGPFX" Failed to open config '%s': %s (%d)\n",
          filename, strerror(res), res);
      return res;
   }

   res = file_truncate(fd, 0);
   if (res != 0) {
      Log(LGPFX" Failed to truncate '%s': %s (%d)\n",
          filename, strerror(res), res);
      goto exit;
   }

   e = conf->list;
   offset = 0;

   while (e) {
      size_t numBytes;
      char *s = NULL;

      if (e->save == 0) {
         Log(LGPFX" not writing key '%s'\n", e->key);
         e = e->next;
         continue;
      }

      switch (e->type) {
      case CONFIG_KV_INT64:
         s = safe_asprintf("%s = \"%lld\"\n", e->key, e->u.val);
         break;
      case CONFIG_KV_BOOL:
         s = safe_asprintf("%s = \"%s\"\n", e->key, e->u.trueOrFalse ? "TRUE" : "FALSE");
         break;
      default:
         ASSERT(e->type == CONFIG_KV_UNKNOWN || e->type == CONFIG_KV_STRING);
         if (e->u.str) {
            s = safe_asprintf("%s = \"%s\"\n", e->key, e->u.str);
         }
      }

      if (s) {
         numBytes = 0;
         res = file_pwrite(fd, offset, s, strlen(s), &numBytes);
         if (res != 0 || numBytes != strlen(s)) {
            Log(LGPFX" Failed to pwrite %zd bytes: %s (%d)\n",
                strlen(s), strerror(res), res);
            free(s);
            goto exit;
         }
         offset += numBytes;
         free(s);
      }
      e = e->next;
   }

exit:
   if (res != 0) {
      // XXX: consider cleaning-up.
   }
   if (fd) {
      file_close(fd);
   }
   return res;
}