static int write_disk_entry(git_filebuf *file, git_index_entry *entry) { struct entry_short *ondisk; size_t path_len, disk_size; char *path; path_len = strlen(entry->path); if (entry->flags & GIT_IDXENTRY_EXTENDED) disk_size = long_entry_size(path_len); else disk_size = short_entry_size(path_len); if (git_filebuf_reserve(file, (void **)&ondisk, disk_size) < GIT_SUCCESS) return GIT_ENOMEM; memset(ondisk, 0x0, disk_size); /** * Yes, we have to truncate. * * The on-disk format for Index entries clearly defines * the time and size fields to be 4 bytes each -- so even if * we store these values with 8 bytes on-memory, they must * be truncated to 4 bytes before writing to disk. * * In 2038 I will be either too dead or too rich to care about this */ ondisk->ctime.seconds = htonl((uint32_t)entry->ctime.seconds); ondisk->mtime.seconds = htonl((uint32_t)entry->mtime.seconds); ondisk->ctime.nanoseconds = htonl(entry->ctime.nanoseconds); ondisk->mtime.nanoseconds = htonl(entry->mtime.nanoseconds); ondisk->dev = htonl(entry->dev); ondisk->ino = htonl(entry->ino); ondisk->mode = htonl(entry->mode); ondisk->uid = htonl(entry->uid); ondisk->gid = htonl(entry->gid); ondisk->file_size = htonl((uint32_t)entry->file_size); git_oid_cpy(&ondisk->oid, &entry->oid); ondisk->flags = htons(entry->flags); if (entry->flags & GIT_IDXENTRY_EXTENDED) { struct entry_long *ondisk_ext; ondisk_ext = (struct entry_long *)ondisk; ondisk_ext->flags_extended = htons(entry->flags_extended); path = ondisk_ext->path; } else path = ondisk->path; memcpy(path, entry->path, path_len); return GIT_SUCCESS; }
static size_t read_entry(git_index_entry *dest, const void *buffer, size_t buffer_size) { size_t path_length, entry_size; uint16_t flags_raw; const char *path_ptr; const struct entry_short *source; if (INDEX_FOOTER_SIZE + minimal_entry_size > buffer_size) return 0; source = (const struct entry_short *)(buffer); dest->ctime.seconds = ntohl(source->ctime.seconds); dest->ctime.nanoseconds = ntohl(source->ctime.nanoseconds); dest->mtime.seconds = ntohl(source->mtime.seconds); dest->mtime.nanoseconds = ntohl(source->mtime.nanoseconds); dest->dev = ntohl(source->dev); dest->ino = ntohl(source->ino); dest->mode = ntohl(source->mode); dest->uid = ntohl(source->uid); dest->gid = ntohl(source->gid); dest->file_size = ntohl(source->file_size); git_oid_cpy(&dest->oid, &source->oid); dest->flags = ntohs(source->flags); if (dest->flags & GIT_IDXENTRY_EXTENDED) { struct entry_long *source_l = (struct entry_long *)source; path_ptr = source_l->path; flags_raw = ntohs(source_l->flags_extended); memcpy(&dest->flags_extended, &flags_raw, 2); } else path_ptr = source->path; path_length = dest->flags & GIT_IDXENTRY_NAMEMASK; /* if this is a very long string, we must find its * real length without overflowing */ if (path_length == 0xFFF) { const char *path_end; path_end = memchr(path_ptr, '\0', buffer_size); if (path_end == NULL) return 0; path_length = path_end - path_ptr; } if (dest->flags & GIT_IDXENTRY_EXTENDED) entry_size = long_entry_size(path_length); else entry_size = short_entry_size(path_length); if (INDEX_FOOTER_SIZE + entry_size > buffer_size) return 0; dest->path = git__strdup(path_ptr); assert(dest->path); return entry_size; }