-/*
- * This is all pretty stupid, but we use this packetized line
- * format to make a streaming format possible without ever
- * over-running the read buffers. That way we'll never read
- * into what might be the pack data (which should go to another
- * process entirely).
- *
- * The writing side could use stdio, but since the reading
- * side can't, we stay with pure read/write interfaces.
- */
-static void safe_read(int fd, void *buffer, unsigned size)
-{
- int n = 0;
-
- while (n < size) {
- int ret = read(0, buffer + n, size - n);
- if (ret < 0) {
- if (errno == EINTR || errno == EAGAIN)
- continue;
- die("read error (%s)", strerror(errno));
- }
- if (!ret)
- die("unexpected EOF");
- n += ret;
+ namelen = strlen(name);
+ lock_name = xmalloc(namelen + 10);
+ memcpy(lock_name, name, namelen);
+ memcpy(lock_name + namelen, ".lock", 6);
+
+ strcpy(new_hex, sha1_to_hex(new_sha1));
+ old_hex = sha1_to_hex(old_sha1);
+ if (!has_sha1_file(new_sha1)) {
+ cmd->error_string = "bad pack";
+ return error("unpack should have generated %s, "
+ "but I can't find it!", new_hex);
+ }
+ safe_create_leading_directories(lock_name);
+
+ newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
+ if (newfd < 0) {
+ cmd->error_string = "can't lock";
+ return error("unable to create %s (%s)",
+ lock_name, strerror(errno));
+ }
+
+ /* Write the ref with an ending '\n' */
+ new_hex[40] = '\n';
+ new_hex[41] = 0;
+ written = write(newfd, new_hex, 41);
+ /* Remove the '\n' again */
+ new_hex[40] = 0;
+
+ close(newfd);
+ if (written != 41) {
+ unlink(lock_name);
+ cmd->error_string = "can't write";
+ return error("unable to write %s", lock_name);
+ }
+ if (verify_old_ref(name, old_hex) < 0) {
+ unlink(lock_name);
+ cmd->error_string = "raced";
+ return error("%s changed during push", name);
+ }
+ if (run_update_hook(name, old_hex, new_hex)) {
+ unlink(lock_name);
+ cmd->error_string = "hook declined";
+ return error("hook declined to update %s", name);
+ }
+ else if (rename(lock_name, name) < 0) {
+ unlink(lock_name);
+ cmd->error_string = "can't rename";
+ return error("unable to replace %s", name);
+ }
+ else {
+ fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
+ return 0;