+
+int pa_authkey_save(const char *fn, const void *data, size_t length) {
+ int fd = -1;
+ int unlock = 0, ret = -1;
+ ssize_t r;
+ char path[PATH_MAX];
+ const char *p;
+ assert(fn && data && length);
+
+ if (!(p = normalize_path(fn, path, sizeof(path))))
+ return -2;
+
+ if ((fd = open(p, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
+ pa_log(__FILE__": failed to open cookie file '%s': %s\n", fn, strerror(errno));
+ goto finish;
+ }
+
+ unlock = pa_lock_fd(fd, 1) >= 0;
+
+ if ((r = pa_loop_write(fd, data, length)) < 0 || (size_t) r != length) {
+ pa_log(__FILE__": failed to read cookie file '%s': %s\n", fn, strerror(errno));
+ goto finish;
+ }
+
+ ret = 0;
+
+finish:
+
+ if (fd >= 0) {
+
+ if (unlock)
+ pa_lock_fd(fd, 0);
+
+ close(fd);
+ }
+
+ return ret;
+}