static find_tp_ret_t blind_scan_get_tp_info(void *p_data, 
  nc_channel_info_t *p_channel)
{
  bl_scan_priv_t *p_priv = (bl_scan_priv_t *)p_data;
  find_tp_ret_t ret = FIND_SUC;
  
  if(p_priv->b_disp_moving)
  {
    u32 interval = (mtos_ticks_get() * 10) - p_priv->disp_move_start_time;
    if(interval > p_priv->disp_move_time)
    {
      //Moving Done
      p_priv->b_disp_moving = FALSE;
    }
    else
    {
      //Skeep Moving 
      return FIND_BUSY;
    }
  }

  ret = find_tp(p_priv, p_channel);
  notify_progress(progress_calc(p_priv));
  if(FIND_SUC == ret) //found a new tp
  {
    if(p_priv->hook.check_tp(p_channel) != SUCCESS)
    {
      //if the tp isn't need scan, skip it
      p_priv->cur_channel_idx++;
      return FIND_BUSY;
    }
    else
    {
      return FIND_SUC;
    }
  }
  else if(FIND_BUSY == ret) //need retry, go on
  {
    return FIND_BUSY;
  }
  else if(FIND_DONE == ret) //cur sat found finish
  {
    //the current satellite scan done, goto the next one
    p_priv->cur_sat_idx++;
    if(p_priv->cur_sat_idx < p_priv->total_preset_sat)//has the next
    {
      //goto the next satellite
      switch_to_sat(p_priv, p_priv->cur_sat_idx);
      return FIND_BUSY;
    }
    else
    {
      notify_progress(100);
      return FIND_DONE;
    }
  }

  return FIND_DONE;
}
static RET_CODE blind_scan_tp_done(void *p_data, void *p_tp_info)
{
  bl_scan_priv_t *p_priv = (bl_scan_priv_t *)p_data;
  scan_tp_info_t *p_tp = (scan_tp_info_t *)p_tp_info;
  scan_tp_info_i_t tp_info = {{0}};
  u8 ret = 0;
  //Notify progress
  notify_progress(progress_calc(p_data));

  tp_info.pg_num = p_tp->pg_num;
  tp_info.p_pg_list = p_tp->p_pg_list;
  BS_UNLOCK();
  
  /*BS_DBG("%s,line %d:tp [%d,%d]\n",__FUNCTION__,__LINE__,
    p_tp->tp_info.channel_info.frequency,p_tp->tp_info.channel_info.param.dvbs.symbol_rate);*/
  //return p_priv->hook.process_pg_list(&tp_info);
#ifdef BS_DEBUG_ON
  g_tpdone_ticks = mtos_ticks_get();
  /*BS_DBG("***************tp done ticks %d,used ticks[%d]\n",
    g_tpdone_ticks, g_tpdone_ticks - g_lock_ticks);*/
  g_used_ticks += (g_tpdone_ticks - g_lock_ticks);
#endif
  ret = p_priv->hook.process_pg_list(&tp_info);
  log_perf(LOG_AP_SCAN,PERF_DO_LOCK, 0, 0);
  return ret;
}
Esempio n. 3
0
/* Generate progress notification messages in order to test progress bars. */
static char *
debug_progress (const char *subcmd, size_t argc, char *const *const argv)
{
  if (argc < 1) {
  error:
    reply_with_error ("progress: expecting arg (time in seconds as string)");
    return NULL;
  }

  char *secs_str = argv[0];
  unsigned secs;
  if (sscanf (secs_str, "%u", &secs) != 1)
    goto error;
  if (secs == 0 || secs > 1000000) { /* RHBZ#816839 */
    reply_with_error ("progress: argument is 0, less than 0, or too large");
    return NULL;
  }

  unsigned i;
  unsigned tsecs = secs * 10;   /* 1/10ths of seconds */
  for (i = 1; i <= tsecs; ++i) {
    usleep (100000);
    notify_progress ((uint64_t) i, (uint64_t) tsecs);
  }

  char *ret = strdup ("ok");
  if (ret == NULL) {
    reply_with_perror ("strdup");
    return NULL;
  }

  return ret;
}
Esempio n. 4
0
void
pulse_mode_end (void)
{
  pulse_mode_cancel ();         /* Cancel the itimer. */

  notify_progress (1, 1);
}
Esempio n. 5
0
/* Generate progress notification messages in order to test progress bars. */
static char *
debug_progress (const char *subcmd, size_t argc, char *const *const argv)
{
  uint64_t secs, rate = 0;
  char *ret;

  if (argc < 1) {
  error:
    reply_with_error ("progress: expecting one or more args: time in seconds [, rate in microseconds]");
    return NULL;
  }

  if (sscanf (argv[0], "%" SCNu64, &secs) != 1)
    goto error;
  if (secs == 0 || secs > 1000000) { /* RHBZ#816839 */
    reply_with_error ("progress: argument is 0, less than 0, or too large");
    return NULL;
  }

  if (argc >= 2) {
    if (sscanf (argv[1], "%" SCNu64, &rate) != 1)
      goto error;
    if (rate == 0 || rate > 1000000) {
      reply_with_error ("progress: rate is 0 or too large");
      return NULL;
    }
  }

  /* Note the inner loops go to '<= limit' because we want to ensure
   * that the final 100% completed message is set.
   */
  if (rate == 0) {              /* Ordinary rate-limited progress messages. */
    uint64_t tsecs = secs * 10; /* 1/10ths of seconds */
    uint64_t i;

    for (i = 1; i <= tsecs; ++i) {
      usleep (100000);
      notify_progress (i, tsecs);
    }
  }
  else {                        /* Send messages at a given rate. */
    uint64_t usecs = secs * 1000000; /* microseconds */
    uint64_t i;
    struct timeval now;

    for (i = rate; i <= usecs; i += rate) {
      usleep (rate);
      gettimeofday (&now, NULL);
      notify_progress_no_ratelimit (i, usecs, &now);
    }
  }

  ret = strdup ("ok");
  if (ret == NULL) {
    reply_with_perror ("strdup");
    return NULL;
  }

  return ret;
}
static RET_CODE on_unlocked_evt(bl_scan_priv_t *p_priv, nim_channel_info_t *p_ch)
{
  BS_DBG("\n $$$$ on_unlocked_evt 1\n");  
  if(cancel_flag)
  {
    if(pause_flag)
      BS_UNLOCK_PAUSE();
    return SUCCESS;
  }
  BS_DBG("\n $$$$ on_unlocked_evt 2\n");  
  memcpy(& p_priv->cur_channel_unlocked, p_ch, sizeof(nim_channel_info_t));
  //add perf log
  log_perf(LOG_AP_SCAN, PERF_GET_TP_END,
  p_ch->frequency,0);

  log_perf(LOG_AP_SCAN, PERF_UNLOCK_EVT,
   p_ch->frequency, 0);
  log_perf(LOG_AP_SCAN, PERF_GET_ONETP_END,0,0);
  log_perf(LOG_AP_SCAN, PERF_GET_ONETP_BEGIN,0,0);
  log_perf(LOG_AP_SCAN, PERF_GET_TP_BEGIN,0,0);

  log_perf(LOG_AP_SCAN, PERF_SAVE_DB_START,UNLOCK_TP_DB,0);
  log_perf(LOG_AP_SCAN, PERF_SAVE_DB_DONE,UNLOCK_TP_DB,0);
  //Notify progress 
  notify_progress(progress_calc(p_priv));

  return SUCCESS;
}
Esempio n. 7
0
int
do_zero_device (const char *device)
{
  int64_t ssize = do_blockdev_getsize64 (device);
  if (ssize == -1)
    return -1;
  uint64_t size = (uint64_t) ssize;

  int fd = open (device, O_RDWR|O_CLOEXEC);
  if (fd == -1) {
    reply_with_perror ("%s", device);
    return -1;
  }

  char buf[sizeof zero_buf];

  uint64_t pos = 0;

  while (pos < size) {
    uint64_t n64 = size - pos;
    size_t n;
    if (n64 > sizeof buf)
      n = sizeof buf;
    else
      n = (size_t) n64; /* safe because of if condition */

    /* Check if the block is already zero before overwriting it. */
    ssize_t r;
    r = pread (fd, buf, n, pos);
    if (r == -1) {
      reply_with_perror ("pread: %s at offset %" PRIu64, device, pos);
      close (fd);
      return -1;
    }

    if (!is_zero (buf, sizeof buf)) {
      r = pwrite (fd, zero_buf, n, pos);
      if (r == -1) {
        reply_with_perror ("pwrite: %s (with %" PRId64 " bytes left to write)",
                           device, size);
        close (fd);
        return -1;
      }
      pos += r;
    }
    else
      pos += n;

    notify_progress (pos, size);
  }

  if (close (fd) == -1) {
    reply_with_perror ("close: %s", device);
    return -1;
  }

  return 0;
}
static RET_CODE blind_scan_tp_done(void *p_data, void *p_tp_info)
{
  bl_scan_priv_t *p_priv = (bl_scan_priv_t *)p_data;
  scan_tp_info_t *p_tp = (scan_tp_info_t *)p_tp_info;
  scan_tp_info_i_t tp_info = {{0}};
  
  //Notify progress 
  notify_progress(progress_calc(p_data));
  
  tp_info.pg_num = p_tp->pg_num;
  tp_info.p_pg_list = p_tp->p_pg_list;
  p_priv->cur_channel_idx++;
  
  return p_priv->hook.process_pg_list(&tp_info);
}
Esempio n. 9
0
int
do_fill (int c, int len, const char *path)
{
  int fd;
  ssize_t r;
  size_t len_sz;
  size_t n;
  char buf[BUFSIZ];

  if (c < 0 || c > 255) {
    reply_with_error ("%d: byte number must be in range 0..255", c);
    return -1;
  }
  memset (buf, c, BUFSIZ);
  if (len < 0) {
    reply_with_error ("%d: length is < 0", len);
    return -1;
  }
  len_sz = (size_t) len;

  CHROOT_IN;
  fd = open (path, O_WRONLY | O_CREAT | O_NOCTTY, 0666);
  CHROOT_OUT;

  if (fd == -1) {
    reply_with_perror ("open: %s", path);
    return -1;
  }

  n = 0;
  while (n < len_sz) {
    r = write (fd, buf, len_sz - n < BUFSIZ ? len_sz - n : BUFSIZ);
    if (r == -1) {
      reply_with_perror ("write: %s", path);
      close (fd);
      return -1;
    }
    n += r;
    notify_progress ((uint64_t) n, (uint64_t) len_sz);
  }

  if (close (fd) == -1) {
    reply_with_perror ("close: %s", path);
    return -1;
  }

  return 0;
}
Esempio n. 10
0
int
do_fill_pattern (const char *pattern, int len, const char *path)
{
  size_t patlen = strlen (pattern);

  if (patlen < 1) {
    reply_with_error ("pattern string must be non-empty");
    return -1;
  }

  if (len < 0) {
    reply_with_error ("%d: length is < 0", len);
    return -1;
  }
  size_t len_sz = (size_t) len;

  int fd;
  CHROOT_IN;
  fd = open (path, O_WRONLY | O_CREAT | O_NOCTTY, 0666);
  CHROOT_OUT;

  if (fd == -1) {
    reply_with_perror ("open: %s", path);
    return -1;
  }

  /* XXX This implementation won't be very efficient for large files. */
  size_t n = 0;
  while (n < len_sz) {
    size_t wrlen = len_sz - n < patlen ? len_sz - n : patlen;
    if (xwrite (fd, pattern, wrlen) == -1) {
      reply_with_perror ("write: %s", path);
      close (fd);
      return -1;
    }
    n += wrlen;
    notify_progress ((uint64_t) n, (uint64_t) len_sz);
  }

  if (close (fd) == -1) {
    reply_with_perror ("close: %s", path);
    return -1;
  }

  return 0;
}
Esempio n. 11
0
int
do_zero (const char *device)
{
  char buf[sizeof zero_buf];
  int fd;
  size_t i, offset;

  fd = open (device, O_RDWR|O_CLOEXEC);
  if (fd == -1) {
    reply_with_perror ("%s", device);
    return -1;
  }

  for (i = 0; i < 32; ++i) {
    offset = i * sizeof zero_buf;

    /* Check if the block is already zero before overwriting it. */
    if (pread (fd, buf, sizeof buf, offset) != sizeof buf) {
      reply_with_perror ("pread: %s", device);
      close (fd);
      return -1;
    }

    if (!is_zero (buf, sizeof buf)) {
      if (pwrite (fd, zero_buf, sizeof zero_buf, offset) != sizeof zero_buf) {
        reply_with_perror ("pwrite: %s", device);
        close (fd);
        return -1;
      }
    }

    notify_progress ((uint64_t) i, 32);
  }

  if (close (fd) == -1) {
    reply_with_perror ("close: %s", device);
    return -1;
  }

  return 0;
}
static RET_CODE on_locked(bl_scan_priv_t *p_priv, nc_channel_info_t *p_ch)
{
  tp_rcv_para_t *p_tp_para = &p_priv->cur_tp_info.tp_info;
  scan_sat_info_t *p_sat = p_priv->perset_sat_list + p_priv->cur_sat_idx;
  RET_CODE ret = ERR_FAILURE;
  bs_cfg_t *p_cur_cfg = p_priv->bs_cfg_info + p_priv->cur_bs_times;

  if(C_BAND == p_cur_cfg->freqBand &&
    DOUBLE_LOCAL_OSCILLATOR == p_cur_cfg->localOscillator)
  {
    p_ch->polarization =
      p_ch->channel_info.frequency > (1550 * KHZ) ? 1 : 0;
    BS_DBG("$$$$ %s,line %d,pol=%d\n",__FUNCTION__,__LINE__,p_ch->polarization);
  }
  else
  {
    p_ch->polarization = p_cur_cfg->polarization;
    BS_DBG("$$$$ %s,line %d,pol=%d\n",__FUNCTION__,__LINE__,p_ch->polarization);
  }

  memset(&p_priv->cur_tp_info, 0, sizeof(scan_tp_info_i_t));
  p_tp_para->freq = bs_calc_down_frq(p_ch, p_sat);
  p_tp_para->sym = p_ch->channel_info.param.dvbs.symbol_rate;
  p_tp_para->polarity = p_ch->polarization;
  p_tp_para->is_on22k = p_ch->onoff22k;
  p_tp_para->nim_type = p_ch->channel_info.param.dvbs.nim_type;
  p_priv->cur_tp_info.sat_id = p_sat->sat_id;
  p_priv->cur_tp_info.can_locked = 1;
  p_priv->cur_tp_info.tp_origin = TP_FROM_SCAN;
  memcpy(&p_priv->cur_tp_info.perf, &p_ch->channel_info.param.dvbs.perf,
    sizeof(nim_channel_perf_t));
  BS_DBG("agc[%ld], snr[%ld],ber[%d]\n", p_priv->cur_tp_info.perf.agc,
    p_priv->cur_tp_info.perf.snr, (u8)p_priv->cur_tp_info.perf.ber);
  //add perf log
  log_perf(LOG_AP_SCAN, PERF_SAVE_DB_START,LOCK_TP_DB,0);
  ret = p_priv->hook.process_tp(&p_priv->cur_tp_info);
  log_perf(LOG_AP_SCAN, PERF_SAVE_DB_DONE,LOCK_TP_DB,0);
  //Notify progress
  notify_progress(progress_calc(p_priv));
  return ret;
}
static RET_CODE on_locked(bl_scan_priv_t *p_priv, nc_channel_info_t *p_ch)
{    
  tp_rcv_para_t *p_tp_para = &p_priv->cur_tp_info.tp_info;
  scan_sat_info_t *p_sat = p_priv->perset_sat_list + p_priv->cur_sat_idx;
  RET_CODE ret = ERR_FAILURE;
        
  memset(&p_priv->cur_tp_info, 0, sizeof(scan_tp_info_i_t));
  p_tp_para->freq = bs_calc_down_frq(p_ch, p_sat);
  p_tp_para->sym = p_ch->channel_info.param.dvbs.symbol_rate;
  p_tp_para->polarity = p_ch->polarization;
  p_tp_para->is_on22k = p_ch->onoff22k;
  p_tp_para->nim_type = p_ch->channel_info.param.dvbs.nim_type;
  p_tp_para->fec_inner = p_ch->channel_info.param.dvbs.fec_inner;
  p_priv->cur_tp_info.sat_id = p_sat->sat_id;
  p_priv->cur_tp_info.can_locked = 1;
  p_priv->cur_tp_info.tp_origin = TP_FROM_SCAN;

  memcpy(&p_priv->cur_tp_info.perf, &p_ch->channel_info.param.dvbs.perf, sizeof(nim_channel_perf_t));
  ret = p_priv->hook.process_tp(&p_priv->cur_tp_info);

  if((ret == SUCCESS) && (p_priv->bs_times == 2))
  {
    //save a locked tp with a scan window
    p_priv->tp_list_locked.tp_list[p_priv->tp_list_locked.tp_depth].freq = 
                                                                  p_ch->channel_info.frequency;
    p_priv->tp_list_locked.tp_list[p_priv->tp_list_locked.tp_depth].sym = \
                                              p_ch->channel_info.param.dvbs.symbol_rate;
    calc_tp_offset_info(
           p_priv->tp_list_locked.tp_list[p_priv->tp_list_locked.tp_depth].sym, 
           &p_priv->tp_list_locked.tp_list[p_priv->tp_list_locked.tp_depth].freq_offset,
           &p_priv->tp_list_locked.tp_list[p_priv->tp_list_locked.tp_depth].sym_reta_offset);

    p_priv->tp_list_locked.tp_depth++;
  }
  //Notify progress 
  notify_progress(progress_calc(p_priv));
  return ret;
}
static RET_CODE on_unlocked(bl_scan_priv_t *p_priv)
{      
  //Notify progress 
  notify_progress(progress_calc(p_priv));
  p_priv->cur_tp_info.can_locked = 0;
  p_priv->hook.process_tp(&p_priv->cur_tp_info);
  
  if(p_priv->bs_times == 2)
  {
    //save a unlocked tp with a scan window
    p_priv->tp_list_unlocked.tp_list[p_priv->tp_list_unlocked.tp_depth].freq = 
                               p_priv->channel_map[p_priv->cur_channel_idx].frequency;
    p_priv->tp_list_unlocked.tp_list[p_priv->tp_list_unlocked.tp_depth].sym = \
                               p_priv->channel_map[p_priv->cur_channel_idx].param.dvbs.symbol_rate;

    p_priv->tp_list_unlocked.tp_list[p_priv->tp_list_unlocked.tp_depth].freq_offset = 0;
    p_priv->tp_list_unlocked.tp_list[p_priv->tp_list_unlocked.tp_depth].sym_reta_offset = 0;

    p_priv->tp_list_unlocked.tp_depth++;
  }
  p_priv->cur_channel_idx++;
  return SUCCESS;
}
Esempio n. 15
0
/* XXX This function really should be cancellable (for the benefit of
 * virt-sparsify).  However currently the library can only handle
 * cancellation for FileIn/FileOut operations.
 */
int
do_zero_free_space (const char *dir)
{
  size_t len = strlen (dir);
  char filename[sysroot_len+len+14]; /* sysroot + dir + "/" + 8.3 + "\0" */
  int fd;
  unsigned skip = 0;
  struct statvfs statbuf;
  fsblkcnt_t bfree_initial;

  /* Choose a randomly named 8.3 file.  Because of the random name,
   * this won't conflict with existing files, and it should be
   * compatible with any filesystem type inc. FAT.
   */
  snprintf (filename, sysroot_len+len+14, "%s%s/XXXXXXXX.XXX", sysroot, dir);
  if (random_name (filename) == -1) {
    reply_with_perror ("random_name");
    return -1;
  }

  if (verbose)
    printf ("random filename: %s\n", filename);

  /* Open file and fill with zeroes until we run out of space. */
  fd = open (filename, O_WRONLY|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0600);
  if (fd == -1) {
    reply_with_perror ("open: %s", filename);
    return -1;
  }

  /* To estimate progress in this operation, we're going to track
   * free blocks in this filesystem down to zero.
   */
  if (fstatvfs (fd, &statbuf) == -1) {
    reply_with_perror ("fstatvfs");
    close (fd);
    return -1;
  }
  bfree_initial = statbuf.f_bfree;

  for (;;) {
    if (write (fd, zero_buf, sizeof zero_buf) == -1) {
      if (errno == ENOSPC)      /* expected error */
        break;
      reply_with_perror ("write: %s", filename);
      close (fd);
      unlink (filename);
      return -1;
    }

    skip++;
    if ((skip & 256) == 0 && fstatvfs (fd, &statbuf) == 0)
      notify_progress (bfree_initial - statbuf.f_bfree, bfree_initial);
  }

  /* Make sure the file is completely written to disk. */
  close (fd); /* expect this to give an error, don't check it */

  sync_disks ();

  notify_progress (bfree_initial, bfree_initial);

  /* Remove the file. */
  if (unlink (filename) == -1) {
    reply_with_perror ("unlink: %s", filename);
    return -1;
  }

  return 0;
}
Esempio n. 16
0
/* Takes optional arguments, consult optargs_bitmask. */
static int
copy (const char *src, const char *src_display,
      const char *dest, const char *dest_display,
      int wrflags, int wrmode,
      int64_t srcoffset, int64_t destoffset, int64_t size, int sparse)
{
  int64_t saved_size = size;
  int src_fd, dest_fd;
  char buf[BUFSIZ];
  size_t n;
  ssize_t r;
  int err;

  if ((optargs_bitmask & GUESTFS_COPY_DEVICE_TO_DEVICE_SRCOFFSET_BITMASK)) {
    if (srcoffset < 0) {
      reply_with_error ("srcoffset is negative");
      return -1;
    }
  }
  else
    srcoffset = 0;

  if ((optargs_bitmask & GUESTFS_COPY_DEVICE_TO_DEVICE_DESTOFFSET_BITMASK)) {
    if (destoffset < 0) {
      reply_with_error ("destoffset is negative");
      return -1;
    }
  }
  else
    destoffset = 0;

  if ((optargs_bitmask & GUESTFS_COPY_DEVICE_TO_DEVICE_SIZE_BITMASK)) {
    if (size < 0) {
      reply_with_error ("size is negative");
      return -1;
    }
  }
  else
    size = -1;

  if (! (optargs_bitmask & GUESTFS_COPY_DEVICE_TO_DEVICE_SPARSE_BITMASK))
    sparse = 0;

  /* Open source and destination. */
  src_fd = open (src, O_RDONLY|O_CLOEXEC);
  if (src_fd == -1) {
    reply_with_perror ("%s", src_display);
    return -1;
  }

  if (srcoffset > 0 && lseek (src_fd, srcoffset, SEEK_SET) == (off_t) -1) {
    reply_with_perror ("lseek: %s", src_display);
    close (src_fd);
    return -1;
  }

  dest_fd = open (dest, wrflags, wrmode);
  if (dest_fd == -1) {
    reply_with_perror ("%s", dest_display);
    close (src_fd);
    return -1;
  }

  if (destoffset > 0 && lseek (dest_fd, destoffset, SEEK_SET) == (off_t) -1) {
    reply_with_perror ("lseek: %s", dest_display);
    close (src_fd);
    close (dest_fd);
    return -1;
  }

  if (size == -1)
    pulse_mode_start ();

  while (size != 0) {
    /* Calculate bytes to copy. */
    if (size == -1 || size > (int64_t) sizeof buf)
      n = sizeof buf;
    else
      n = size;

    r = read (src_fd, buf, n);
    if (r == -1) {
      err = errno;
      if (size == -1)
        pulse_mode_cancel ();
      errno = err;
      reply_with_perror ("read: %s", src_display);
      close (src_fd);
      close (dest_fd);
      return -1;
    }

    if (r == 0) {
      if (size == -1) /* if size == -1, this is normal end of loop */
        break;
      reply_with_error ("%s: input too short", src_display);
      close (src_fd);
      close (dest_fd);
      return -1;
    }

    if (sparse && is_zero (buf, r)) {
      if (lseek (dest_fd, r, SEEK_CUR) == -1) {
        err = errno;
        if (size == -1)
          pulse_mode_cancel ();
        errno = err;
        reply_with_perror ("%s: seek (because of sparse flag)", dest_display);
        close (src_fd);
        close (dest_fd);
        return -1;
      }
      goto sparse_skip;
    }

    if (xwrite (dest_fd, buf, r) == -1) {
      err = errno;
      if (size == -1)
        pulse_mode_cancel ();
      errno = err;
      reply_with_perror ("%s: write", dest_display);
      close (src_fd);
      close (dest_fd);
      return -1;
    }
  sparse_skip:

    if (size != -1) {
      size -= r;
      notify_progress ((uint64_t) (saved_size - size), (uint64_t) saved_size);
    }
  }

  if (size == -1)
    pulse_mode_end ();

  if (close (src_fd) == -1) {
    reply_with_perror ("close: %s", src_display);
    close (dest_fd);
    return -1;
  }

  if (close (dest_fd) == -1) {
    reply_with_perror ("close: %s", dest_display);
    return -1;
  }

  return 0;
}
Esempio n. 17
0
int
do_copy_size (const char *src, const char *dest, int64_t ssize)
{
  char *buf;
  int src_fd, dest_fd;

  if (STRPREFIX (src, "/dev/"))
    src_fd = open (src, O_RDONLY);
  else {
    buf = sysroot_path (src);
    if (!buf) {
      reply_with_perror ("malloc");
      return -1;
    }
    src_fd = open (buf, O_RDONLY);
    free (buf);
  }
  if (src_fd == -1) {
    reply_with_perror ("%s", src);
    return -1;
  }

  if (STRPREFIX (dest, "/dev/"))
    dest_fd = open (dest, O_WRONLY);
  else {
    buf = sysroot_path (dest);
    if (!buf) {
      reply_with_perror ("malloc");
      close (src_fd);
      return -1;
    }
    dest_fd = open (buf, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0666);
    free (buf);
  }
  if (dest_fd == -1) {
    reply_with_perror ("%s", dest);
    close (src_fd);
    return -1;
  }

  uint64_t position = 0, size = (uint64_t) ssize;

  while (position < size) {
    char buf[1024*1024];

    /* Calculate bytes to copy. */
    uint64_t n64 = size - position;
    size_t n;
    if (n64 > sizeof buf)
      n = sizeof buf;
    else
      n = (size_t) n64; /* safe because of if condition */

    ssize_t r = read (src_fd, buf, n);
    if (r == -1) {
      reply_with_perror ("%s: read", src);
      close (src_fd);
      close (dest_fd);
      return -1;
    }
    if (r == 0) {
      reply_with_error ("%s: input file too short", src);
      close (src_fd);
      close (dest_fd);
      return -1;
    }

    if (xwrite (dest_fd, buf, r) == -1) {
      reply_with_perror ("%s: write", dest);
      close (src_fd);
      close (dest_fd);
      return -1;
    }

    position += r;
    notify_progress ((uint64_t) position, (uint64_t) size);
  }

  if (close (src_fd) == -1) {
    reply_with_perror ("%s: close", src);
    close (dest_fd);
    return -1;
  }
  if (close (dest_fd) == -1) {
    reply_with_perror ("%s: close", dest);
    return -1;
  }

  return 0;
}
Esempio n. 18
0
/* at_splines_new_full modifies its 'bitmap' argument
   when it does the thin_image thing.
*/
at_spline_list_array_type * 
at_splines_new_full(at_bitmap_type *       const bitmap,
                    at_fitting_opts_type * const opts,
                    at_msg_func                  msg_func, 
                    void *                 const msg_data,
                    at_progress_func             notify_progress,
                    void *                 const progress_data,
                    at_testcancel_func           test_cancel,
                    void *                 const testcancel_data) {

    at_spline_list_array_type * retval;
    image_header_type image_header;
    pixel_outline_list_type pixelOutlineList;
    at_exception_type exp;
    distance_map_type distanceMap;
    bool haveDistMap;

    exp = at_exception_new(msg_func, msg_data);

    image_header.width  = at_bitmap_get_width(bitmap);
    image_header.height = at_bitmap_get_height(bitmap);

    if (opts->centerline) {
        if (opts->preserve_width) {
            /* Preserve line width prior to thinning. */
            bool const paddedTrue = true;
            distanceMap = new_distance_map(*bitmap, 255, paddedTrue, &exp);
            haveDistMap = true;
        } else
            haveDistMap = false;
        thin_image(bitmap, opts->backgroundSpec, opts->background_color, &exp);
    } else
        haveDistMap = false;

    if (at_exception_got_fatal(&exp))
        retval = NULL;
    else {
        if (opts->centerline) {
            pixel background_color;

            if (opts->backgroundSpec) 
                background_color = opts->background_color;
            else
                PPM_ASSIGN(background_color, 255, 255, 255);
            
            pixelOutlineList =
                find_centerline_pixels(*bitmap, background_color, 
                                       notify_progress, progress_data,
                                       test_cancel, testcancel_data, &exp);
        } else
            pixelOutlineList =
                find_outline_pixels(*bitmap,
                                    opts->backgroundSpec,
                                    opts->background_color, 
                                    notify_progress, progress_data,
                                    test_cancel, testcancel_data, &exp);

        if (at_exception_got_fatal(&exp) ||
            (test_cancel && test_cancel(testcancel_data)))
            retval = NULL;
        else {
            at_spline_list_array_type * splinesP;
        
            MALLOCVAR_NOFAIL(splinesP); 
            fit_outlines_to_splines(pixelOutlineList, opts,
                                    haveDistMap ? &distanceMap : NULL,
                                    image_header.width,
                                    image_header.height,
                                    &exp,
                                    notify_progress, progress_data,
                                    test_cancel, testcancel_data,
                                    splinesP);

            if (at_exception_got_fatal(&exp) ||
                (test_cancel && test_cancel(testcancel_data)))
                retval = NULL;
            else {
                if (notify_progress)
                    notify_progress(1.0, progress_data);

                retval = splinesP;
            }
            free_pixel_outline_list(&pixelOutlineList);
        }
        if (haveDistMap)
            free_distance_map(&distanceMap);
    }
    return retval;
}
Esempio n. 19
0
/* at_splines_new_full modify its argument: BITMAP
   when despeckle, quantize and/or thin_image are invoked. */
at_splines_type *
at_splines_new_full (at_bitmap * bitmap,
                     at_fitting_opts_type * opts,
                     at_msg_func msg_func,
                     gpointer msg_data,
                     at_progress_func notify_progress,
                     gpointer progress_data,
                     at_testcancel_func test_cancel,
                     gpointer testcancel_data)
{
    image_header_type image_header;
    at_splines_type * splines = NULL;
    pixel_outline_list_type pixels;
    QuantizeObj *myQuant = NULL; /* curently not used */
    at_exception_type exp     = at_exception_new(msg_func, msg_data);
    at_distance_map dist_map, *dist = NULL;

#define CANCELP (test_cancel && test_cancel(testcancel_data))
#define FATALP  (at_exception_got_fatal(&exp))
#define FREE_SPLINE() do {if (splines) {at_splines_free(splines); splines = NULL;}} while(0)

#define CANCEL_THEN_CLEANUP_DIST() if (CANCELP) goto cleanup_dist;
#define CANCEL_THEN_CLEANUP_PIXELS() if (CANCELP) {FREE_SPLINE(); goto cleanup_pixels;}

#define FATAL_THEN_RETURN() if (FATALP) return splines;
#define FATAL_THEN_CLEANUP_DIST() if (FATALP) goto cleanup_dist;
#define FATAL_THEN_CLEANUP_PIXELS() if (FATALP) {FREE_SPLINE(); goto cleanup_pixels;}

    if (opts->despeckle_level > 0)
    {
        despeckle (bitmap,
                   opts->despeckle_level,
                   opts->despeckle_tightness,
                   opts->noise_removal,
                   &exp);
        FATAL_THEN_RETURN();
    }

    image_header.width = at_bitmap_get_width(bitmap);
    image_header.height = at_bitmap_get_height(bitmap);

    if (opts->color_count > 0)
    {
        quantize (bitmap, opts->color_count, opts->background_color, &myQuant, &exp);
        if (myQuant)
            quantize_object_free(myQuant); /* curently not used */
        FATAL_THEN_RETURN();
    }

    if (opts->centerline)
    {
        if (opts->preserve_width)
        {
            /* Preserve line width prior to thinning. */
            dist_map = new_distance_map(bitmap, 255, /*padded=*/TRUE, &exp);
            dist = &dist_map;
            FATAL_THEN_RETURN();
        }
        /* Hereafter, dist is allocated. dist must be freed if
        the execution is canceled or exception is raised;
         use FATAL_THEN_CLEANUP_DIST. */
        thin_image (bitmap, opts->background_color, &exp);
        FATAL_THEN_CLEANUP_DIST()
    }

    /* Hereafter, pixels is allocated. pixels must be freed if
       the execution is canceled; use CANCEL_THEN_CLEANUP_PIXELS. */
    if (opts->centerline)
    {
        at_color background_color = { 0xff, 0xff, 0xff };
        if (opts->background_color)
            background_color = *opts->background_color;

        pixels = find_centerline_pixels(bitmap, background_color,
                                        notify_progress, progress_data,
                                        test_cancel, testcancel_data, &exp);
    }
    else
        pixels = find_outline_pixels(bitmap, opts->background_color,
                                     notify_progress, progress_data,
                                     test_cancel, testcancel_data, &exp);
    FATAL_THEN_CLEANUP_DIST();
    CANCEL_THEN_CLEANUP_DIST();

    XMALLOC(splines, sizeof(at_splines_type));
    *splines = fitted_splines (pixels, opts, dist,
                               image_header.width,
                               image_header.height,
                               &exp,
                               notify_progress, progress_data,
                               test_cancel, testcancel_data);
    FATAL_THEN_CLEANUP_PIXELS();
    CANCEL_THEN_CLEANUP_PIXELS();

    if (notify_progress)
        notify_progress(1.0, progress_data);

cleanup_pixels:
    free_pixel_outline_list (&pixels);
cleanup_dist:
    if (dist)
        free_distance_map (dist);
    return splines;
#undef CANCELP
#undef FATALP
#undef FREE_SPLINE
#undef CANCEL_THEN_CLEANUP_DIST
#undef CANCEL_THEN_CLEANUP_PIXELS

#undef FATAL_THEN_RETURN
#undef FATAL_THEN_CLEANUP_DIST
#undef FATAL_THEN_CLEANUP_PIXELS

}
Esempio n. 20
0
int
do_copy_size (const char *src, const char *dest, int64_t ssize)
{
  int src_fd, dest_fd;

  if (is_device_parameter (src))
    src_fd = open (src, O_RDONLY | O_CLOEXEC);
  else {
    CLEANUP_FREE char *buf = sysroot_path (src);
    if (!buf) {
      reply_with_perror ("malloc");
      return -1;
    }
    src_fd = open (buf, O_RDONLY | O_CLOEXEC);
  }
  if (src_fd == -1) {
    reply_with_perror ("%s", src);
    return -1;
  }

  if (is_device_parameter (dest))
    dest_fd = open (dest, O_WRONLY | O_CLOEXEC);
  else {
    CLEANUP_FREE char *buf = sysroot_path (dest);
    if (!buf) {
      reply_with_perror ("malloc");
      close (src_fd);
      return -1;
    }
    dest_fd = open (buf, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY|O_CLOEXEC, 0666);
  }
  if (dest_fd == -1) {
    reply_with_perror ("%s", dest);
    close (src_fd);
    return -1;
  }

  uint64_t position = 0, size = (uint64_t) ssize;
  CLEANUP_FREE char *buf = NULL;
  buf = malloc (BUFSIZ);
  if (buf == NULL) {
    reply_with_perror ("malloc");
    close (src_fd);
    close (dest_fd);
    return -1;
  }

  while (position < size) {
    /* Calculate bytes to copy. */
    uint64_t n64 = size - position;
    size_t n;
    if (n64 > BUFSIZ)
      n = BUFSIZ;
    else
      n = (size_t) n64; /* safe because of if condition */

    ssize_t r = read (src_fd, buf, n);
    if (r == -1) {
      reply_with_perror ("%s: read", src);
      close (src_fd);
      close (dest_fd);
      return -1;
    }
    if (r == 0) {
      reply_with_error ("%s: input file too short", src);
      close (src_fd);
      close (dest_fd);
      return -1;
    }

    if (xwrite (dest_fd, buf, r) == -1) {
      reply_with_perror ("%s: write", dest);
      close (src_fd);
      close (dest_fd);
      return -1;
    }

    position += r;
    notify_progress ((uint64_t) position, (uint64_t) size);
  }

  if (close (src_fd) == -1) {
    reply_with_perror ("%s: close", src);
    close (dest_fd);
    return -1;
  }
  if (close (dest_fd) == -1) {
    reply_with_perror ("%s: close", dest);
    return -1;
  }

  return 0;
}
static find_tp_ret_t blind_scan_get_tp_info(void *p_data,
  nc_channel_info_t *p_channel)
{
  bl_scan_priv_t *p_priv = (bl_scan_priv_t *)p_data;
  find_tp_ret_t ret = FIND_SUC;
  if(p_priv->b_disp_moving)
  {
    u32 interval = (mtos_ticks_get() * 10) - p_priv->disp_move_start_time;
    if(interval > p_priv->disp_move_time)
    {
      //Moving Done
      BS_DBG("%s,line %d,[%d],[%d]\n", __FUNCTION__, __LINE__, interval, p_priv->disp_move_time);
      p_priv->b_disp_moving = FALSE;
    }
    else
    {
      //Skeep Moving
      return FIND_BUSY;
    }
  }

  ret = find_tp(p_priv, p_channel);
  if(FIND_SUC == ret) //found a new tp
  {
      return FIND_SUC;
  }
  else if(FIND_DONE == p_priv->cur_sat_finish_flag) //cur sat found finish
  {
      
    //go to next time
    if(p_priv->cur_bs_times < p_priv->total_bs_times - 1)
    {
       p_priv->scan_over_flag = FALSE;
       p_priv->cur_sat_finish_flag = 0;
       p_priv->cur_bs_times++;
       blind_scan_next_time_process(p_priv);
       return FIND_BUSY;
    }
    else
    {
      p_priv->cur_sat_idx++;
      if(p_priv->cur_sat_idx < p_priv->total_preset_sat)//has the next
      {
        //goto the next satellite
        p_priv->scan_over_flag = FALSE;
        switch_to_sat(p_priv, p_priv->cur_sat_idx);
        return FIND_BUSY;
      }
      else
      {
        notify_progress(100);
        return FIND_DONE;
      }
    }
  }
  else if(FIND_BUSY == ret) //need retry, go on
  {
    return FIND_BUSY;
  }


  return FIND_DONE;
}