Пример #1
0
/*********************************************************
 ******************    Main Function    ******************
 *********************************************************/
int main(int agrc, char *agrv[])
{
    int rt = 0; /* return value of function main */

/*     struct fileio_t *fp = create_fileio("t.txt", "r+"); */
/*     struct fileio_t *fp = create_fileio(NULL, NULL); */
/*     fp->open(fp, "t.txt", "r+"); */
/*     printf("%s", fp->read(fp)); */
/*     printf("%s", fp->read(fp)); */
/*     fp->vwrite(fp, "Hello World! --- %d\n", 100); */
/*     fp->write(fp, "Hello World! --- 100\n"); */
/*     fp->close(fp); */

/*         struct cfg_t *cfg = create_cfg("t.txt"); */
/*         printf("value: %s\n", cfg->get_value(cfg, "name")); */
/*         cfg->set_value(cfg, "name", ",:=", "antonio"); */
/*         cfg->set_value(cfg, "name", ",:=", NULL); */
/*         cfg->destroy(cfg); */
/*         struct ini_t *ini = create_ini("t.txt"); */
    
/*         printf("value: %s\n", ini->get_value(ini, "info", "name1")); */
/*         ini->set_value(ini, "info2", "name", "antonio"); */
/*         ini->destroy(ini); */

    struct fileio_t *fp = fileio_create();
    if (fp->open(fp, "t.txt", "r+") == NULL) return -1;
    struct filelock_t *lock = filelock_create(fp->get_file_handle(fp));
    
    printf("pid: %d\n", getpid());
    lock->lock_register(lock, F_WRLCK);
    int ret = lock->is_write_lockable(lock, 0, SEEK_SET, 0);
    printf("ret: %d\n", ret);
    if (ret == 1) {
        if (!lock->write_lock_all(lock)) printf("lock succ\n"); 
    } else {
        printf("write lock by pid %d\n", ret);
    }
    ret = lock->is_write_lockable(lock, 0, SEEK_SET, 0);
    printf("ret: %d\n", ret);
    if (lock->is_write_lockable(lock, 1, SEEK_SET, 1)) printf("can write\n");
    else printf("can not write\n");
    printf("sleep start\n");    
    sleep(atoi(agrv[1]));
    printf("sleep end\n");    
    lock->destroy(lock);
    printf("destroy lock succ\n");
    fp->destroy(fp);

    return rt;
}
Пример #2
0
static int goldfish_nand_init(GoldfishDevice *dev)
{
    GoldfishNandDevice *s = (GoldfishNandDevice *)dev;
    /* Initialize system partition image */
    {
        char        tmp[PATH_MAX+32];
        const char* sysImage = s->system_path;
        const char* initImage = s->system_init_path;
        uint64_t    sysBytes = s->system_size;

        if (sysBytes == 0) {
            PANIC("Invalid system partition size: %jd", sysBytes);
        }

        snprintf(tmp,sizeof(tmp),"system");

        if (sysImage && *sysImage) {
            if (filelock_create(sysImage) == NULL) {
                fprintf(stderr,"WARNING: System image already in use, changes will not persist!\n");
                /* If there is no file= parameters, nand_add_dev will create
                 * a temporary file to back the partition image. */
            } else {
                pstrcat(tmp,sizeof(tmp),",file=");
                pstrcat(tmp,sizeof(tmp),sysImage);
            }
        }
        if (initImage && *initImage) {
            if (!path_exists(initImage)) {
                PANIC("Invalid initial system image path: %s", initImage);
            }
            pstrcat(tmp,sizeof(tmp),",initfile=");
            pstrcat(tmp,sizeof(tmp),initImage);
        } /*else {
            PANIC("Missing initial system image path!");
        }*/
        nand_add_dev(tmp);
    }

    /* Initialize data partition image */
    {
        char        tmp[PATH_MAX+32];
        const char* dataImage = s->user_data_path;
        const char* initImage = s->user_data_init_path;
        uint64_t    dataBytes = s->user_data_size;

        if (dataBytes == 0) {
            PANIC("Invalid data partition size: %jd", dataBytes);
        }

        snprintf(tmp,sizeof(tmp),"userdata,size=0x%jx", dataBytes);

        if (dataImage && *dataImage) {
            if (filelock_create(dataImage) == NULL) {
                fprintf(stderr, "WARNING: Data partition already in use. Changes will not persist!\n");
                /* Note: if there is no file= parameters, nand_add_dev() will
                 *       create a temporary file to back the partition image. */
            } else {
                /* Create the file if needed */
                if (!path_exists(dataImage)) {
                    if (path_empty_file(dataImage) < 0) {
                        PANIC("Could not create data image file %s: %s", dataImage, strerror(errno));
                    }
                }
                pstrcat(tmp, sizeof(tmp), ",file=");
                pstrcat(tmp, sizeof(tmp), dataImage);
            }
        }
        if (initImage && *initImage) {
            pstrcat(tmp, sizeof(tmp), ",initfile=");
            pstrcat(tmp, sizeof(tmp), initImage);
        }
        nand_add_dev(tmp);
    }

    /* Initialize cache partition */
    {
        char        tmp[PATH_MAX+32];
        const char* partPath = s->cache_path;
        uint64_t    partSize = s->cache_size;

        snprintf(tmp,sizeof(tmp),"cache,size=0x%jx", partSize);

        if (partPath && *partPath && strcmp(partPath, "<temp>") != 0) {
            if (filelock_create(partPath) == NULL) {
                fprintf(stderr, "WARNING: Cache partition already in use. Changes will not persist!\n");
                /* Note: if there is no file= parameters, nand_add_dev() will
                 *       create a temporary file to back the partition image. */
            } else {
                /* Create the file if needed */
                if (!path_exists(partPath)) {
                    if (path_empty_file(partPath) < 0) {
                        PANIC("Could not create cache image file %s: %s", partPath, strerror(errno));
                    }
                }
                pstrcat(tmp, sizeof(tmp), ",file=");
                pstrcat(tmp, sizeof(tmp), partPath);
            }
        }
        nand_add_dev(tmp);
    }
    return 0;
}