# 5.8 Special Attributes

_Peios / Developing for Peios / SDK Reference / file.h — File Security_

> Reading and setting a file's read-only flag and Created At, and finding its creation date.

KACS keeps two attributes for every file on a managed mount:

- **Read-only.** A read-only regular file cannot be opened for writing or
  deleted, by anyone, its owner included, until the flag is cleared.
- **Created At**, a settable creation date. It differs from the filesystem's
  **birth time**, which `statx` reports and nobody can set: Created At is the
  date a person sees, which a copy or a restore can carry across.

```c
int peios_file_get_sattr(int dirfd, const char *path, struct kacs_sattr *out, uint32_t at_flags);
int peios_file_set_sattr(int dirfd, const char *path, const struct kacs_sattr *attrs,
                         uint32_t at_flags);
int peios_fd_get_sattr(int fd, struct kacs_sattr *out);
int peios_fd_set_sattr(int fd, const struct kacs_sattr *attrs);
```

`struct kacs_sattr` holds both:

| Field | Meaning |
|---|---|
| `version` | `KACS_SATTR_VERSION`. |
| `valid` | On a write, which fields to change: `KACS_SATTR_VALID_ATTRIBUTES`, `KACS_SATTR_VALID_CREATED_AT`. A read sets both. |
| `attributes` | `KACS_SATTR_READONLY`. |
| `flags` | `KACS_SATTR_CREATED_AT_SET` if Created At is set. |
| `created_at_sec`, `created_at_nsec` | Created At, since the Unix epoch. |

A read always fills the struct: a file that has never had either attribute
set reads as not read-only with no Created At. A write changes only the fields
its `valid` mask names, so two programs changing different attributes cannot
undo each other. The struct means the same in both directions: a write with
`KACS_SATTR_VALID_CREATED_AT` sets Created At if `flags` carries
`KACS_SATTR_CREATED_AT_SET`, and unsets it if not. A struct read from one file
can therefore be written to another to copy both attributes exactly.

Reading needs `FILE_READ_ATTRIBUTES` and writing `FILE_WRITE_ATTRIBUTES`:
through the descriptor's grant for the fd forms, or a live check for the path
forms. `at_flags` accepts `AT_SYMLINK_NOFOLLOW`.

```c
/* Make a file read-only, leaving its Created At alone. */
struct kacs_sattr a = {
    .version    = KACS_SATTR_VERSION,
    .valid      = KACS_SATTR_VALID_ATTRIBUTES,
    .attributes = KACS_SATTR_READONLY,
};
if (peios_file_set_sattr(AT_FDCWD, "contract.pdf", &a, 0) != 0) { /* errno */ }
```

## 5.8.1 The creation date

```c
int peios_file_creation_time(int dirfd, const char *path, int64_t *sec, uint32_t *nsec,
                             uint32_t at_flags);
int peios_fd_creation_time(int fd, int64_t *sec, uint32_t *nsec);
```

A file's creation date is its Created At while that is set, and its birth time
otherwise. These calls apply that rule, so a program showing or copying a
creation date does not have to. They fail with `ENODATA` if the file has
neither, as on a filesystem that records no birth time.

A tool that copies files and wants the copy to keep its creation date should
read it with `peios_file_creation_time` and set it on the copy as Created At.

## 5.8.2 See also

- **[Special Attributes](/kernel/internals/kacs/file-security/special-attributes.md)**
  — the kernel's rules, including what read-only refuses.
