struct sm_stat * sm_unmon_1_svc(mon_id *arg, struct svc_req *req) { static sm_stat res; HostInfo *hp; if (debug) { syslog(LOG_DEBUG, "un-monitor request for host %s", arg->mon_name); syslog(LOG_DEBUG, "recall host: %s prog: %d ver: %d proc: %d", arg->my_id.my_name, arg->my_id.my_prog, arg->my_id.my_vers, arg->my_id.my_proc); } if ((hp = find_host(arg->mon_name, FALSE))) { if (do_unmon(hp, &arg->my_id)) sync_file(); else { syslog(LOG_ERR, "unmon request from %s, no matching monitor", arg->my_id.my_name); } } else syslog(LOG_ERR, "unmon request from %s for unknown host %s", arg->my_id.my_name, arg->mon_name); res.state = status_info->ourState; return (&res); }
static uint32 smbvfs_wrap_write(PFILE_HND file_hnd, char *data, SMB_OFF_T offset, uint16 numtowrite, uint16 writethrough, uint16 *nwritten) { files_struct *fsp = NULL; ssize_t nwrite; if (!get_file_info(file_hnd, &fsp)) return NT_STATUS_INVALID_HANDLE; if (fsp == NULL) return NT_STATUS_NO_SUCH_FILE; DEBUG(12,("file name: [%s]\n", smbstrA(fsp->fsp_name))); nwrite = write_file(fsp, data, offset, numtowrite); DEBUG( 3, ( "SMBwriteX offset=%d nwritten=%d\n", (int)offset, nwrite ) ); if (nwrite < 0) { *nwritten = 0; return NT_STATUS_ACCESS_DENIED; } *nwritten = nwrite; if (writethrough != 0) sync_file(fsp->conn, fsp); return NT_STATUS_NOPROBLEMO; }
void QuickFile::close_file() { if (! valid) return; sync_file(); munmap(fptr, filesize); close(fd); valid = false; }
static void handle_inotify(csiebox_client* client) { int len = 0, i = 0; char buffer[EVENT_BUF_LEN]; memset(buffer, 0, EVENT_BUF_LEN); if ((len = read(client->inotify_fd, buffer, EVENT_BUF_LEN)) <= 0) { return; } i = 0; while (i < len) { struct inotify_event* event = (struct inotify_event*)&buffer[i]; char path[PATH_MAX]; memset(path, 0, PATH_MAX); char* wd_path; if (!get_from_hash(&(client->inotify_hash), (void**)&wd_path, event->wd)) { continue; } sprintf(path, "%s/", wd_path); strncat(path, event->name, event->len); fprintf(stderr, "wd: %d\n", event->wd); if (event->mask & IN_CREATE) { fprintf(stderr, "type: create\n"); fprintf(stderr, "sync file: %s\n", path); sync_file(client, path); if (event->mask & IN_ISDIR) { add_inotify(client, path); } } else if (event->mask & IN_ATTRIB) { fprintf(stderr, "type: attrib\n"); fprintf(stderr, "sync file meta: %s\n", path); sync_file_meta(client, path); } else if (event->mask & IN_DELETE) { fprintf(stderr, "type: delete\n"); fprintf(stderr, "rm file: %s\n", path); rm_file(client, path, event->mask & IN_ISDIR); } else { fprintf(stderr, "type: modify\n"); fprintf(stderr, "sync file: %s\n", path); sync_file(client, path); } i += EVENT_SIZE + event->len; } memset(buffer, 0, EVENT_BUF_LEN); }
void file_sync_all(connection_struct *conn) { files_struct *fsp, *next; for (fsp=conn->sconn->files; fsp; fsp=next) { next=fsp->next; if ((conn == fsp->conn) && (fsp->fh->fd != -1)) { sync_file(conn, fsp, True /* write through */); } } }
//this is where the server handle requests, you should write your code here static void handle_request(csiebox_server* server, int conn_fd) { csiebox_protocol_header header; memset(&header, 0, sizeof(header)); while (recv_message(conn_fd, &header, sizeof(header))) { if (header.req.magic != CSIEBOX_PROTOCOL_MAGIC_REQ) { continue; } switch (header.req.op) { case CSIEBOX_PROTOCOL_OP_LOGIN: fprintf(stderr, ">>>>>>>>>> login\n"); csiebox_protocol_login req; if (complete_message_with_header(conn_fd, &header, &req)) { login(server, conn_fd, &req); } break; case CSIEBOX_PROTOCOL_OP_SYNC_META: fprintf(stderr, ">>>>>>>>>> sync meta\n"); csiebox_protocol_meta meta; if (complete_message_with_header(conn_fd, &header, &meta)) { sync_meta(server, conn_fd, &meta); } break; case CSIEBOX_PROTOCOL_OP_SYNC_FILE: fprintf(stderr, ">>>>>>>>>> sync file\n"); csiebox_protocol_file file; if (complete_message_with_header(conn_fd, &header, &file)) { sync_file(server, conn_fd, &file); } break; case CSIEBOX_PROTOCOL_OP_SYNC_HARDLINK: fprintf(stderr, ">>>>>>>>>> sync hardlink\n"); csiebox_protocol_hardlink hardlink; if (complete_message_with_header(conn_fd, &header, &hardlink)) { sync_hardlink(server, conn_fd, &hardlink); } break; case CSIEBOX_PROTOCOL_OP_SYNC_END: fprintf(stderr, "========== sync end\n"); break; case CSIEBOX_PROTOCOL_OP_RM: fprintf(stderr, ">>>>>>>>>> sync rm\n"); csiebox_protocol_rm rm; if (complete_message_with_header(conn_fd, &header, &rm)) { sync_rm(server, conn_fd, &rm); } break; default: fprintf(stderr, ">>>>>>>>>> unknown op %x\n", header.req.op); break; } } fprintf(stderr, "========== end of connection\n"); logout(server, conn_fd); }
static int save_bridge_info(struct bridge_info *b) { int rc; char dirpath[PATH_MAX], path[PATH_MAX], path2[PATH_MAX]; char dir1[5], dir2[5]; unsigned char hexpwdhash[41]; FILE *f; fprintf(stderr, "save_bridge_info 1\n"); memset(dir1, 0, sizeof(dir1)); memset(dir2, 0, sizeof(dir2)); snis_format_sha1_hash(b->pwdhash, hexpwdhash, 41); memcpy(&dir1[0], &hexpwdhash[0], 4); memcpy(&dir2[0], &hexpwdhash[4], 4); sprintf(path, "%s/%s", database_root, dir1); if (make_dir(path)) { fprintf(stderr, "snis_multiverse: mkdir failed: %s: %s\n", path, strerror(errno)); return -1; } sprintf(dirpath, "%s/%s/%s", database_root, dir1, dir2); if (make_dir(dirpath)) { fprintf(stderr, "snis_multiverse: mkdir failed: %s: %s\n", dirpath, strerror(errno)); return -1; } sprintf(path, "%s/%s.update", dirpath, hexpwdhash); sprintf(path2, "%s/%s.data", dirpath, hexpwdhash); f = fopen(path, "w+"); if (!f) { fprintf(stderr, "snis_multiverse: fopen %s failed: %s\n", path, strerror(errno)); return -1; } rc = write_bridge_info(f, b); if (rc) { fprintf(stderr, "snis_multiverse: failed to write to %s\n", path); fclose(f); return -1; } sync_file(f); fclose(f); rc = rename(path, path2); sync_dir(dirpath); if (rc) { fprintf(stderr, "snis_multiverse: failed to rename %s to %s: %s\n", path, path2, strerror(errno)); return -1; } fprintf(stderr, "snis_multiverse: wrote bridge info to %s\n", path2); return 0; }
struct sm_stat_res * sm_mon_1_svc(mon *arg, struct svc_req *req) { static sm_stat_res res; HostInfo *hp; MonList *lp; if (debug) { syslog(LOG_DEBUG, "monitor request for host %s", arg->mon_id.mon_name); syslog(LOG_DEBUG, "recall host: %s prog: %d ver: %d proc: %d", arg->mon_id.my_id.my_name, arg->mon_id.my_id.my_prog, arg->mon_id.my_id.my_vers, arg->mon_id.my_id.my_proc); } res.res_stat = stat_fail; /* Assume fail until set otherwise */ res.state = status_info->ourState; /* Find existing host entry, or create one if not found */ /* If find_host() fails, it will have logged the error already. */ if (!gethostbyname(arg->mon_id.mon_name)) { syslog(LOG_ERR, "Invalid hostname to sm_mon: %s", arg->mon_id.mon_name); } else if ((hp = find_host(arg->mon_id.mon_name, TRUE))) { lp = (MonList *)malloc(sizeof(MonList)); if (!lp) { syslog(LOG_ERR, "Out of memory"); } else { strncpy(lp->notifyHost, arg->mon_id.my_id.my_name, SM_MAXSTRLEN); lp->notifyProg = arg->mon_id.my_id.my_prog; lp->notifyVers = arg->mon_id.my_id.my_vers; lp->notifyProc = arg->mon_id.my_id.my_proc; memcpy(lp->notifyData, arg->priv, sizeof(lp->notifyData)); lp->next = hp->monList; hp->monList = lp; sync_file(); res.res_stat = stat_succ; /* Report success */ } } return (&res); }
// done static int handle_request(csiebox_server* server, int conn_fd) { csiebox_protocol_header header; memset(&header, 0, sizeof(header)); int connection = recv_message(conn_fd, &header, sizeof(header)); if (connection > 0 && header.req.magic == CSIEBOX_PROTOCOL_MAGIC_REQ) { switch (header.req.op) { case CSIEBOX_PROTOCOL_OP_LOGIN: fprintf(stderr, "========== "); csiebox_protocol_login req; if (complete_message_with_header(conn_fd, &header, &req)) { login(server, conn_fd, &req); fprintf(stderr, "[%s] [fd = %d] login\n", server->client[conn_fd]->account.user, conn_fd); } return 0; case CSIEBOX_PROTOCOL_OP_SYNC_META: fprintf(stderr, "sync meta\n"); csiebox_protocol_meta meta; if (complete_message_with_header(conn_fd, &header, &meta)) { sync_file(server, conn_fd, &meta); } return 0; case CSIEBOX_PROTOCOL_OP_SYNC_END: fprintf(stderr, "========== [%s] [fd = %d] sync end\n", server->client[conn_fd]->account.user, conn_fd); sync_end(server, conn_fd); return 0; case CSIEBOX_PROTOCOL_OP_RM: fprintf(stderr, "sync rm\n"); csiebox_protocol_rm rm; if (complete_message_with_header(conn_fd, &header, &rm)) { rm_file(server, conn_fd, &rm); } return 0; default: fprintf(stderr, "unknow op %x\n", header.req.op); return 0; } } else if (connection <= 0) { fprintf(stderr, "========== [%s] [fd = %d] logout\n", server->client[conn_fd]->account.user, conn_fd); logout(server, conn_fd); return 1; } return 0; }
int main( int argc, char **argv ) { if(argc < 3) { CL_INFO_PRINT("at least two argument!\n"); } /* ************ filename1 wait sync filename2 dest **************/ char *filename1,*filename2; int fd1,fd2; char * buf; uint32 checksum1; struct stat st; struct chunk_descriptor *chunk_desc; struct chunk_modify_descriptor chunk_modify_desc; filename1 = argv[1]; filename2 = argv[2]; chunk_desc = malloc(sizeof(struct chunk_descriptor)); if(!chunk_desc) { CL_ERR_PRINT("out memory"); return -1; } if(get_file_stat(filename1,chunk_desc)!=0) { return -1; } build_hash_table(chunk_desc); init_chunk_modify_descriptor(&chunk_modify_desc); generate_modify_descriptor(filename2,&chunk_modify_desc,chunk_desc); // print_hash_stat(); // print_chunk_modify(&chunk_modify_desc); sync_file(filename1,&chunk_modify_desc); cleanup_modify_list(&chunk_modify_desc); cleanup_hash_list(); free(chunk_desc->chunk); free(chunk_desc); return 0; }
void SyncTreeWidget::onPressed(QTreeWidgetItem * item, int column) { if (column == 0 && (item->flags() & Qt::ItemIsUserCheckable)) { Qt::CheckState ps = item->checkState(0); FileTreeWidgetItem* ftw = dynamic_cast<FileTreeWidgetItem*>(item); assert(ftw); if (ps == Qt::Checked) { propagateCheckabilityDown(item, true); propagateCheckUpdateUp(item); unsync_file(ftw->fileInfo(), m_master, m_copy); } else { assert (ps == Qt::Unchecked); propagateCheckabilityDown(item, false); propagatePartialCheckUp(item); // We used to be unsynced, now we need to sync sync_file(ftw->fileInfo(), m_master, m_copy); } } }
struct sm_stat * sm_unmon_all_1_svc(my_id *arg, struct svc_req *req) { static sm_stat res; HostInfo *hp; int i; if (debug) { syslog(LOG_DEBUG, "unmon_all for host: %s prog: %d ver: %d proc: %d", arg->my_name, arg->my_prog, arg->my_vers, arg->my_proc); } for (i = status_info->noOfHosts, hp = status_info->hosts; i; i--, hp++) { do_unmon(hp, arg); } sync_file(); res.state = status_info->ourState; return (&res); }
static void sync_all(csiebox_client* client, char* longest_path, int level) { char* cwd = (char*)malloc(sizeof(char) * PATH_MAX); memset(cwd, 0, sizeof(char) * PATH_MAX); if (getcwd(cwd, PATH_MAX) == 0) { fprintf(stderr, "getcwd fail\n"); } add_inotify(client, cwd); DIR* dir; struct dirent* file; struct stat file_stat; dir = opendir("."); while ((file = readdir(dir)) != NULL) { if (strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0) { continue; } lstat(file->d_name, &file_stat); sync_file(client, file->d_name); if ((file_stat.st_mode & S_IFMT) == S_IFDIR) { level++; if (level > max_level) { max_level = level; strcpy(longest_path, convert_to_relative_path(client, file->d_name)); } if (chdir(file->d_name) != 0) { fprintf(stderr, "bad dir %s\n", file->d_name); continue; } sync_all(client, longest_path, level); chdir(cwd); } } closedir(dir); free(cwd); return; }
void notify_hosts(void) { int i; int attempts; int work_to_do = FALSE; HostInfo *hp; pid_t pid; /* First check if there is in fact any work to do. */ for (i = status_info->noOfHosts, hp = status_info->hosts; i ; i--, hp++) { if (hp->notifyReqd) { work_to_do = TRUE; break; } } if (!work_to_do) return; /* No work found */ pid = fork(); if (pid == -1) { syslog(LOG_ERR, "Unable to fork notify process - %s", strerror(errno)); return; } if (pid) return; /* Here in the child process. We continue until all the hosts marked */ /* as requiring notification have been duly notified. */ /* If one of the initial attempts fails, we sleep for a while and */ /* have another go. This is necessary because when we have crashed, */ /* (eg. a power outage) it is quite possible that we won't be able to */ /* contact all monitored hosts immediately on restart, either because */ /* they crashed too and take longer to come up (in which case the */ /* notification isn't really required), or more importantly if some */ /* router etc. needed to reach the monitored host has not come back */ /* up yet. In this case, we will be a bit late in re-establishing */ /* locks (after the grace period) but that is the best we can do. */ /* We try 10 times at 5 sec intervals, 10 more times at 1 minute */ /* intervals, then 24 more times at hourly intervals, finally */ /* giving up altogether if the host hasn't come back to life after */ /* 24 hours. */ for (attempts = 0; attempts < 44; attempts++) { work_to_do = FALSE; /* Unless anything fails */ for (i = status_info->noOfHosts, hp = status_info->hosts; i ; i--, hp++) { if (hp->notifyReqd) { if (notify_one_host(hp->hostname)) { hp->notifyReqd = FALSE; sync_file(); } else work_to_do = TRUE; } } if (!work_to_do) break; if (attempts < 10) sleep(5); else if (attempts < 20) sleep(60); else sleep(60*60); } exit(0); }