@@ -308,14 +308,11 @@ int fchmod_umask(int fd, mode_t m) {
308308}
309309
310310int fchmod_opath (int fd , mode_t m ) {
311- char procfs_path [STRLEN ("/proc/self/fd/" ) + DECIMAL_STR_MAX (int )];
312-
313311 /* This function operates also on fd that might have been opened with
314312 * O_PATH. Indeed fchmodat() doesn't have the AT_EMPTY_PATH flag like
315313 * fchownat() does. */
316314
317- xsprintf (procfs_path , "/proc/self/fd/%i" , fd );
318- if (chmod (procfs_path , m ) < 0 ) {
315+ if (chmod (FORMAT_PROC_FD_PATH (fd ), m ) < 0 ) {
319316 if (errno != ENOENT )
320317 return - errno ;
321318
@@ -329,12 +326,9 @@ int fchmod_opath(int fd, mode_t m) {
329326}
330327
331328int futimens_opath (int fd , const struct timespec ts [2 ]) {
332- char procfs_path [STRLEN ("/proc/self/fd/" ) + DECIMAL_STR_MAX (int )];
333-
334329 /* Similar to fchmod_path() but for futimens() */
335330
336- xsprintf (procfs_path , "/proc/self/fd/%i" , fd );
337- if (utimensat (AT_FDCWD , procfs_path , ts , 0 ) < 0 ) {
331+ if (utimensat (AT_FDCWD , FORMAT_PROC_FD_PATH (fd ), ts , 0 ) < 0 ) {
338332 if (errno != ENOENT )
339333 return - errno ;
340334
@@ -380,7 +374,6 @@ int fd_warn_permissions(const char *path, int fd) {
380374}
381375
382376int touch_file (const char * path , bool parents , usec_t stamp , uid_t uid , gid_t gid , mode_t mode ) {
383- char fdpath [STRLEN ("/proc/self/fd/" ) + DECIMAL_STR_MAX (int )];
384377 _cleanup_close_ int fd = -1 ;
385378 int r , ret = 0 ;
386379
@@ -412,18 +405,16 @@ int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gi
412405 /* Let's make a path from the fd, and operate on that. With this logic, we can adjust the access mode,
413406 * ownership and time of the file node in all cases, even if the fd refers to an O_PATH object — which is
414407 * something fchown(), fchmod(), futimensat() don't allow. */
415- xsprintf (fdpath , "/proc/self/fd/%i" , fd );
416-
417408 ret = fchmod_and_chown (fd , mode , uid , gid );
418409
419410 if (stamp != USEC_INFINITY ) {
420411 struct timespec ts [2 ];
421412
422413 timespec_store (& ts [0 ], stamp );
423414 ts [1 ] = ts [0 ];
424- r = utimensat (AT_FDCWD , fdpath , ts , 0 );
415+ r = utimensat (AT_FDCWD , FORMAT_PROC_FD_PATH ( fd ) , ts , 0 );
425416 } else
426- r = utimensat (AT_FDCWD , fdpath , NULL , 0 );
417+ r = utimensat (AT_FDCWD , FORMAT_PROC_FD_PATH ( fd ) , NULL , 0 );
427418 if (r < 0 && ret >= 0 )
428419 return - errno ;
429420
@@ -703,13 +694,10 @@ int unlink_or_warn(const char *filename) {
703694}
704695
705696int inotify_add_watch_fd (int fd , int what , uint32_t mask ) {
706- char path [STRLEN ("/proc/self/fd/" ) + DECIMAL_STR_MAX (int ) + 1 ];
707697 int wd ;
708698
709699 /* This is like inotify_add_watch(), except that the file to watch is not referenced by a path, but by an fd */
710- xsprintf (path , "/proc/self/fd/%i" , what );
711-
712- wd = inotify_add_watch (fd , path , mask );
700+ wd = inotify_add_watch (fd , FORMAT_PROC_FD_PATH (what ), mask );
713701 if (wd < 0 )
714702 return - errno ;
715703
@@ -1156,7 +1144,6 @@ int chase_symlinks_and_opendir(
11561144 char * * ret_path ,
11571145 DIR * * ret_dir ) {
11581146
1159- char procfs_path [STRLEN ("/proc/self/fd/" ) + DECIMAL_STR_MAX (int )];
11601147 _cleanup_close_ int path_fd = -1 ;
11611148 _cleanup_free_ char * p = NULL ;
11621149 DIR * d ;
@@ -1182,8 +1169,7 @@ int chase_symlinks_and_opendir(
11821169 return r ;
11831170 assert (path_fd >= 0 );
11841171
1185- xsprintf (procfs_path , "/proc/self/fd/%i" , path_fd );
1186- d = opendir (procfs_path );
1172+ d = opendir (FORMAT_PROC_FD_PATH (path_fd ));
11871173 if (!d )
11881174 return - errno ;
11891175
@@ -1237,12 +1223,9 @@ int chase_symlinks_and_stat(
12371223}
12381224
12391225int access_fd (int fd , int mode ) {
1240- char p [STRLEN ("/proc/self/fd/" ) + DECIMAL_STR_MAX (fd ) + 1 ];
1241-
12421226 /* Like access() but operates on an already open fd */
12431227
1244- xsprintf (p , "/proc/self/fd/%i" , fd );
1245- if (access (p , mode ) < 0 ) {
1228+ if (access (FORMAT_PROC_FD_PATH (fd ), mode ) < 0 ) {
12461229 if (errno != ENOENT )
12471230 return - errno ;
12481231
0 commit comments