]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blobdiff - rtcp/xfs_rtcp.c
libfrog: convert fsgeom.c functions to negative error codes
[thirdparty/xfsprogs-dev.git] / rtcp / xfs_rtcp.c
index ebad94d5fc22592953a3ccb21068b76765580e24..7c4197b1314ceb1a6e6d8075ab6d0582da2eb459 100644 (file)
@@ -1,75 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
- * 
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- * 
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- * 
- * Further, this software is distributed without any warranty that it is
- * free of the rightful claim of any third person regarding infringement
- * or the like.  Any license provided herein, whether implied or
- * otherwise, applies only to this software file.  Patent licenses, if
- * any, provided herein do not apply to combinations of this program with
- * other software, or any other product whatsoever.
- * 
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc., 59
- * Temple Place - Suite 330, Boston MA 02111-1307, USA.
- * 
- * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
- * Mountain View, CA  94043, or:
- * 
- * http://www.sgi.com 
- * 
- * For further information regarding this notice, see: 
- * 
- * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
+ * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
+ * All Rights Reserved.
  */
 
-#include <libxfs.h>
-#include <malloc.h>
-#include <sys/stat.h>
-#include <sys/ioctl.h>
+#include "libxfs.h"
+#include "libfrog/fsgeom.h"
 
 int rtcp(char *, char *, int);
 int xfsrtextsize(char *path);
 
-int pflag;
+static int pflag;
 char *progname;
 
-void
-usage()
+static void
+usage(void)
 {
-       fprintf(stderr, "%s [-e extsize] [-p] source target\n", progname);
+       fprintf(stderr, _("%s [-e extsize] [-p] [-V] source target\n"), progname);
        exit(2);
 }
 
 int
 main(int argc, char **argv)
 {
-       register int    c, i, r, errflg = 0;
+       int     c, i, r, errflg = 0;
        struct stat     s2;
-       int             eflag;
        int             extsize = - 1;
 
        progname = basename(argv[0]);
+       setlocale(LC_ALL, "");
+       bindtextdomain(PACKAGE, LOCALEDIR);
+       textdomain(PACKAGE);
 
        while ((c = getopt(argc, argv, "pe:V")) != EOF) {
                switch (c) {
                case 'e':
-                       eflag = 1;
                        extsize = atoi(optarg);
                        break;
                case 'p':
                        pflag = 1;
                        break;
                case 'V':
-                       printf("%s version %s\n", progname, VERSION);
-                       break;
+                       printf(_("%s version %s\n"), progname, VERSION);
+                       exit(0);
                default:
                        errflg++;
                }
@@ -82,7 +55,8 @@ main(int argc, char **argv)
        argv  = &argv[optind];
 
        if (argc < 2) {
-               fprintf(stderr, "%s: must specify files to copy\n", progname);
+               fprintf(stderr, _("%s: must specify files to copy\n"),
+                       progname);
                errflg++;
        }
 
@@ -96,13 +70,14 @@ main(int argc, char **argv)
         */
        if (argc > 2) {
                if (stat(argv[argc-1], &s2) < 0) {
-                       fprintf(stderr, "%s: stat of %s failed\n",
+                       fprintf(stderr, _("%s: stat of %s failed\n"),
                                progname, argv[argc-1]);
                        exit(2);
                }
 
                if (!S_ISDIR(s2.st_mode)) {
-                       fprintf(stderr, "%s: final argument is not directory\n",
+                       fprintf(stderr,
+                               _("%s: final argument is not directory\n"),
                                progname);
                        usage();
                }
@@ -127,9 +102,9 @@ rtcp( char *source, char *target, int fextsize)
 {
        int             fromfd, tofd, readct, writect, iosz, reopen;
        int             remove = 0, rtextsize;
-       char            *sp, *fbuf, *ptr;
+       char            *sp, *fbuf, *ptr;
        char            tbuf[ PATH_MAX ];
-       struct  stat    s1, s2;
+       struct stat     s1, s2;
        struct fsxattr  fsxattr;
        struct dioattr  dioattr;
 
@@ -149,27 +124,28 @@ rtcp( char *source, char *target, int fextsize)
        }
 
        if ( stat(source, &s1) ) {
-               fprintf(stderr, "%s: failed stat on\n", progname);
-               perror(source);
+               fprintf(stderr, _("%s: failed stat on %s: %s\n"),
+                       progname, source, strerror(errno));
                return( -1);
        }
 
        /*
         * check for a realtime partition
         */
-       sprintf(tbuf,"%s",target);
+       snprintf(tbuf, sizeof(tbuf), "%s", target);
        if ( stat(target, &s2) ) {
                if (!S_ISDIR(s2.st_mode)) {
                        /* take out target file name */
                        if ((ptr = strrchr(tbuf, '/')) != NULL)
                                *ptr = '\0';
                        else
-                               sprintf(tbuf, ".");
-               } 
+                               snprintf(tbuf, sizeof(tbuf), ".");
+               }
        }
 
        if ( (rtextsize = xfsrtextsize( tbuf ))  <= 0 ) {
-               fprintf(stderr, "%s: %s filesystem has no realtime partition\n",
+               fprintf(stderr,
+                       _("%s: %s filesystem has no realtime partition\n"),
                        progname, tbuf);
                return( -1 );
        }
@@ -177,36 +153,38 @@ rtcp( char *source, char *target, int fextsize)
        /*
         * check if target is a directory
         */
-       sprintf(tbuf,"%s",target);
+       snprintf(tbuf, sizeof(tbuf), "%s", target);
        if ( !stat(target, &s2) ) {
                if (S_ISDIR(s2.st_mode)) {
-                       sprintf(tbuf,"%s/%s",target, basename(source));
-               } 
+                       snprintf(tbuf, sizeof(tbuf), "%s/%s", target,
+                               basename(source));
+               }
        }
-       
+
        if ( stat(tbuf, &s2) ) {
                /*
                 * create the file if it does not exist
                 */
                if ( (tofd = open(tbuf, O_RDWR|O_CREAT|O_DIRECT, 0666)) < 0 ) {
-                       fprintf(stderr, "%s: Open of %s failed.\n",
-                               progname, tbuf);
+                       fprintf(stderr, _("%s: open of %s failed: %s\n"),
+                               progname, tbuf, strerror(errno));
                        return( -1 );
                }
                remove = 1;
-               
+
                /*
                 * mark the file as a realtime file
                 */
-               fsxattr.fsx_xflags = XFS_XFLAG_REALTIME;
+               fsxattr.fsx_xflags = FS_XFLAG_REALTIME;
                if (fextsize != -1 )
                        fsxattr.fsx_extsize = fextsize;
                else
                        fsxattr.fsx_extsize = 0;
 
-               if ( ioctl( tofd, XFS_IOC_FSSETXATTR, &fsxattr) ) { 
-                       fprintf(stderr, "%s: Set attributes on %s failed.\n",
-                               progname, tbuf);
+               if ( xfsctl(tbuf, tofd, FS_IOC_FSSETXATTR, &fsxattr) ) {
+                       fprintf(stderr,
+                               _("%s: set attributes on %s failed: %s\n"),
+                               progname, tbuf, strerror(errno));
                        close( tofd );
                        unlink( tbuf );
                        return( -1 );
@@ -216,14 +194,15 @@ rtcp( char *source, char *target, int fextsize)
                 * open existing file
                 */
                if ( (tofd = open(tbuf, O_RDWR|O_DIRECT)) < 0 ) {
-                       fprintf(stderr, "%s: Open of %s failed.\n",
-                               progname, tbuf);
+                       fprintf(stderr, _("%s: open of %s failed: %s\n"),
+                               progname, tbuf, strerror(errno));
                        return( -1 );
                }
-               
-               if ( ioctl( tofd, XFS_IOC_FSGETXATTR, &fsxattr) ) {
-                       fprintf(stderr, "%s: Get attributes of %s failed.\n",
-                               progname, tbuf);
+
+               if ( xfsctl(tbuf, tofd, FS_IOC_FSGETXATTR, &fsxattr) ) {
+                       fprintf(stderr,
+                               _("%s: get attributes of %s failed: %s\n"),
+                               progname, tbuf, strerror(errno));
                        close( tofd );
                        return( -1 );
                }
@@ -231,19 +210,21 @@ rtcp( char *source, char *target, int fextsize)
                /*
                 * check if the existing file is already a realtime file
                 */
-               if ( !(fsxattr.fsx_xflags & XFS_XFLAG_REALTIME) ) {
-                       fprintf(stderr, "%s: %s is not a realtime file.\n",
+               if ( !(fsxattr.fsx_xflags & FS_XFLAG_REALTIME) ) {
+                       fprintf(stderr, _("%s: %s is not a realtime file.\n"),
                                progname, tbuf);
+                       close( tofd );
                        return( -1 );
                }
-               
+
                /*
                 * check for matching extent size
                 */
                if ( (fextsize != -1) && (fsxattr.fsx_extsize != fextsize) ) {
-                       fprintf(stderr, "%s: %s file extent size is %d, "
-                                       "instead of %d.\n",
+                       fprintf(stderr, _("%s: %s file extent size is %d, "
+                                       "instead of %d.\n"),
                                progname, tbuf, fsxattr.fsx_extsize, fextsize);
+                       close( tofd );
                        return( -1 );
                }
        }
@@ -253,8 +234,8 @@ rtcp( char *source, char *target, int fextsize)
         */
        reopen = 0;
        if ( (fromfd = open(source, O_RDONLY|O_DIRECT)) < 0 ) {
-               fprintf(stderr, "%s: Open of %s source failed.\n",
-                       progname, source);
+               fprintf(stderr, _("%s: open of %s source failed: %s\n"),
+                       progname, source, strerror(errno));
                close( tofd );
                if (remove)
                        unlink( tbuf );
@@ -263,11 +244,11 @@ rtcp( char *source, char *target, int fextsize)
 
        fsxattr.fsx_xflags = 0;
        fsxattr.fsx_extsize = 0;
-       if ( ioctl( fromfd, XFS_IOC_FSGETXATTR, &fsxattr) ) {
+       if ( xfsctl(source, fromfd, FS_IOC_FSGETXATTR, &fsxattr) ) {
                reopen = 1;
        } else {
-               if (! (fsxattr.fsx_xflags & XFS_XFLAG_REALTIME) ){
-                       fprintf(stderr, "%s: %s is not a realtime file.\n",
+               if (! (fsxattr.fsx_xflags & FS_XFLAG_REALTIME) ){
+                       fprintf(stderr, _("%s: %s is not a realtime file.\n"),
                                progname, source);
                        reopen = 1;
                }
@@ -276,8 +257,8 @@ rtcp( char *source, char *target, int fextsize)
        if (reopen) {
                close( fromfd );
                if ( (fromfd = open(source, O_RDONLY )) < 0 ) {
-                       fprintf(stderr, "%s: Open of %s source failed.\n",
-                               progname, source);
+                       fprintf(stderr, _("%s: open of %s source failed: %s\n"),
+                               progname, source, strerror(errno));
                        close( tofd );
                        if (remove)
                                unlink( tbuf );
@@ -288,18 +269,19 @@ rtcp( char *source, char *target, int fextsize)
        /*
         * get direct I/O parameters
         */
-       if ( ioctl( tofd, XFS_IOC_DIOINFO, &dioattr) ) {
-               fprintf(stderr, "%s: Could not get direct I/O information.\n",
-                       progname);
+       if ( xfsctl(tbuf, tofd, XFS_IOC_DIOINFO, &dioattr) ) {
+               fprintf(stderr,
+                       _("%s: couldn't get direct I/O information: %s\n"),
+                       progname, strerror(errno));
                close( fromfd );
                close( tofd );
-               if ( remove ) 
+               if ( remove )
                        unlink( tbuf );
                return( -1 );
        }
 
        if ( rtextsize % dioattr.d_miniosz ) {
-               fprintf(stderr, "%s: extent size %d not a multiple of %d.\n",
+               fprintf(stderr, _("%s: extent size %d not a multiple of %d.\n"),
                        progname, rtextsize, dioattr.d_miniosz);
                close( fromfd );
                close( tofd );
@@ -313,17 +295,17 @@ rtcp( char *source, char *target, int fextsize)
         * file system block size.
         */
        if ( s1.st_size % dioattr.d_miniosz ) {
-               printf("The size of %s is not a multiple of %d.\n",
+               printf(_("The size of %s is not a multiple of %d.\n"),
                        source, dioattr.d_miniosz);
                if ( pflag ) {
-                       printf("%s will be padded to %lld bytes.\n",
+                       printf(_("%s will be padded to %lld bytes.\n"),
                                tbuf, (long long)
                                (((s1.st_size / dioattr.d_miniosz) + 1)  *
                                        dioattr.d_miniosz) );
-                               
+
                } else {
-                       printf("Use the -p option to pad %s "
-                               "to a size which is a multiple of %d bytes.\n",
+                       printf(_("Use the -p option to pad %s to a "
+                               "size which is a multiple of %d bytes.\n"),
                                tbuf, dioattr.d_miniosz);
                        close( fromfd );
                        close( tofd );
@@ -335,7 +317,7 @@ rtcp( char *source, char *target, int fextsize)
 
        iosz =  dioattr.d_miniosz;
        fbuf = memalign( dioattr.d_mem, iosz);
-       bzero (fbuf, iosz);
+       memset(fbuf, 0, iosz);
 
        /*
         * read the entire source file
@@ -350,7 +332,7 @@ rtcp( char *source, char *target, int fextsize)
 
                /*
                 * if there is a short read, pad to a block boundary
-                */
+                */
                if ( readct != iosz ) {
                        if ( (readct % dioattr.d_miniosz)  != 0 )  {
                                readct = ( (readct/dioattr.d_miniosz) + 1 ) *
@@ -359,19 +341,20 @@ rtcp( char *source, char *target, int fextsize)
                }
 
                /*
-                * write to target file 
+                * write to target file
                 */
                writect = write( tofd, fbuf, readct);
 
                if ( writect != readct ) {
-                       fprintf(stderr, "%s: Write error.\n", progname);
+                       fprintf(stderr, _("%s: write error: %s\n"),
+                               progname, strerror(errno));
                        close(fromfd);
                        close(tofd);
                        free( fbuf );
                        return( -1 );
                }
 
-               bzero( fbuf, iosz);
+               memset( fbuf, 0, iosz);
        }
 
        close(fromfd);
@@ -381,26 +364,26 @@ rtcp( char *source, char *target, int fextsize)
 }
 
 /*
- * Determine the realtime extent size of the XFS file system 
+ * Determine the realtime extent size of the XFS file system
  */
 int
 xfsrtextsize( char *path)
 {
-       int fd, rval, rtextsize;
-       xfs_fsop_geom_t geo;
+       struct xfs_fsop_geom    geo;
+       int                     fd, rval, rtextsize;
 
        fd = open( path, O_RDONLY );
        if ( fd < 0 ) {
-               fprintf(stderr, "%s: Could not open ", progname);
-               perror(path);
+               fprintf(stderr, _("%s: could not open %s: %s\n"),
+                       progname, path, strerror(errno));
                return -1;
        }
-       rval = ioctl(fd, XFS_IOC_FSGEOMETRY, &geo );
+       rval = -xfrog_geometry(fd, &geo);
        close(fd);
+       if (rval)
+               return -1;
 
        rtextsize = geo.rtextsize * geo.blocksize;
 
-       if ( rval < 0 )
-               return -1;
        return rtextsize;
 }