Files
dingtalk-workspace-cli/internal/audit/filelock_windows.go
修雨 d0787ce8ee fix(audit): stateless hash chain, waitable forwarder, profile actor, observability
Address PR #555 review:
- chain: derive prev_hash from file tail under cross-process flock, drop the
  global .chain sidecar so per-day files stay independently verifiable and
  concurrent dws processes cannot fork the chain
- forward: track async forwards with WaitGroup and add bounded Close(ctx) so
  in-flight deliveries are not dropped on process exit
- actor: resolve Actor from the active runtime profile (profile-keyed cache)
- observability: BuildSink returns init errors; write/forward failures reported
  to file log and to stderr when DWS_AUDIT_DEBUG is set
- cli: reject `audit tail --lines` < 1; check CSV writer/flush errors
- wire CloseAuditSink into PersistentPostRunE
- add regression tests for cross-date/cross-process chain, forwarder
  wait/timeout, init-failure, tail validation, CSV export
2026-07-14 11:56:09 +08:00

51 lines
1.3 KiB
Go

// Copyright 2026 Alibaba Group
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build windows
package audit
import (
"os"
"unsafe"
"golang.org/x/sys/windows"
)
const lockfileExclusiveLock = 0x00000002
// lockFile takes a non-blocking exclusive lock on the first byte range; returns
// an error when another process holds it so the caller can retry with a timeout.
func lockFile(f *os.File) error {
ol := new(windows.Overlapped)
return windows.LockFileEx(
windows.Handle(f.Fd()),
lockfileExclusiveLock|windows.LOCKFILE_FAIL_IMMEDIATELY,
0,
1,
0,
(*windows.Overlapped)(unsafe.Pointer(ol)),
)
}
func unlockFile(f *os.File) {
ol := new(windows.Overlapped)
_ = windows.UnlockFileEx(
windows.Handle(f.Fd()),
0,
1,
0,
(*windows.Overlapped)(unsafe.Pointer(ol)),
)
}