@@ -71,6 +71,89 @@ zfs_append_partition(char *path, size_t max_len)
71
71
return (len );
72
72
}
73
73
74
+ /*
75
+ * Remove partition suffix from a vdev path. Partition suffixes may take three
76
+ * forms: "-partX", "pX", or "X", where X is a string of digits. The second
77
+ * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
78
+ * third case only occurs when preceded by a string matching the regular
79
+ * expression "^([hsv]|xv)d[a-z]+", i.e. a scsi, ide, virtio or xen disk.
80
+ *
81
+ * caller must free the returned string
82
+ */
83
+ char *
84
+ zfs_strip_partition (char * path )
85
+ {
86
+ char * tmp = strdup (path );
87
+ char * part = NULL , * d = NULL ;
88
+ if (!tmp )
89
+ return (NULL );
90
+
91
+ if ((part = strstr (tmp , "-part" )) && part != tmp ) {
92
+ d = part + 5 ;
93
+ } else if ((part = strrchr (tmp , 'p' )) &&
94
+ part > tmp + 1 && isdigit (* (part - 1 ))) {
95
+ d = part + 1 ;
96
+ } else if ((tmp [0 ] == 'h' || tmp [0 ] == 's' || tmp [0 ] == 'v' ) &&
97
+ tmp [1 ] == 'd' ) {
98
+ for (d = & tmp [2 ]; isalpha (* d ); part = ++ d ) { }
99
+ } else if (strncmp ("xvd" , tmp , 3 ) == 0 ) {
100
+ for (d = & tmp [3 ]; isalpha (* d ); part = ++ d ) { }
101
+ }
102
+ if (part && d && * d != '\0' ) {
103
+ for (; isdigit (* d ); d ++ ) { }
104
+ if (* d == '\0' )
105
+ * part = '\0' ;
106
+ }
107
+
108
+ return (tmp );
109
+ }
110
+
111
+ /*
112
+ * Same as zfs_strip_partition, but allows "/dev/" to be in the pathname
113
+ *
114
+ * path: /dev/sda1
115
+ * returns: /dev/sda
116
+ *
117
+ * Returned string must be freed.
118
+ */
119
+ static char *
120
+ zfs_strip_partition_path (char * path )
121
+ {
122
+ char * newpath = strdup (path );
123
+ char * sd_offset ;
124
+ char * new_sd ;
125
+
126
+ if (!newpath )
127
+ return (NULL );
128
+
129
+ /* Point to "sda1" part of "/dev/sda1" */
130
+ sd_offset = strrchr (newpath , '/' ) + 1 ;
131
+
132
+ /* Get our new name "sda" */
133
+ new_sd = zfs_strip_partition (sd_offset );
134
+ if (!new_sd ) {
135
+ free (newpath );
136
+ return (NULL );
137
+ }
138
+
139
+ /* Paste the "sda" where "sda1" was */
140
+ strlcpy (sd_offset , new_sd , strlen (sd_offset ) + 1 );
141
+
142
+ /* Free temporary "sda" */
143
+ free (new_sd );
144
+
145
+ return (newpath );
146
+ }
147
+
148
+ /*
149
+ * Strip the unwanted portion of a device path.
150
+ */
151
+ char *
152
+ zfs_strip_path (char * path )
153
+ {
154
+ return (strrchr (path , '/' ) + 1 );
155
+ }
156
+
74
157
/*
75
158
* Allocate and return the underlying device name for a device mapper device.
76
159
* If a device mapper device maps to multiple devices, return the first device.
@@ -349,80 +432,6 @@ zfs_get_enclosure_sysfs_path(const char *dev_name)
349
432
return (path );
350
433
}
351
434
352
- /*
353
- * Remove partition suffix from a vdev path. Partition suffixes may take three
354
- * forms: "-partX", "pX", or "X", where X is a string of digits. The second
355
- * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
356
- * third case only occurs when preceded by a string matching the regular
357
- * expression "^([hsv]|xv)d[a-z]+", i.e. a scsi, ide, virtio or xen disk.
358
- *
359
- * caller must free the returned string
360
- */
361
- char *
362
- zfs_strip_partition (char * path )
363
- {
364
- char * tmp = strdup (path );
365
- char * part = NULL , * d = NULL ;
366
- if (!tmp )
367
- return (NULL );
368
-
369
- if ((part = strstr (tmp , "-part" )) && part != tmp ) {
370
- d = part + 5 ;
371
- } else if ((part = strrchr (tmp , 'p' )) &&
372
- part > tmp + 1 && isdigit (* (part - 1 ))) {
373
- d = part + 1 ;
374
- } else if ((tmp [0 ] == 'h' || tmp [0 ] == 's' || tmp [0 ] == 'v' ) &&
375
- tmp [1 ] == 'd' ) {
376
- for (d = & tmp [2 ]; isalpha (* d ); part = ++ d ) { }
377
- } else if (strncmp ("xvd" , tmp , 3 ) == 0 ) {
378
- for (d = & tmp [3 ]; isalpha (* d ); part = ++ d ) { }
379
- }
380
- if (part && d && * d != '\0' ) {
381
- for (; isdigit (* d ); d ++ ) { }
382
- if (* d == '\0' )
383
- * part = '\0' ;
384
- }
385
-
386
- return (tmp );
387
- }
388
-
389
- /*
390
- * Same as zfs_strip_partition, but allows "/dev/" to be in the pathname
391
- *
392
- * path: /dev/sda1
393
- * returns: /dev/sda
394
- *
395
- * Returned string must be freed.
396
- */
397
- char *
398
- zfs_strip_partition_path (char * path )
399
- {
400
- char * newpath = strdup (path );
401
- char * sd_offset ;
402
- char * new_sd ;
403
-
404
- if (!newpath )
405
- return (NULL );
406
-
407
- /* Point to "sda1" part of "/dev/sda1" */
408
- sd_offset = strrchr (newpath , '/' ) + 1 ;
409
-
410
- /* Get our new name "sda" */
411
- new_sd = zfs_strip_partition (sd_offset );
412
- if (!new_sd ) {
413
- free (newpath );
414
- return (NULL );
415
- }
416
-
417
- /* Paste the "sda" where "sda1" was */
418
- strlcpy (sd_offset , new_sd , strlen (sd_offset ) + 1 );
419
-
420
- /* Free temporary "sda" */
421
- free (new_sd );
422
-
423
- return (newpath );
424
- }
425
-
426
435
#ifdef HAVE_LIBUDEV
427
436
428
437
/*
0 commit comments