Ejemplo n.º 1
0
Archivo: os.cpp Proyecto: andrewrk/zig
void os_path_resolve(Buf *ref_path, Buf *target_path, Buf *out_abs_path) {
    if (os_path_is_absolute(target_path)) {
        buf_init_from_buf(out_abs_path, target_path);
        return;
    }

    os_path_join(ref_path, target_path, out_abs_path);
    return;
}
Ejemplo n.º 2
0
std::string getIntensityLocalAssetPath(const AssetId& id, AssetManager* manager)
{
    // The id is assumed to be the shortpath
    REFLECT_PYTHON_ALTNAME(os.path.join, os_path_join);
    REFLECT_PYTHON(get_asset_dir);

    python::object temp = os_path_join( get_asset_dir(), id );

    return python::extract<std::string>(temp);
}
Ejemplo n.º 3
0
int os_copy_no_clobber(const char *source_path, const char *dest_dir,
        const char *prefix, const char *dest_extension,
        ByteBuffer &out_path, Sha256Hasher *hasher)
{
    ByteBuffer dir_plus_prefix;
    os_path_join(dir_plus_prefix, dest_dir, prefix);

    ByteBuffer full_path;
    int out_fd;
    for (int counter = 0;; counter += 1) {
        full_path = dir_plus_prefix;
        if (counter != 0) {
            ByteBuffer counter_buf;
            counter_buf.format("%d", counter);
            full_path.append(counter_buf);
        }
        full_path.append(dest_extension);
        out_fd = open(full_path.raw(), O_CREAT|O_WRONLY|O_EXCL, 0660);
        if (out_fd == -1) {
            if (errno == EEXIST) {
                continue;
            } else if (errno == ENOMEM) {
                return GenesisErrorNoMem;
            } else {
                return GenesisErrorFileAccess;
            }
        }
        break;
    }

    FILE *out_f = fdopen(out_fd, "wb");
    if (!out_f) {
        close(out_fd);
        os_delete(full_path.raw());
        return GenesisErrorNoMem;
    }

    FILE *in_f = fopen(source_path, "rb");
    if (!in_f) {
        fclose(out_f);
        os_delete(full_path.raw());
        return GenesisErrorFileAccess;
    }

    int err;
    if ((err = copy_open_files(in_f, out_f, hasher))) {
        fclose(in_f);
        fclose(out_f);
        os_delete(full_path.raw());
        return err;
    }
    out_path = full_path;

    return 0;
}
Ejemplo n.º 4
0
int os_create_temp_file(const char *dir, OsTempFile *out_tmp_file) {
    os_path_join(out_tmp_file->path, dir, "XXXXXX");
    int fd = mkstemp(out_tmp_file->path.raw());
    if (fd == -1)
        return GenesisErrorFileAccess;
    out_tmp_file->file = fdopen(fd, "w+");
    if (!out_tmp_file->file) {
        close(fd);
        return GenesisErrorNoMem;
    }
    return 0;
}
Ejemplo n.º 5
0
int os_readdir(const char *dir, List<OsDirEntry*> &entries) {
    for (int i = 0; i < entries.length(); i += 1)
        os_dir_entry_unref(entries.at(i));
    entries.clear();

    DIR *dp = opendir(dir);
    if (!dp) {
        switch (errno) {
            case EACCES:
                return GenesisErrorPermissionDenied;
            case EMFILE: // fall through
            case ENFILE:
                return GenesisErrorSystemResources;
            case ENOENT:
                return GenesisErrorFileNotFound;
            case ENOMEM:
                return GenesisErrorNoMem;
            case ENOTDIR:
                return GenesisErrorNotDir;
            default:
                panic("unexpected error from opendir: %s", strerror(errno));
        }
    }
    struct dirent *ep;
    while ((ep = readdir(dp))) {
        if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
            continue;
        ByteBuffer full_path;
        os_path_join(full_path, dir, ep->d_name);
        struct stat st;
        if (stat(full_path.raw(), &st)) {
            int c_err = errno;
            switch (c_err) {
                case EACCES:
                    closedir(dp);
                    return GenesisErrorPermissionDenied;
                case ELOOP:
                case ENAMETOOLONG:
                case EOVERFLOW:
                    closedir(dp);
                    return GenesisErrorUnimplemented;
                case ENOENT:
                case ENOTDIR:
                    // we can simply skip this entry
                    continue;
                case ENOMEM:
                    closedir(dp);
                    return GenesisErrorNoMem;
                default:
                    panic("unexpected error from stat: %s", strerror(errno));
            }
        }
        OsDirEntry *entry = create_zero<OsDirEntry>();
        if (!entry) {
            closedir(dp);
            return GenesisErrorNoMem;
        }
        entry->name = ByteBuffer(ep->d_name);
        entry->is_dir = S_ISDIR(st.st_mode);
        entry->is_file = S_ISREG(st.st_mode);
        entry->is_link = S_ISLNK(st.st_mode);
        entry->is_hidden = ep->d_name[0] == '.';
        entry->size = st.st_size;
        entry->mtime = st.st_mtime;
        entry->ref_count = 1;

        if (entries.append(entry)) {
            closedir(dp);
            os_dir_entry_unref(entry);
            return GenesisErrorNoMem;
        }
    }
    closedir(dp);
    return 0;
}
Ejemplo n.º 6
0
void os_get_app_config_path(ByteBuffer &out) {
    ByteBuffer app_config_dir;
    os_get_app_config_dir(app_config_dir);

    os_path_join(out, app_config_dir, "config");
}
Ejemplo n.º 7
0
void os_get_samples_dir(ByteBuffer &out) {
    ByteBuffer app_dir;
    os_get_app_dir(app_dir);

    os_path_join(out, app_dir, "samples");
}
Ejemplo n.º 8
0
void os_get_projects_dir(ByteBuffer &out) {
    ByteBuffer app_dir;
    os_get_app_dir(app_dir);

    os_path_join(out, app_dir, "projects");
}
Ejemplo n.º 9
0
void os_get_app_dir(ByteBuffer &out) {
    ByteBuffer home_dir;
    os_get_home_dir(home_dir);

    os_path_join(out, home_dir, ".genesis");
}
Ejemplo n.º 10
0
int32_t _dfu_send_image(Dfu_t *pD, int program_mode, class_Firmware_t *firmware_manifest)
{
    int32_t r;
    int  softdevice_size;
    int  bootloader_size;
    int  application_size;
    int firmware_size;
    char *bin_file_path ;
    buf32_t *firmware;
    char *dat_file_path;
    buf32_t *init_packet;
    DfuTransport_t *pT;

    /*
    Does DFU for one image. Reads the firmware image and init file.
    Opens the transport backend, calls setup, send and finalize and closes the backend again.
    @param program_mode: What type of firmware the DFU is
    @type program_mode: nordicsemi.dfu.model.HexType
    @param firmware_manifest: The manifest for the firmware image
    @type firmware_manifest: nordicsemi.dfu.manifest.Firmware
    @return:
    */

    pT = pD->dfu_transport;

    r = 0;
    if(firmware_manifest == 0)//is None:
    {
        printf("firmware_manifest must be provided.\n"); //raise MissingArgumentException("firmware_manifest must be provided.")
        return(-1);
    }

    bool bRet;
    bRet = pT->is_open(pT);
    if( bRet )
    {
        printf("IllegalStateException(Transport is already open.)\n"); //raise IllegalStateException("Transport is already open.")
        return(-1);
    }

    r = pT->open(pT); //TODO this initialisation (e.g. put RTR500BLE into program mode [SET_DEVICE_DFU_MODE_ON]) should be done here
    if(r != 0) {return(r);}

    r = self_wait_while_opening_transport(pD);
    if(r != 0) {return(r);}

    softdevice_size = 0;
    bootloader_size = 0;
    application_size = 0;

    bin_file_path = os_path_join(pD->unpacked_zip_path, firmware_manifest->bin_file); //"TODO";//os.path.join(self.unpacked_zip_path, firmware_manifest.bin_file);
    firmware = _read_file(bin_file_path);
    free( bin_file_path );
    if( firmware == 0)
    {
        return(-1);
    }

    dat_file_path = os_path_join(pD->unpacked_zip_path, firmware_manifest->dat_file); //"TODO"; //os.path.join(self.unpacked_zip_path, firmware_manifest.dat_file);    
    init_packet = _read_file(dat_file_path);
    free( dat_file_path );
    if( init_packet == 0)
    {
        return(-1);
    }

    if(program_mode == HexType_SD_BL)
    {
        //if( firmware_manifest->manifestType != SoftdeviceBootloaderFirmware ) //if( not isinstance(firmware_manifest, SoftdeviceBootloaderFirmware) )
        //{
        //    printf("Wrong type of manifest\n"); //raise NordicSemiException("Wrong type of manifest")
        //    return(-1);
        //}
        softdevice_size = firmware_manifest->sd_size;
        bootloader_size = firmware_manifest->bl_size;
        firmware_size = firmware->length;

        if( softdevice_size + bootloader_size != firmware_size )
        {
            /*TODO raise NordicSemiException(
                "Size of bootloader ({} bytes) and softdevice ({} bytes)"
                " is not equal to firmware provided ({} bytes)".format(
                bootloader_size, softdevice_size, firmware_size));
                */
            return(42);
        }
    }
    else
    {
        if(program_mode == HexType_SOFTDEVICE)
        {
            softdevice_size = firmware->length;
        }
        else
        {
            if(program_mode == HexType_BOOTLOADER)
            {
                bootloader_size = firmware->length;
            }
            else
            {
                if(program_mode == HexType_APPLICATION)
                {
                    application_size = firmware->length;
                }
            }
        }
    }

    //TODO start_time = time();
    /*
    logger.info("Starting DFU upgrade of type %s, SoftDevice size: %s, bootloader size: %s, application size: %s",
    program_mode,
    softdevice_size,
    bootloader_size,
    application_size)

    logger.info("Sending DFU start packet, afterwards we wait for the flash on "
    "target to be initialized before continuing.")
    */


    pT->SET_DEVICE_DFU_MODE_ON(pT);

    pT->send_start_dfu(pT, program_mode, softdevice_size, bootloader_size, application_size);

    //logger.info("Sending DFU init packet");
    pT->send_init_packet(pT, init_packet);

    //logger.info("Sending firmware file");
    pT->send_firmware(pT, firmware);

    pT->send_validate_firmware(pT);

    pT->send_activate_firmware(pT);

    //TODO end_time = time();
    //logger.info("DFU upgrade took {0}s".format(end_time - start_time));
   
    printf("     5\n");
    time_sleep(1.0);
    printf("    4\n");
    time_sleep(1.0);
    printf("   3\n");
    time_sleep(1.0);
    printf("  2\n");
    time_sleep(1.0);
    printf(" 1\n");
    time_sleep(1.0);
    printf("0\n");

    pT->SET_DEVICE_DFU_MODE_OFF(pT);
    pT->close(pT);  //TODO this DEinitialisation (e.g. put RTR500BLE into Normal mode [SET_DEVICE_DFU_MODE_OFF]) should be done here

    return(r);
}