Beispiel #1
0
void main(void)
{
	clear_all_ram();		//clear ram
	system_initial();		//system init
	time_tcc_pro();			//8ms
	io_init(); 				//seems ok
	_asm{eni}  // enable global interrupt by howie
	_asm{wdtc} //watch dog interrupt clear by howie,clear watch dog in while
	b16ad_init();
	ad_init(); 				// adc init by howie.
	//pwm2_init();
	//while(1);
	while(1)
	{
		while(1)
		{
			IOC70|=0B00000010;//setting P71 floating 设置4脚浮空
			b16ad_init();//输出初始化电平状态
			if(adc_value >= ADC_004V)
			{
				Charger();
			}
		}
	}
}
Beispiel #2
0
/* Looks up for an opened adouble structure, opens resource fork of selected file. 
 * FIXME What about noadouble?
*/
static struct adouble *adl_lkup(struct vol *vol, struct path *path, struct adouble *adp)
{
	static struct adouble ad;
	
	struct ofork *of;
	int isdir;
	
	if (adp)
	    return adp;
	    
	isdir  = S_ISDIR(path->st.st_mode);

	if (!isdir && (of = of_findname(vol, path))) {
		adp = of->of_ad;
	} else {
		ad_init(&ad, vol->v_adouble, vol->v_ad_options);
		adp = &ad;
	} 

    if ( ad_metadata( path->u_name, ((isdir) ? ADFLAGS_DIR : 0), adp) < 0 ) {
        adp = NULL; /* FIXME without resource fork adl_lkup will be call again */
    }
    
	return adp;	
}
int main(void) {
	serieport_init();
	ad_init();
	da_init();
	timer_init();
	
	struct audio_settings settings;
	settings.equ_freqA = 200;
	settings.equ_freqB = 400;
	settings.equ_band1_vol = 100;
	settings.equ_band2_vol = 100;
	settings.equ_band3_vol = 100;
	settings.echo_delay = 48000 * 2;
	settings.echo_vol_decay = 50;
	
	serieport_print_settings(&settings);
		
	unsigned int value;
	
	while(1) {
		if (serieport_getsettings(&settings) == 1) {
			serieport_print_settings(&settings);
		}

	}
	return 0;
}
Beispiel #4
0
static int ad_conv_v22ea_rf(const char *path, const struct stat *sp, const struct vol *vol)
{
    EC_INIT;
    struct adouble adv2;
    struct adouble adea;

    LOG(log_debug, logtype_default,"ad_conv_v22ea_rf(\"%s\"): BEGIN", fullpathname(path));

    if (S_ISDIR(sp->st_mode))
        return 0;

    ad_init(&adea, vol);
    ad_init_old(&adv2, AD_VERSION2, adea.ad_options);

    /* Open and lock adouble:v2 file */
    EC_ZERO( ad_open(&adv2, path, ADFLAGS_HF | ADFLAGS_RF | ADFLAGS_RDWR) );

    if (adv2.ad_rlen > 0) {
        EC_NEG1_LOG( ad_tmplock(&adv2, ADEID_RFORK, ADLOCK_WR | ADLOCK_FILELOCK, 0, 0, 0) );

        /* Create a adouble:ea resource fork */
        EC_ZERO_LOG( ad_open(&adea, path, ADFLAGS_HF | ADFLAGS_RF | ADFLAGS_RDWR | ADFLAGS_CREATE, 0666) );

        EC_ZERO_LOG( copy_fork(ADEID_RFORK, &adea, &adv2) );
        adea.ad_rlen = adv2.ad_rlen;
        ad_flush(&adea);
    }

EC_CLEANUP:
    EC_ZERO_LOG( ad_close(&adv2, ADFLAGS_HF | ADFLAGS_RF) );
    EC_ZERO_LOG( ad_close(&adea, ADFLAGS_HF | ADFLAGS_RF) );
    LOG(log_debug, logtype_default,"ad_conv_v22ea_rf(\"%s\"): END: %d", fullpathname(path), ret);
    EC_EXIT;
}
Beispiel #5
0
/**@brief Function for application main entry. Does not return.
 */
int main(void)
{
    uint32_t err_code;
    nrf_clock_lf_cfg_t clock_lf_cfg = NRF_CLOCK_LFCLKSRC;

    utils_setup();

    // Enable SoftDevice.
    err_code = softdevice_ant_evt_handler_set(ant_evt_dispatch);
    APP_ERROR_CHECK(err_code);

    err_code = softdevice_handler_init(&clock_lf_cfg, NULL, 0, NULL);
    APP_ERROR_CHECK(err_code);

    err_code = ant_stack_static_config();
    APP_ERROR_CHECK(err_code);

    // Setup and open Channel_0 as a Bidirectional Master.
    app_channel_setup();

    #ifdef INCLUDE_DEBUG_CHANNEL
    // Setup and open Debug Channel
    ad_init();
    ad_register_custom_command_callback(app_custom_debug_command_handler);
    #endif

    // Enter main loop
    for (;;)
    {
        err_code = sd_app_evt_wait();
        APP_ERROR_CHECK(err_code);
    }
}
Beispiel #6
0
// DO NOT MODIFY THE CODE BELOW
int w3_node_init() {

	int status;
	int ret = XST_SUCCESS;

	microblaze_enable_exceptions();

	//Initialize the AD9512 clock buffers (RF reference and sampling clocks)
	status = clk_init(CLK_BASEADDR, 2);
	if(status != XST_SUCCESS) {
		xil_printf("w3_node_init: Error in clk_init (%d)\n", status);
		ret = XST_FAILURE;
	}

	status = clk_config_dividers (CLK_BASEADDR, 2, CLK_SAMP_OUTSEL_AD_RFA);
	if(status != XST_SUCCESS) {
		xil_printf("wclk_config_dividers (%d)\n", status);
		ret = XST_FAILURE;
	}
	//Initialize the AD9963 ADCs/DACs for on-board RF interfaces
	ad_init(AD_BASEADDR, (RFA_AD_CS | RFB_AD_CS), 2);
	if(status != XST_SUCCESS) {
		xil_printf("w3_node_init: Error in ad_init (%d)\n", status);
		ret = XST_FAILURE;
	}

	//Initialize the radio_controller core and MAX2829 transceivers for on-board RF interfaces
	status = radio_controller_init(RC_BASEADDR, (RC_RFA | RC_RFB), 1, 1);
	if(status != XST_SUCCESS) {
		xil_printf("w3_node_init: Error in radioController_initialize (%d)\n", status);
		ret = XST_FAILURE;
	}

	return ret;
}
Beispiel #7
0
int ser_init()
{
	i2c_init(callback);
	spi_init(callback);
	uart_init(callback);
	ad_init();

	ser_loadParams();
		
	return 0;	
}
Beispiel #8
0
static int
ad_reinit(device_t dev)
{
    struct ata_channel *ch = device_get_softc(device_get_parent(dev));
    struct ata_device *atadev = device_get_softc(dev);

    /* if detach pending, return error */
    if (!(ch->devices & (ATA_ATA_MASTER << atadev->unit)))
	return 1;

    ad_init(dev);
    return 0;
}
Beispiel #9
0
static int
ad_reinit(device_t dev)
{
    struct ata_channel *ch = device_get_softc(device_get_parent(dev));
    struct ata_device *atadev = device_get_softc(dev);

    /* if detach pending, return error */
    if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATA_MASTER)) ||
	((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATA_SLAVE))) {
	return 1;
    }
    ad_init(dev);
    return 0;
}
/* This function is called periodically by status_run(). It updates the device_state_t
 * data, which is accessed by the READ command in object device
 */
void device_state_update() {
	struct timeval now;

	if (!first_time) {
		first_time = 1;
		ad_init(0);
		ad_init(1);
	}

	gettimeofday(&now, NULL);
	device_state.cur_time = now.tv_sec;
	device_state.ps_voltage[0] = 5000;
	device_state.ps_current[0] = 500;

	device_state.ps_current[1] = 500;
	device_state.ps_voltage[1] = ad_read(0);

	device_state.ps_current[2] = 500;
	device_state.ps_voltage[2] = ad_read(1);

	device_state.error_code = 0;
	device_state.memory_free = free_memory();
	device_state.state_code = 0;
}
Beispiel #11
0
int main(void){
		
		spi_init();	
		ad_init();
		SREG |= (1<<7);				//Globala avbrott	


				while (a<255) {
						a++;
						if (a>254) {
								a = 1;
						}
				}
		return 0;

}
Beispiel #12
0
int main(void)
{
  /* キー入力情報を取得するための変数を宣言する */
  unsigned char key_data;

  lcd_init();    /* LCD の初期化            */
  ad_init();     /* A/D 変換器の初期化      */
  da_init();     /* D/A 変換器の初期化      */

  /* ◎ここにキー入力取得のためのポートの初期化を記述する */

	/* 追記	*/
	P6DDR &= ~0x07;  /* P60,1,2   入力 */
	PADDR |= 0x0f;   /* PA0,1,2,3 出力 */
	/* ここまで */

  while (1) {
    play_mode = NOSELECT;
    key_data = menu();                    /* メニューを選ぶ   */
    /* 録音キーが押されたらSAMPLE, 再生キーが押されたらPLAY   */
    /* をplay_modeに格納する処理を記述する                    */
		
		/*追記*/
		if(key_data == '*'){
			play_mode = SAMPLE;
		}
		if(key_data == '5'){
			play_mode = INVERSE;
		}	
		if(key_data == '#'){
			play_mode = PLAY;
		}
		/*ここまで*/
    
    
    /* ◎*キー入力されていれば録音/再生の関数を呼び出す処理を記述する  */
		sample_replay(play_mode);	
    
  }
  return 1;
}
Beispiel #13
0
static int ad_conv_v22ea_hf(const char *path, const struct stat *sp, const struct vol *vol)
{
    EC_INIT;
    struct adouble adv2;
    struct adouble adea;
    const char *adpath;
    int adflags;
    uint32_t ctime, mtime, afpinfo = 0;
    char *emptyad;

    LOG(log_debug, logtype_default,"ad_conv_v22ea_hf(\"%s\"): BEGIN", fullpathname(path));

    ad_init(&adea, vol);
    ad_init_old(&adv2, AD_VERSION2, adea.ad_options);
    adflags = S_ISDIR(sp->st_mode) ? ADFLAGS_DIR : 0;

    /* Open and lock adouble:v2 file */
    EC_ZERO( ad_open(&adv2, path, adflags | ADFLAGS_HF | ADFLAGS_RDWR) );

    EC_NEG1_LOG( ad_tmplock(&adv2, ADEID_RFORK, ADLOCK_WR | ADLOCK_FILELOCK, 0, 0, 0) );
    EC_NEG1_LOG( adv2.ad_ops->ad_header_read(path, &adv2, sp) );

    /* Check if it's a non-empty header */
    if (S_ISREG(sp->st_mode))
        emptyad = &emptyfilad[0];
    else
        emptyad = &emptydirad[0];

    if (ad_getentrylen(&adv2, ADEID_COMMENT) != 0)
        goto copy;
    if (ad_getentryoff(&adv2, ADEID_FINDERI)
        && (ad_getentrylen(&adv2, ADEID_FINDERI) == ADEDLEN_FINDERI)
        && (memcmp(ad_entry(&adv2, ADEID_FINDERI), emptyad, ADEDLEN_FINDERI) != 0))
        goto copy;
    if (ad_getentryoff(&adv2, ADEID_FILEDATESI)) {
        EC_ZERO_LOG( ad_getdate(&adv2, AD_DATE_CREATE | AD_DATE_UNIX, &ctime) );
        EC_ZERO_LOG( ad_getdate(&adv2, AD_DATE_MODIFY | AD_DATE_UNIX, &mtime) );
        if ((ctime != mtime) || (mtime != sp->st_mtime))
            goto copy;
    }
    if (ad_getentryoff(&adv2, ADEID_AFPFILEI)) {
        if (memcmp(ad_entry(&adv2, ADEID_AFPFILEI), &afpinfo, ADEDLEN_AFPFILEI) != 0)
            goto copy;
    }

    LOG(log_debug, logtype_default,"ad_conv_v22ea_hf(\"%s\"): default adouble", fullpathname(path), ret);
    goto EC_CLEANUP;

copy:
    /* Create a adouble:ea meta EA */
    LOG(log_debug, logtype_default,"ad_conv_v22ea_hf(\"%s\"): copying adouble", fullpathname(path), ret);
    EC_ZERO_LOGSTR( ad_open(&adea, path, adflags | ADFLAGS_HF | ADFLAGS_RDWR | ADFLAGS_CREATE),
                    "ad_conv_v22ea_hf(\"%s\"): error creating metadata EA: %s",
                    fullpathname(path), strerror(errno));
    EC_ZERO_LOG( ad_copy_header(&adea, &adv2) );
    ad_flush(&adea);

EC_CLEANUP:
    EC_ZERO_LOG( ad_close(&adv2, ADFLAGS_HF | ADFLAGS_SETSHRMD) );
    EC_ZERO_LOG( ad_close(&adea, ADFLAGS_HF | ADFLAGS_SETSHRMD) );
    LOG(log_debug, logtype_default,"ad_conv_v22ea_hf(\"%s\"): END: %d", fullpathname(path), ret);
    EC_EXIT;
}
int w3_node_init() {

	int status;
	u8 CMswitch;

	XTmrCtr *TmrCtrInstancePtr = &TimerCounter;
	int ret = XST_SUCCESS;

	microblaze_enable_exceptions();

	//Initialize the AD9512 clock buffers (RF reference and sampling clocks)
	CMswitch = clk_config_read_clkmod_status(CLK_BASEADDR);
	status = clk_init(CLK_BASEADDR, 2);

	if(status != XST_SUCCESS) {
		xil_printf("w3_node_init: Error in clk_init (%d)\n", status);
		ret = XST_FAILURE;
	}

	//Configure this node's clock inputs and outputs, based on the state of the CM-MMCX switch
	// If no CM-MMCX is present, CMswitch will read as 0x3 (on-board clock sources, off-board outputs disabled)
	switch(CMswitch){
				  // RF 		|	 Sample			|	Outputs
		case 0x0: // Off-Board	| 	 Off-Board		|	Off
			clk_config_outputs(CLK_BASEADDR, CLK_OUTPUT_OFF, (CLK_SAMP_OUTSEL_CLKMODHDR | CLK_RFREF_OUTSEL_CLKMODHDR));
			clk_config_input_rf_ref(CLK_BASEADDR, CLK_INSEL_CLKMOD);
			xil_printf("\nClock config %d:\n  RF: Off-board\n  Samp: Off-board\n  Off-board Outputs: Disabled\n\n", CMswitch);
		break;
		case 0x1: // Off-Board	| 	 On-Board		|	Off
			clk_config_outputs(CLK_BASEADDR, CLK_OUTPUT_OFF, (CLK_SAMP_OUTSEL_CLKMODHDR | CLK_RFREF_OUTSEL_CLKMODHDR));
			clk_config_input_rf_ref(CLK_BASEADDR, CLK_INSEL_CLKMOD);
			xil_printf("\nClock config %d:\n  RF: Off-board\n  Samp: On-board\n  Off-board Outputs: Disabled\n\n", CMswitch);
		break;
		case 0x2: // On-Board	| 	 On-Board		|	On
			clk_config_outputs(CLK_BASEADDR, CLK_OUTPUT_ON, (CLK_SAMP_OUTSEL_CLKMODHDR | CLK_RFREF_OUTSEL_CLKMODHDR));
			clk_config_dividers(CLK_BASEADDR, 1, CLK_SAMP_OUTSEL_CLKMODHDR | CLK_RFREF_OUTSEL_CLKMODHDR);
			clk_config_input_rf_ref(CLK_BASEADDR, CLK_INSEL_ONBOARD);
			xil_printf("\nClock config %d:\n  RF: On-board\n  Samp: On-board\n  Off-board Outputs: Enabled\n\n", CMswitch);
		break;
		case 0x3: // On-Board	| 	 On-Board		|	Off
			clk_config_outputs(CLK_BASEADDR, CLK_OUTPUT_OFF, (CLK_SAMP_OUTSEL_CLKMODHDR | CLK_RFREF_OUTSEL_CLKMODHDR));
			clk_config_input_rf_ref(CLK_BASEADDR, CLK_INSEL_ONBOARD);
			//xil_printf("\nClock config %d:\n  RF: On-board\n  Samp: On-board\n  Off-board Outputs: Disabled\n\n", CMswitch);
		break;
	}

#ifdef WLAN_4RF_EN
	//Turn on clocks to FMC
	clk_config_outputs(CLK_BASEADDR, CLK_OUTPUT_ON, (CLK_SAMP_OUTSEL_FMC | CLK_RFREF_OUTSEL_FMC));

	//FMC samp clock divider = 2 (40MHz sampling reference, same as on-board AD9963 ref clk)
	clk_config_dividers(CLK_BASEADDR, 2, CLK_SAMP_OUTSEL_FMC);

	//FMC RF ref clock divider = 2 (40MHz RF reference, same as on-board MAX2829 ref clk)
	clk_config_dividers(CLK_BASEADDR, 2, CLK_RFREF_OUTSEL_FMC);
#endif

	//Initialize the AD9963 ADCs/DACs for on-board RF interfaces
	ad_init(AD_BASEADDR, AD_ALL_RF, 3);
	xil_printf("AD Readback: 0x%08x\n", ad_spi_read(AD_BASEADDR, RFA_AD_CS, 0x32));

	if(status != XST_SUCCESS) {
		xil_printf("w3_node_init: Error in ad_init (%d)\n", status);
		ret = XST_FAILURE;
	}

	//Initialize the radio_controller core and MAX2829 transceivers for on-board RF interfaces
	status = radio_controller_init(RC_BASEADDR, RC_ALL_RF, 1, 1);

	if(status != XST_SUCCESS) {
		xil_printf("w3_node_init: Error in radioController_initialize (%d)\n", status);
		//Comment out allow boot even if an RF interfce doesn't lock (hack for debugging - not for reference release)
		ret = XST_FAILURE;
	}

	//Initialize the EEPROM read/write core
	iic_eeprom_init(EEPROM_BASEADDR, 0x64);

#ifdef WLAN_4RF_EN
	iic_eeprom_init(FMC_EEPROM_BASEADDR, 0x64);
#endif

	//Initialize the timer counter
	status = XTmrCtr_Initialize(TmrCtrInstancePtr, TMRCTR_DEVICE_ID);
	if (status != XST_SUCCESS) {
		xil_printf("w3_node_init: Error in XtmrCtr_Initialize (%d)\n", status);
		ret = XST_FAILURE;
	}

	// Set timer 0 to into a "count down" mode
	XTmrCtr_SetOptions(TmrCtrInstancePtr, 0, (XTC_DOWN_COUNT_OPTION));

	//Give the PHY control of the red user LEDs (PHY counts 1-hot on SIGNAL errors)
	//Note: Uncommenting this line will make the RED LEDs controlled by hardware.
	//This will move the LEDs on PHY bad signal events
	//userio_set_ctrlSrc_hw(USERIO_BASEADDR, W3_USERIO_CTRLSRC_LEDS_RED);

	return ret;
}
Beispiel #15
0
static int
ad_attach(device_t dev)
{
    struct ata_channel *ch = device_get_softc(device_get_parent(dev));
    struct ata_device *atadev = device_get_softc(dev);
    struct ad_softc *adp;
    device_t parent;

    /* check that we have a virgin disk to attach */
    if (device_get_ivars(dev))
	return EEXIST;

    if (!(adp = malloc(sizeof(struct ad_softc), M_AD, M_NOWAIT | M_ZERO))) {
	device_printf(dev, "out of memory\n");
	return ENOMEM;
    }
    device_set_ivars(dev, adp);

    /* get device geometry into internal structs */
    if (ad_get_geometry(dev))
	return ENXIO;

    /* set the max size if configured */
    if (ata_setmax)
	ad_set_geometry(dev);

    /* init device parameters */
    ad_init(dev);

    /* announce we are here */
    ad_describe(dev);

    /* create the disk device */
    adp->disk = disk_alloc();
    adp->disk->d_strategy = ad_strategy;
    adp->disk->d_ioctl = ad_ioctl;
    adp->disk->d_dump = ad_dump;
    adp->disk->d_name = "ad";
    adp->disk->d_drv1 = dev;
    adp->disk->d_maxsize = ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS;
    if (atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48)
	adp->disk->d_maxsize = min(adp->disk->d_maxsize, 65536 * DEV_BSIZE);
    else					/* 28bit ATA command limit */
	adp->disk->d_maxsize = min(adp->disk->d_maxsize, 256 * DEV_BSIZE);
    adp->disk->d_sectorsize = DEV_BSIZE;
    adp->disk->d_mediasize = DEV_BSIZE * (off_t)adp->total_secs;
    adp->disk->d_fwsectors = adp->sectors;
    adp->disk->d_fwheads = adp->heads;
    adp->disk->d_unit = device_get_unit(dev);
    if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
	adp->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
    if ((atadev->param.support.command2 & ATA_SUPPORT_CFA) ||
	atadev->param.config == ATA_PROTO_CFA)
	adp->disk->d_flags |= DISKFLAG_CANDELETE;
    strlcpy(adp->disk->d_ident, atadev->param.serial,
	sizeof(adp->disk->d_ident));
    strlcpy(adp->disk->d_descr, atadev->param.model,
	sizeof(adp->disk->d_descr));
    parent = device_get_parent(ch->dev);
    if (parent != NULL && device_get_parent(parent) != NULL &&
	    (device_get_devclass(parent) ==
	     devclass_find("atapci") ||
	     device_get_devclass(device_get_parent(parent)) ==
	     devclass_find("pci"))) {
	adp->disk->d_hba_vendor = pci_get_vendor(parent);
	adp->disk->d_hba_device = pci_get_device(parent);
	adp->disk->d_hba_subvendor = pci_get_subvendor(parent);
	adp->disk->d_hba_subdevice = pci_get_subdevice(parent);
    }
    ata_disk_firmware_geom_adjust(adp->disk);
    disk_create(adp->disk, DISK_VERSION);
    device_add_child(dev, "subdisk", device_get_unit(dev));
    bus_generic_attach(dev);

    callout_init(&atadev->spindown_timer, 1);
    return 0;
}
Beispiel #16
0
static int do_move(const char *from, const char *to)
{
    struct stat sb;
    int ask, ch, first;

    /*
     * Check access.  If interactive and file exists, ask user if it
     * should be replaced.  Otherwise if file exists but isn't writable
     * make sure the user wants to clobber it.
     */
    if (!fflg && !access(to, F_OK)) {

        /* prompt only if source exist */
        if (lstat(from, &sb) == -1) {
            SLOG("%s: %s", from, strerror(errno));
            return (1);
        }

        ask = 0;
        if (nflg) {
            if (vflg)
                printf("%s not overwritten\n", to);
            return (0);
        } else if (iflg) {
            (void)fprintf(stderr, "overwrite %s? (y/n [n]) ", to);
            ask = 1;
        } else if (access(to, W_OK) && !stat(to, &sb)) {
            (void)fprintf(stderr, "override for %s? (y/n [n]) ", to);
            ask = 1;
        }
        if (ask) {
            first = ch = getchar();
            while (ch != '\n' && ch != EOF)
                ch = getchar();
            if (first != 'y' && first != 'Y') {
                (void)fprintf(stderr, "not overwritten\n");
                return (0);
            }
        }
    }

    int mustcopy = 0;
    /* 
     * Besides the usual EXDEV we copy instead of moving if
     * 1) source AFP volume != dest AFP volume
     * 2) either source or dest isn't even an AFP volume
     */
    if (!svolume.vol->v_path
        || !dvolume.vol->v_path
        || strcmp(svolume.vol->v_path, dvolume.vol->v_path) != 0)
        mustcopy = 1;
    
    cnid_t cnid = 0;
    if (!mustcopy) {
        if ((cnid = cnid_for_path(&svolume, from, &did)) == CNID_INVALID) {
            SLOG("Couldn't resolve CNID for %s", from);
            return -1;
        }

        if (stat(from, &sb) != 0) {
            SLOG("Cant stat %s: %s", to, strerror(errno));
            return -1;
        }

        if (rename(from, to) != 0) {
            if (errno == EXDEV) {
                mustcopy = 1;
                char path[MAXPATHLEN];

                /*
                 * If the source is a symbolic link and is on another
                 * filesystem, it can be recreated at the destination.
                 */
                if (lstat(from, &sb) == -1) {
                    SLOG("%s: %s", from, strerror(errno));
                    return (-1);
                }
                if (!S_ISLNK(sb.st_mode)) {
                    /* Can't mv(1) a mount point. */
                    if (realpath(from, path) == NULL) {
                        SLOG("cannot resolve %s: %s: %s", from, path, strerror(errno));
                        return (1);
                    }
                }
            } else { /* != EXDEV */
                SLOG("rename %s to %s: %s", from, to, strerror(errno));
                return (1);
            }
        } /* rename != 0*/
        
        switch (sb.st_mode & S_IFMT) {
        case S_IFREG:
            if (dvolume.vol->vfs->vfs_renamefile(dvolume.vol, -1, from, to) != 0) {
                SLOG("Error moving adouble file for %s", from);
                return -1;
            }
            break;
        case S_IFDIR:
            break;
        default:
            SLOG("Not a file or dir: %s", from);
            return -1;
        }
    
        /* get CNID of new parent dir */
        cnid_t newpdid, newdid;
        if ((newdid = cnid_for_paths_parent(&dvolume, to, &newpdid)) == CNID_INVALID) {
            SLOG("Couldn't resolve CNID for parent of %s", to);
            return -1;
        }

        if (stat(to, &sb) != 0) {
            SLOG("Cant stat %s: %s", to, strerror(errno));
            return 1;
        }

        char *p = strdup(to);
        char *name = basename(p);
        if (cnid_update(dvolume.vol->v_cdb, cnid, &sb, newdid, name, strlen(name)) != 0) {
            SLOG("Cant update CNID for: %s", to);
            return 1;
        }
        free(p);

        struct adouble ad;
        ad_init(&ad, dvolume.vol);
        if (ad_open(&ad, to, S_ISDIR(sb.st_mode) ? (ADFLAGS_DIR | ADFLAGS_HF | ADFLAGS_RDWR) : ADFLAGS_HF | ADFLAGS_RDWR) != 0) {
            SLOG("Error opening adouble for: %s", to);
            return 1;
        }
        ad_setid(&ad, sb.st_dev, sb.st_ino, cnid, newdid, dvolume.db_stamp);
        ad_flush(&ad);
        ad_close(&ad, ADFLAGS_HF);

        if (vflg)
            printf("%s -> %s\n", from, to);
        return (0);
    }
    
    if (mustcopy)
        return copy(from, to);

    /* If we get here it's an error */
    return -1;
}
Beispiel #17
0
int ad_set(int argc, char **argv, AFPObj *obj)
{
    int c, firstarg;
    afpvol_t vol;
    struct stat st;
    int adflags = 0;
    struct adouble ad;

    while ((c = getopt(argc, argv, ":l:t:c:f:a:")) != -1) {
        switch(c) {
        case 'l':
            new_label = strdup(optarg);
            break;
        case 't':
            new_type = strdup(optarg);
            break;
        case 'c':
            new_creator = strdup(optarg);
            break;
        case 'f':
            new_flags = strdup(optarg);
            break;
        case 'a':
            new_attributes = strdup(optarg);
            break;
        case ':':
        case '?':
            usage_set();
            return -1;
            break;
        }

    }

    if (argc <= optind)
        exit(1);

    cnid_init();

    openvol(obj, argv[optind], &vol);
    if (vol.vol->v_path == NULL)
        exit(1);

    if (stat(argv[optind], &st) != 0) {
        perror("stat");
        exit(1);
    }

    if (S_ISDIR(st.st_mode))
        adflags = ADFLAGS_DIR;

    ad_init(&ad, vol.vol);

    if (ad_open(&ad, argv[optind], adflags | ADFLAGS_HF | ADFLAGS_CREATE | ADFLAGS_RDWR, 0666) < 0)
        goto exit;

    if (new_label)
        change_label(argv[optind], &vol, &st, &ad, new_label);
    if (new_type)
        change_type(argv[optind], &vol, &st, &ad, new_type);
    if (new_creator)
        change_creator(argv[optind], &vol, &st, &ad, new_creator);
    if (new_flags)
        change_flags(argv[optind], &vol, &st, &ad, new_flags);
    if (new_attributes)
        change_attributes(argv[optind], &vol, &st, &ad, new_attributes);

    ad_flush(&ad);
    ad_close(&ad, ADFLAGS_HF);

exit:
    closevol(&vol);

    return 0;
}
Beispiel #18
0
static int copy(const char *path,
                const struct stat *statp,
                int tflag,
                struct FTW *ftw)
{
    static int base = 0;

    struct stat to_stat;
    int dne;
    size_t nlen;
    const char *p;
    char *target_mid;

    if (alarmed)
        return -1;

    /* This currently doesn't work with "." */
    if (strcmp(path, ".") == 0) {
        ERROR("\".\" not supported");
    }
    const char *dir = strrchr(path, '/');
    if (dir == NULL)
        dir = path;
    else
        dir++;
    if (check_netatalk_dirs(dir) != NULL)
        return FTW_SKIP_SUBTREE;

    /*
     * If we are in case (2) above, we need to append the
     * source name to the target name.
     */
    if (type != FILE_TO_FILE) {
        /*
         * Need to remember the roots of traversals to create
         * correct pathnames.  If there's a directory being
         * copied to a non-existent directory, e.g.
         *     cp -R a/dir noexist
         * the resulting path name should be noexist/foo, not
         * noexist/dir/foo (where foo is a file in dir), which
         * is the case where the target exists.
         *
         * Also, check for "..".  This is for correct path
         * concatenation for paths ending in "..", e.g.
         *     cp -R .. /tmp
         * Paths ending in ".." are changed to ".".  This is
         * tricky, but seems the easiest way to fix the problem.
         *
         * XXX
         * Since the first level MUST be FTS_ROOTLEVEL, base
         * is always initialized.
         */
        if (ftw->level == 0) {
            if (type != DIR_TO_DNE) {
                base = ftw->base;

                if (strcmp(&path[base], "..") == 0)
                    base += 1;
            } else
                base = strlen(path);
        }

        p = &path[base];
        nlen = strlen(path) - base;
        target_mid = to.target_end;
        if (*p != '/' && target_mid[-1] != '/')
            *target_mid++ = '/';
        *target_mid = 0;
        if (target_mid - to.p_path + nlen >= PATH_MAX) {
            SLOG("%s%s: name too long (not copied)", to.p_path, p);
            badcp = rval = 1;
            return 0;
        }
        (void)strncat(target_mid, p, nlen);
        to.p_end = target_mid + nlen;
        *to.p_end = 0;
        STRIP_TRAILING_SLASH(to);
    }

    /* Not an error but need to remember it happened */
    if (stat(to.p_path, &to_stat) == -1)
        dne = 1;
    else {
        if (to_stat.st_dev == statp->st_dev &&
            to_stat.st_ino == statp->st_ino) {
            SLOG("%s and %s are identical (not copied).", to.p_path, path);
            badcp = rval = 1;
            if (S_ISDIR(statp->st_mode))
                /* without using glibc extension FTW_ACTIONRETVAL cant handle this */
                return FTW_SKIP_SUBTREE;
            return 0;
        }
        if (!S_ISDIR(statp->st_mode) &&
            S_ISDIR(to_stat.st_mode)) {
            SLOG("cannot overwrite directory %s with "
                 "non-directory %s",
                 to.p_path, path);
            badcp = rval = 1;
            return 0;
        }
        dne = 0;
    }

    /* Convert basename to appropiate volume encoding */
    if (dvolume.volinfo.v_path) {
        if ((convert_dots_encoding(&svolume, &dvolume, to.p_path, MAXPATHLEN)) == -1) {
            SLOG("Error converting name for %s", to.p_path);
            badcp = rval = 1;
            return -1;
        }
    }

    switch (statp->st_mode & S_IFMT) {
    case S_IFLNK:
        if (ftw_copy_link(ftw, path, statp, !dne))
            badcp = rval = 1;
        break;
    case S_IFDIR:
        if (!Rflag) {
            SLOG("%s is a directory", path);
            badcp = rval = 1;
            return -1;
        }
        /*
         * If the directory doesn't exist, create the new
         * one with the from file mode plus owner RWX bits,
         * modified by the umask.  Trade-off between being
         * able to write the directory (if from directory is
         * 555) and not causing a permissions race.  If the
         * umask blocks owner writes, we fail..
         */
        if (dne) {
            if (mkdir(to.p_path, statp->st_mode | S_IRWXU) < 0)
                ERROR("mkdir: %s: %s", to.p_path, strerror(errno));
        } else if (!S_ISDIR(to_stat.st_mode)) {
            errno = ENOTDIR;
            ERROR("%s", to.p_path);
        }

        /* Create ad dir and copy ".Parent" */
        if (dvolume.volinfo.v_path && dvolume.volinfo.v_adouble == AD_VERSION2) {

            /* Create ".AppleDouble" dir */
            mode_t omask = umask(0);
            bstring addir = bfromcstr(to.p_path);
            bcatcstr(addir, "/.AppleDouble");
            mkdir(cfrombstr(addir), 02777);
            bdestroy(addir);

            if (svolume.volinfo.v_path && svolume.volinfo.v_adouble == AD_VERSION2) {
                /* copy ".Parent" file */
                if (dvolume.volume.vfs->vfs_copyfile(&dvolume.volume, -1, path, to.p_path)) {
                    SLOG("Error copying adouble for %s -> %s", path, to.p_path);
                    badcp = rval = 1;
                    break;
                }
            }

            /* Get CNID of Parent and add new childir to CNID database */
            ppdid = pdid;
            if ((did = cnid_for_path(&dvolume, to.p_path, &pdid)) == CNID_INVALID) {
                SLOG("Error resolving CNID for %s", to.p_path);
                badcp = rval = 1;
                return -1;
            }

            struct adouble ad;
            struct stat st;
            if (lstat(to.p_path, &st) != 0) {
                badcp = rval = 1;
                break;
            }
            ad_init(&ad, dvolume.volinfo.v_adouble, dvolume.volinfo.v_ad_options);
            if (ad_open_metadata(to.p_path, ADFLAGS_DIR, O_RDWR | O_CREAT, &ad) != 0) {
                ERROR("Error opening adouble for: %s", to.p_path);
            }
            ad_setid( &ad, st.st_dev, st.st_ino, did, pdid, dvolume.db_stamp);
            ad_setname(&ad, utompath(&dvolume.volinfo, basename(to.p_path)));
            ad_setdate(&ad, AD_DATE_CREATE | AD_DATE_UNIX, st.st_mtime);
            ad_setdate(&ad, AD_DATE_MODIFY | AD_DATE_UNIX, st.st_mtime);
            ad_setdate(&ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
            ad_setdate(&ad, AD_DATE_BACKUP, AD_DATE_START);
            ad_flush(&ad);
            ad_close_metadata(&ad);

            umask(omask);
        }

        if (pflag) {
            if (setfile(statp, -1))
                rval = 1;
#if 0
            if (preserve_dir_acls(statp, curr->fts_accpath, to.p_path) != 0)
                rval = 1;
#endif
        }
        break;

    case S_IFBLK:
    case S_IFCHR:
        SLOG("%s is a device file (not copied).", path);
        break;
    case S_IFSOCK:
        SLOG("%s is a socket (not copied).", path);
        break;
    case S_IFIFO:
        SLOG("%s is a FIFO (not copied).", path);
        break;
    default:
        if (ftw_copy_file(ftw, path, statp, dne))
            badcp = rval = 1;

        if (dvolume.volinfo.v_path && dvolume.volinfo.v_adouble == AD_VERSION2) {

            mode_t omask = umask(0);
            if (svolume.volinfo.v_path && svolume.volinfo.v_adouble == AD_VERSION2) {
                /* copy ad-file */
                if (dvolume.volume.vfs->vfs_copyfile(&dvolume.volume, -1, path, to.p_path)) {
                    SLOG("Error copying adouble for %s -> %s", path, to.p_path);
                    badcp = rval = 1;
                    break;
                }
            }

            /* Get CNID of Parent and add new childir to CNID database */
            pdid = did;
            cnid_t cnid;
            if ((cnid = cnid_for_path(&dvolume, to.p_path, &did)) == CNID_INVALID) {
                SLOG("Error resolving CNID for %s", to.p_path);
                badcp = rval = 1;
                return -1;
            }

            struct adouble ad;
            struct stat st;
            if (lstat(to.p_path, &st) != 0) {
                badcp = rval = 1;
                break;
            }
            ad_init(&ad, dvolume.volinfo.v_adouble, dvolume.volinfo.v_ad_options);
            if (ad_open_metadata(to.p_path, 0, O_RDWR | O_CREAT, &ad) != 0) {
                ERROR("Error opening adouble for: %s", to.p_path);
            }
            ad_setid( &ad, st.st_dev, st.st_ino, cnid, did, dvolume.db_stamp);
            ad_setname(&ad, utompath(&dvolume.volinfo, basename(to.p_path)));
            ad_setdate(&ad, AD_DATE_CREATE | AD_DATE_UNIX, st.st_mtime);
            ad_setdate(&ad, AD_DATE_MODIFY | AD_DATE_UNIX, st.st_mtime);
            ad_setdate(&ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
            ad_setdate(&ad, AD_DATE_BACKUP, AD_DATE_START);
            ad_flush(&ad);
            ad_close_metadata(&ad);
            umask(omask);
        }
        break;
    }
    if (vflag && !badcp)
        (void)printf("%s -> %s\n", path, to.p_path);

    return 0;
}
Beispiel #19
0
static int
ad_attach(device_t dev)
{
    struct ata_channel *ch = device_get_softc(device_get_parent(dev));
    struct ata_device *atadev = device_get_softc(dev);
    struct disk_info info;
    struct ad_softc *adp;
    cdev_t cdev;
    u_int32_t lbasize;
    u_int64_t lbasize48;

    /* check that we have a virgin disk to attach */
    if (device_get_ivars(dev))
	return EEXIST;

    adp = kmalloc(sizeof(struct ad_softc), M_AD, M_INTWAIT | M_ZERO);
    device_set_ivars(dev, adp);

    if ((atadev->param.atavalid & ATA_FLAG_54_58) &&
	atadev->param.current_heads && atadev->param.current_sectors) {
	adp->heads = atadev->param.current_heads;
	adp->sectors = atadev->param.current_sectors;
	adp->total_secs = (u_int32_t)atadev->param.current_size_1 |
			  ((u_int32_t)atadev->param.current_size_2 << 16);
    }
    else {
	adp->heads = atadev->param.heads;
	adp->sectors = atadev->param.sectors;
	adp->total_secs = atadev->param.cylinders * adp->heads * adp->sectors;  
    }
    lbasize = (u_int32_t)atadev->param.lba_size_1 |
	      ((u_int32_t)atadev->param.lba_size_2 << 16);

    /* does this device need oldstyle CHS addressing */
    if (!ad_version(atadev->param.version_major) || !lbasize)
	atadev->flags |= ATA_D_USE_CHS;

    /* use the 28bit LBA size if valid or bigger than the CHS mapping */
    if (atadev->param.cylinders == 16383 || adp->total_secs < lbasize)
	adp->total_secs = lbasize;

    /* use the 48bit LBA size if valid */
    lbasize48 = ((u_int64_t)atadev->param.lba_size48_1) |
		((u_int64_t)atadev->param.lba_size48_2 << 16) |
		((u_int64_t)atadev->param.lba_size48_3 << 32) |
		((u_int64_t)atadev->param.lba_size48_4 << 48);
    if ((atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) &&
	lbasize48 > ATA_MAX_28BIT_LBA)
	adp->total_secs = lbasize48;

    /* init device parameters */
    ad_init(dev);

    /* create the disk device */
    /* XXX TGEN Maybe use DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT,
       DEVSTAT_PRIORITY_MAX. */
    devstat_add_entry(&adp->stats, "ad", device_get_unit(dev), DEV_BSIZE,
		      DEVSTAT_NO_ORDERED_TAGS,
		      DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
		      DEVSTAT_PRIORITY_DISK);
    cdev = disk_create(device_get_unit(dev), &adp->disk, &ad_ops);
    cdev->si_drv1 = dev;
    if (ch->dma)
        cdev->si_iosize_max = ch->dma->max_iosize;
    else
        cdev->si_iosize_max = DFLTPHYS;
    adp->cdev = cdev;

    bzero(&info, sizeof(info));
    info.d_media_blksize = DEV_BSIZE;		/* mandatory */
    info.d_media_blocks = adp->total_secs;

    info.d_secpertrack = adp->sectors;		/* optional */
    info.d_nheads = adp->heads;
    info.d_ncylinders = adp->total_secs/(adp->heads*adp->sectors);
    info.d_secpercyl = adp->sectors * adp->heads;
    info.d_serialno = atadev->param.serial;

    device_add_child(dev, "subdisk", device_get_unit(dev));
    bus_generic_attach(dev);

    /* announce we are here */
    ad_describe(dev);

    disk_setdiskinfo(&adp->disk, &info);

    return 0;
}