Andrew Jeffery | 45cfc38 | 2017-04-12 14:15:50 +0930 | [diff] [blame] | 1 | /* |
| 2 | * Mailbox Daemon Temporary File helpers |
| 3 | * |
| 4 | * Copyright 2017 IBM |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #define _GNU_SOURCE |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include "test/tmpf.h" |
| 27 | |
Andrew Jeffery | c314404 | 2018-02-26 13:24:52 +1030 | [diff] [blame] | 28 | static const char *tmpf_dir = "/tmp/"; |
| 29 | |
Andrew Jeffery | 45cfc38 | 2017-04-12 14:15:50 +0930 | [diff] [blame] | 30 | int tmpf_init(struct tmpf *tmpf, const char *template) |
| 31 | { |
Andrew Jeffery | c314404 | 2018-02-26 13:24:52 +1030 | [diff] [blame] | 32 | strcpy(tmpf->path, tmpf_dir); |
| 33 | strncat(tmpf->path, template, sizeof(tmpf->path) - sizeof(tmpf_dir)); |
Andrew Jeffery | 45cfc38 | 2017-04-12 14:15:50 +0930 | [diff] [blame] | 34 | |
| 35 | tmpf->fd = mkstemp(tmpf->path); |
| 36 | if (tmpf->fd < 0) { |
| 37 | perror("mkstemp"); |
| 38 | return -1; |
| 39 | } |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | void tmpf_destroy(struct tmpf *tmpf) |
| 45 | { |
| 46 | if (tmpf->fd) |
| 47 | close(tmpf->fd); |
| 48 | |
| 49 | if (tmpf->path) |
| 50 | unlink(tmpf->path); |
| 51 | } |