# 5.6 Working through a Handle

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

> Reading back what an open was granted, deleting a file through its handle, and renaming the link a handle names.

These calls act on the file an open descriptor refers to, rather than on a
name. Each is authorized by the access the descriptor was granted when it
opened, so they work on the file you opened even if someone has since renamed
it, and they cannot hit a different file that has taken the old name. They
need a descriptor from [`peios_file_open`](/peios/developing-for-peios/sdk-reference/sdk-files/opening-a-file.md);
any other descriptor fails with `EOPNOTSUPP`.

## 5.6.1 Reading back the grant

```c
int peios_file_query_open(int fd, struct kacs_open_info *out);
```

Fills `out` from the open:

| Field | Meaning |
|---|---|
| `granted_access` | The access mask the descriptor carries. After a `MAXIMUM_ALLOWED` open, this is what you received. |
| `share_access` | The `KACS_FILE_SHARE_*` sharing the open allows others. |
| `flags` | `KACS_OPEN_INFO_MANAGED`; `_DELETE_PENDING` if the file is marked for deletion; `_DELETE_ON_CLOSE` if this open will mark it when it closes; `_ANCESTORS_PINNED` if the open pins its parent directories. |

The call needs no right: it reports only what the descriptor already carries.

## 5.6.2 Deleting through the handle

```c
int peios_file_set_disposition(int fd, uint32_t flags);
```

Sets the file's **delete disposition** — what happens to it once nothing has
it open. The open needs `DELETE`, or the call fails with `EACCES`. The
flags are those of NT's `FILE_DISPOSITION_INFORMATION_EX`:

| Flag | Meaning |
|---|---|
| `KACS_DISPOSITION_DELETE` | Mark the file **delete-pending**: it is removed when its last open closes, from whichever process. Without this flag, the call clears the mark. |
| `KACS_DISPOSITION_POSIX_SEMANTICS` | With `DELETE`: remove the name now, as `unlink(2)` does. Open descriptors carry on with the file. |
| `KACS_DISPOSITION_ON_CLOSE` | Set or clear this open's own delete-on-close instead: the file becomes delete-pending when this open closes. |
| `KACS_DISPOSITION_IGNORE_READONLY` | Delete a read-only file. Needs `FILE_WRITE_ATTRIBUTES` too. |

While a file is delete-pending its name stays and still lists, but every new
open of it fails with `EBUSY` and the status `KACS_STATUS_DELETE_PENDING`.
The removal is authorized by whoever set the mark, not by whoever closes the
file last.

A read-only file fails with `EPERM` unless `KACS_DISPOSITION_IGNORE_READONLY`
is given. A directory has to be empty when it is removed.

```c
/* Delete the file once everyone has finished with it. */
if (peios_file_set_disposition(fd, KACS_DISPOSITION_DELETE) != 0) { /* errno */ }
close(fd);
```

## 5.6.3 Renaming through the handle

```c
int peios_file_rename(int fd, int newdirfd, const char *newpath, uint32_t flags);
```

Moves the link `fd` names to `newpath`, resolved relative to `newdirfd` as by
`renameat2()`. The open needs `DELETE`; the destination directory is checked
as for any rename. The flags are those of NT's `FILE_RENAME_INFORMATION_EX`:

| Flag | Meaning |
|---|---|
| `KACS_RENAME_REPLACE_IF_EXISTS` | Replace an existing file at `newpath`. Without it, an existing name fails with `EEXIST`. |
| `KACS_RENAME_POSIX_SEMANTICS` | Replace a file that is open, if all its opens share delete. |
| `KACS_RENAME_IGNORE_READONLY` | Replace a read-only file. |

A directory is never replaced. A file that is open, delete-pending, or
read-only without the flag fails with `EBUSY` or `EPERM`. If the handle's link
has been removed, the call fails with `ENOENT`. Both names have to be on the
same mount, or the call fails with `EXDEV`.

## 5.6.4 See also

- **[Sharing and
  Deletion](/kernel/internals/kacs/file-security/sharing-and-deletion.md)** —
  the kernel's rules for delete-pending files and renaming through a handle.
