int main(int argc, char **argv) { int reti; uint16_t vid; struct vol *vol; struct dir *retdir; struct path *path; /* initialize */ printf("Initializing\n============\n"); TEST(setuplog("default:note","/dev/tty")); TEST( afp_options_parse_cmdline(&obj, 3, &args[0]) ); TEST_int( afp_config_parse(&obj, NULL), 0); TEST_int( configinit(&obj), 0); TEST( cnid_init() ); TEST( load_volumes(&obj, NULL) ); TEST_int( dircache_init(8192), 0); obj.afp_version = 32; printf("\n"); /* now run tests */ printf("Running tests\n=============\n"); TEST_expr(vid = openvol(&obj, "test"), vid != 0); TEST_expr(vol = getvolbyvid(vid), vol != NULL); /* test directory.c stuff */ TEST_expr(retdir = dirlookup(vol, DIRDID_ROOT_PARENT), retdir != NULL); TEST_expr(retdir = dirlookup(vol, DIRDID_ROOT), retdir != NULL); TEST_expr(path = cname(vol, retdir, cnamewrap("Network Trash Folder")), path != NULL); TEST_expr(retdir = dirlookup(vol, DIRDID_ROOT), retdir != NULL); TEST_int(getfiledirparms(&obj, vid, DIRDID_ROOT_PARENT, "test"), 0); TEST_int(getfiledirparms(&obj, vid, DIRDID_ROOT, ""), 0); TEST_expr(reti = createdir(&obj, vid, DIRDID_ROOT, "dir1"), reti == 0 || reti == AFPERR_EXIST); TEST_int(getfiledirparms(&obj, vid, DIRDID_ROOT, "dir1"), 0); /* FIXME: this doesn't work although it should. "//" get translated to \000 \000 at means ".." ie this should getfiledirparms for DIRDID_ROOT_PARENT -- at least afair! TEST_int(getfiledirparms(&configs->obj, vid, DIRDID_ROOT, "//"), 0); */ TEST_int(createfile(&obj, vid, DIRDID_ROOT, "dir1/file1"), 0); TEST_int(delete(&obj, vid, DIRDID_ROOT, "dir1/file1"), 0); TEST_int(delete(&obj, vid, DIRDID_ROOT, "dir1"), 0); TEST_int(createfile(&obj, vid, DIRDID_ROOT, "file1"), 0); TEST_int(getfiledirparms(&obj, vid, DIRDID_ROOT, "file1"), 0); TEST_int(delete(&obj, vid, DIRDID_ROOT, "file1"), 0); /* test enumerate.c stuff */ TEST_int(enumerate(&obj, vid, DIRDID_ROOT), 0); }
/* * XXX check client attributes when generation number changes */ void slc_fcmh_refresh_age(struct fidc_membh *f) { struct timeval tmp = { FCMH_ATTR_TIMEO, 0 }; struct fcmh_cli_info *fci; fci = fcmh_2_fci(f); f->fcmh_flags &= ~FCMH_CLI_XATTR_INFO; PFL_GETTIMEVAL(&fci->fci_age); timeradd(&fci->fci_age, &tmp, &fci->fci_age); if (fcmh_isdir(f) && !(f->fcmh_flags & FCMHF_INIT_DIRCACHE)) dircache_init(f); }
/* ------------------------------------------- afp over dsi. this never returns. */ void afp_over_dsi(AFPObj *obj) { DSI *dsi = (DSI *) obj->handle; int rc_idx; u_int32_t err, cmd; u_int8_t function; AFPobj = obj; obj->exit = afp_dsi_die; obj->reply = (int (*)()) dsi_cmdreply; obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention; dsi->tickle = 0; afp_over_dsi_sighandlers(obj); if (dircache_init(obj->options.dircachesize) != 0) afp_dsi_die(EXITERR_SYS); /* set TCP snd/rcv buf */ if (obj->options.tcp_rcvbuf) { if (setsockopt(dsi->socket, SOL_SOCKET, SO_RCVBUF, &obj->options.tcp_rcvbuf, sizeof(obj->options.tcp_rcvbuf)) != 0) { LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_RCVBUF): %s", strerror(errno)); } } if (obj->options.tcp_sndbuf) { if (setsockopt(dsi->socket, SOL_SOCKET, SO_SNDBUF, &obj->options.tcp_sndbuf, sizeof(obj->options.tcp_sndbuf)) != 0) { LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_SNDBUF): %s", strerror(errno)); } } /* set TCP_NODELAY */ int flag = 1; setsockopt(dsi->socket, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag)); /* get stuck here until the end */ while (1) { if (sigsetjmp(recon_jmp, 1) != 0) /* returning from SIGALARM handler for a primary reconnect */ continue; /* Blocking read on the network socket */ cmd = dsi_stream_receive(dsi); if (cmd == 0) { /* cmd == 0 is the error condition */ if (dsi->flags & DSI_RECONSOCKET) { /* we just got a reconnect so we immediately try again to receive on the new fd */ dsi->flags &= ~DSI_RECONSOCKET; continue; } /* the client sometimes logs out (afp_logout) but doesn't close the DSI session */ if (dsi->flags & DSI_AFP_LOGGED_OUT) { LOG(log_note, logtype_afpd, "afp_over_dsi: client logged out, terminating DSI session"); afp_dsi_close(obj); exit(0); } #if 0 /* got ECONNRESET in read from client => exit*/ if (dsi->flags & DSI_GOT_ECONNRESET) { LOG(log_note, logtype_afpd, "afp_over_dsi: client connection reset"); afp_dsi_close(obj); exit(0); } #endif if (dsi->flags & DSI_RECONINPROG) { LOG(log_note, logtype_afpd, "afp_over_dsi: failed reconnect"); afp_dsi_close(obj); exit(0); } /* Some error on the client connection, enter disconnected state */ if (dsi_disconnect(dsi) != 0) afp_dsi_die(EXITERR_CLNT); while (dsi->flags & DSI_DISCONNECTED) pause(); /* gets interrupted by SIGALARM or SIGURG tickle */ continue; /* continue receiving until disconnect timer expires * or a primary reconnect succeeds */ } if (!(dsi->flags & DSI_EXTSLEEP) && (dsi->flags & DSI_SLEEPING)) { LOG(log_debug, logtype_afpd, "afp_over_dsi: got data, ending normal sleep"); dsi->flags &= ~DSI_SLEEPING; dsi->tickle = 0; } if (reload_request) { reload_request = 0; load_volumes(AFPobj); } /* The first SIGINT enables debugging, the next restores the config */ if (debug_request) { static int debugging = 0; debug_request = 0; dircache_dump(); uuidcache_dump(); if (debugging) { if (obj->options.logconfig) setuplog(obj->options.logconfig); else setuplog("default log_note"); debugging = 0; } else { char logstr[50]; debugging = 1; sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid()); setuplog(logstr); } } dsi->flags |= DSI_DATA; dsi->tickle = 0; switch(cmd) { case DSIFUNC_CLOSE: LOG(log_debug, logtype_afpd, "DSI: close session request"); afp_dsi_close(obj); LOG(log_note, logtype_afpd, "done"); exit(0); case DSIFUNC_TICKLE: dsi->flags &= ~DSI_DATA; /* thats no data in the sense we use it in alarm_handler */ LOG(log_debug, logtype_afpd, "DSI: client tickle"); /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */ if ((dsi->flags & DSI_DIE)) dsi_tickle(dsi); break; case DSIFUNC_CMD: #ifdef AFS if ( writtenfork ) { if ( flushfork( writtenfork ) < 0 ) { LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) ); } writtenfork = NULL; } #endif /* AFS */ function = (u_char) dsi->commands[0]; /* AFP replay cache */ rc_idx = dsi->clientID % REPLAYCACHE_SIZE; LOG(log_debug, logtype_dsi, "DSI request ID: %u", dsi->clientID); if (replaycache[rc_idx].DSIreqID == dsi->clientID && replaycache[rc_idx].AFPcommand == function) { LOG(log_note, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s", dsi->clientID, AfpNum2name(function)); err = replaycache[rc_idx].result; /* AFP replay cache end */ } else { /* send off an afp command. in a couple cases, we take advantage * of the fact that we're a stream-based protocol. */ if (afp_switch[function]) { dsi->datalen = DSI_DATASIZ; dsi->flags |= DSI_RUNNING; LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function)); err = (*afp_switch[function])(obj, (char *)&dsi->commands, dsi->cmdlen, (char *)&dsi->data, &dsi->datalen); LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s", AfpNum2name(function), AfpErr2name(err)); dir_free_invalid_q(); #ifdef FORCE_UIDGID /* bring everything back to old euid, egid */ if (obj->force_uid) restore_uidgid ( &obj->uidgid ); #endif /* FORCE_UIDGID */ dsi->flags &= ~DSI_RUNNING; /* Add result to the AFP replay cache */ replaycache[rc_idx].DSIreqID = dsi->clientID; replaycache[rc_idx].AFPcommand = function; replaycache[rc_idx].result = err; } else { LOG(log_error, logtype_afpd, "bad function %X", function); dsi->datalen = 0; err = AFPERR_NOOP; } } /* single shot toggle that gets set by dsi_readinit. */ if (dsi->flags & DSI_NOREPLY) { dsi->flags &= ~DSI_NOREPLY; break; } else if (!dsi_cmdreply(dsi, err)) { LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) ); if (dsi_disconnect(dsi) != 0) afp_dsi_die(EXITERR_CLNT); } break; case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */ function = (u_char) dsi->commands[0]; if ( afp_switch[ function ] != NULL ) { dsi->datalen = DSI_DATASIZ; dsi->flags |= DSI_RUNNING; LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function)); err = (*afp_switch[function])(obj, (char *)&dsi->commands, dsi->cmdlen, (char *)&dsi->data, &dsi->datalen); LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s", AfpNum2name(function), AfpErr2name(err)); dsi->flags &= ~DSI_RUNNING; #ifdef FORCE_UIDGID /* bring everything back to old euid, egid */ if (obj->force_uid) restore_uidgid ( &obj->uidgid ); #endif /* FORCE_UIDGID */ } else { LOG(log_error, logtype_afpd, "(write) bad function %x", function); dsi->datalen = 0; err = AFPERR_NOOP; } if (!dsi_wrtreply(dsi, err)) { LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) ); if (dsi_disconnect(dsi) != 0) afp_dsi_die(EXITERR_CLNT); } break; case DSIFUNC_ATTN: /* attention replies */ break; /* error. this usually implies a mismatch of some kind * between server and client. if things are correct, * we need to flush the rest of the packet if necessary. */ default: LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd); dsi_writeinit(dsi, dsi->data, DSI_DATASIZ); dsi_writeflush(dsi); break; } pending_request(dsi); fce_pending_events(obj); } /* error */ afp_dsi_die(EXITERR_CLNT); }
static int init_dircache(bool preinit) { #ifdef HAVE_DIRCACHE int result = 0; bool clear = false; if (preinit) dircache_init(); if (!global_settings.dircache) return 0; # ifdef HAVE_EEPROM_SETTINGS if (firmware_settings.initialized && firmware_settings.disk_clean && preinit) { result = dircache_load(); if (result < 0) { firmware_settings.disk_clean = false; if (global_status.dircache_size <= 0) { /* This will be in default language, settings are not applied yet. Not really any easy way to fix that. */ splash(0, str(LANG_SCANNING_DISK)); clear = true; } dircache_build(global_status.dircache_size); } } else # endif { if (preinit) return -1; if (!dircache_is_enabled() && !dircache_is_initializing()) { if (global_status.dircache_size <= 0) { splash(0, str(LANG_SCANNING_DISK)); clear = true; } result = dircache_build(global_status.dircache_size); } if (result < 0) { /* Initialization of dircache failed. Manual action is * necessary to enable dircache again. */ splashf(0, "Dircache failed, disabled. Result: %d", result); global_settings.dircache = false; } } if (clear) { backlight_on(); show_logo(); global_status.dircache_size = dircache_get_cache_size(); status_save(); } return result; #else (void)preinit; return 0; #endif }