Beispiel #1
0
int
syscmd_copy_file(char *src_file, char *dst_file)
{
    FILE *src_fp = NULL, *dst_fp = NULL;
    char buf[BUFFER_COUNT];
    int ret = 0, count;
    int w_count = 0;
    int amount = 0;

    if (!sal_strcmp(src_file, dst_file))
    {
        return 0;
    }

    src_fp = sal_fopen(src_file, "r");
    if (NULL == src_fp)
    {
        ret = -1;
        goto error;
    }

    dst_fp = sal_fopen(dst_file, "w+");
    if (NULL == dst_fp)
    {
        ret = -2;
        goto error;
    }

    while ((count = sal_fread(buf, sizeof(char), BUFFER_COUNT, src_fp)) > 0)
    {
        w_count = sal_fwrite(buf, sizeof(char), count, dst_fp);

        if (w_count < count)
        {
            ret = -2;
            goto error;
        }

        /* check the memory on 1M boundary */
        amount += count;
        if(amount & 0x100000)
        {
            ret = -3;
            goto error;
        }
    }
error:
    if (NULL != src_fp)
    {
        sal_fclose(src_fp);
    }
    if (NULL != dst_fp)
    {
        sal_fclose(dst_fp);
    }
    memmgr_free_cached_mem();

    return ret;
}
Beispiel #2
0
STATIC cmd_result_t
_iproc_write_from_bin_file(int unit, int ce, uint32 addr, FILE *fp) {
    uint32 data;
    int len, i;

    len = 0;
    while ((i = sal_fread(&data,4,1,fp)) != 0) {
        if (ce) {
            data = SWAP_ENDIAN(data);
        }
        soc_cm_iproc_write(unit, (addr + (len * 4)), data);
        len += i;
    }

    soc_cm_debug(DK_VERBOSE, "Wrote %d words to iProc 0x%08x\n", len, addr);
    return CMD_OK;
}
Beispiel #3
0
/*******************************************************************************
 * Name:    copy_file_with_progress
 * Purpose: copy file and show progress
 * Input:
 *   src_file: source file
 *   dst_file: dest file
 * Output:
 * Return:
 *   success: 0
 *   failed : -1
 * Note:
 ******************************************************************************/
#define HASHBYTES (50 * 1024)
#define MEMCHECK  (4 * 1024 *1024) /*4M*/
int
copy_file_with_progress(char *src_file, char *dst_file)
{
    FILE *src_fp = NULL, *dst_fp = NULL;
    char buf[BUFFER_COUNT];
    int ret = 0, count;
    int amount = 0;
    int hashbytes = 0;
    int memcheck = 0;
    struct timeval tstart;
    struct timeval tstop;

    if (!sal_strcmp(src_file, dst_file))
    {
        return 0;
    }

    src_fp = sal_fopen(src_file, "r");
    if (NULL == src_fp)
    {
        ret = -1;
        goto error;
    }

    dst_fp = sal_fopen(dst_file, "w+");
    if (NULL == dst_fp)
    {
        ret = -2;
        goto error;
    }

    (void)gettimeofday(&tstart, NULL);
    while ((count = sal_fread(buf, sizeof(char), BUFFER_COUNT, src_fp)) > 0)
    {
        ret = sal_fwrite(buf, sizeof(char), count, dst_fp);
        if (ret <= 0)
        {
            (void)gettimeofday(&tstop, NULL);
            goto error;
        }
        amount += count;

        if(amount >= memcheck)
        {
            if((syslimit_mem_threshold_check() < 0))
            {
                ret = -3;
                goto error;
            }
            memcheck += MEMCHECK;
        }

        while (amount >= hashbytes)
        {
            ctc_cli_out(".");
            (void) fflush(stdout);
            hashbytes += HASHBYTES;
        }
    }
    (void)gettimeofday(&tstop, NULL);

error:
    if (NULL != src_fp)
    {
        sal_fclose(src_fp);
    }
    if (NULL != dst_fp)
    {
        sal_fclose(dst_fp);
    }
    if (amount && ( ret >= 0))
    {
        double  delta;

        /* compute delta in 1/10's second units */
        delta = ((tstop.tv_sec * 10.) + (tstop.tv_usec / 100000)) -
                ((tstart.tv_sec * 10.) + (tstart.tv_usec / 100000));
        delta = delta / 10.;    /* back to seconds */
        ctc_cli_out("\n%lu bytes in %.1f seconds, %.0f kbytes/second\n",
                amount, delta, (amount * 1.) / (1024 * delta));
    }
    return ret;
}