bool CfgSaveExW3(CFG_RW *rw, FOLDER *f, wchar_t *name, UINT *written_size, bool write_binary) { wchar_t tmp[MAX_SIZE]; bool text = !write_binary; UCHAR hash[SHA1_SIZE]; BUF *b; IO *o; bool ret = true; UINT dummy_int = 0; // Validate arguments if (name == NULL || f == NULL) { return false; } if (written_size == NULL) { written_size = &dummy_int; } // Convert to buffer b = CfgFolderToBuf(f, text); if (b == NULL) { return false; } // Hash the contents Hash(hash, b->Buf, b->Size, true); // Compare the contents to be written with the content which was written last if (rw != NULL) { if (Cmp(hash, rw->LashHash, SHA1_SIZE) == 0) { // Contents are not changed ret = false; } else { Copy(rw->LashHash, hash, SHA1_SIZE); } } if (ret || OS_IS_UNIX(GetOsInfo()->OsType)) { // Generate a temporary file name UniFormat(tmp, sizeof(tmp), L"%s.log", name); // Copy the file that currently exist to a temporary file FileCopyW(name, tmp); // Save the new file o = FileCreateW(name); if (o != NULL) { if (FileWrite(o, b->Buf, b->Size) == false) { // File saving failure FileClose(o); FileDeleteW(name); FileRenameW(tmp, name); if (rw != NULL) { Zero(rw->LashHash, sizeof(rw->LashHash)); } } else { // Successful saving file FileClose(o); // Delete the temporary file FileDeleteW(tmp); } } else { // File saving failure FileRenameW(tmp, name); if (rw != NULL) { Zero(rw->LashHash, sizeof(rw->LashHash)); } } } *written_size = b->Size; // Release memory FreeBuf(b); return ret; }
// Add a local-bridge void AddLocalBridge(CEDAR *c, char *hubname, char *devicename, bool local, bool monitor, bool tapmode, char *tapaddr, bool limit_broadcast) { UINT i; HUB *h = NULL; LOCALBRIDGE *br = NULL; // Validate arguments if (c == NULL || hubname == NULL || devicename == NULL) { return; } if (OS_IS_UNIX(GetOsInfo()->OsType) == false) { tapmode = false; } LockList(c->HubList); { LockList(c->LocalBridgeList); { bool exists = false; // Ensure that the same configuration local-bridge doesn't exist already for (i = 0;i < LIST_NUM(c->LocalBridgeList);i++) { LOCALBRIDGE *br = LIST_DATA(c->LocalBridgeList, i); if (StrCmpi(br->DeviceName, devicename) == 0) { if (StrCmpi(br->HubName, hubname) == 0) { if (br->TapMode == tapmode) { exists = true; } } } } if (exists == false) { // Add configuration br = ZeroMalloc(sizeof(LOCALBRIDGE)); StrCpy(br->HubName, sizeof(br->HubName), hubname); StrCpy(br->DeviceName, sizeof(br->DeviceName), devicename); br->Bridge = NULL; br->Local = local; br->TapMode = tapmode; br->LimitBroadcast = limit_broadcast; br->Monitor = monitor; if (br->TapMode) { if (tapaddr != NULL && IsZero(tapaddr, 6) == false) { Copy(br->TapMacAddress, tapaddr, 6); } else { GenMacAddress(br->TapMacAddress); } } Add(c->LocalBridgeList, br); // Find the hub for (i = 0;i < LIST_NUM(c->HubList);i++) { HUB *hub = LIST_DATA(c->HubList, i); if (StrCmpi(hub->Name, br->HubName) == 0) { h = hub; AddRef(h->ref); break; } } } } UnlockList(c->LocalBridgeList); } UnlockList(c->HubList); // Start the local-bridge immediately if (h != NULL && br != NULL && h->Type != HUB_TYPE_FARM_DYNAMIC) { Lock(h->lock_online); { if (h->Offline == false) { LockList(c->LocalBridgeList); { if (IsInList(c->LocalBridgeList, br)) { if (br->Bridge == NULL) { br->Bridge = BrNewBridge(h, br->DeviceName, NULL, br->Local, br->Monitor, br->TapMode, br->TapMacAddress, br->LimitBroadcast, br); } } } UnlockList(c->LocalBridgeList); } } Unlock(h->lock_online); } ReleaseHub(h); }